Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Gustavo Rubio gusrub

🏠
Working from home
View GitHub Profile
@gusrub
gusrub / index.php
Created Apr 26, 2018
Keyword arguments for translation PHP
View index.php
<?php
putenv("APP_LANG=es_MX.utf8");
require_once('i18n.php');
class Person
{
const VERSION = '1.0.0';
public $firstName;
@gusrub
gusrub / i18n.php
Created Apr 26, 2018
i18n translation class
View i18n.php
<?php
use \InvalidArgumentException as InvalidArgumentException;
/**
* This class contain helper methods to localize and translate the application.
*/
class I18n
{
/**
@gusrub
gusrub / index.php
Created Apr 26, 2018
Encapsulating translation
View index.php
<?php
require_once('i18n.php');
use function Helpers\I18n\translate;
class Person
{
const VERSION = '1.0.0';
public $firstName;
View i18n.php
<?php
namespace Helpers\I18n {
// I18N support information here
$language = 'es_MX.utf8';
putenv("LC_ALL=$language");
putenv("LC_LANG=$language");
setlocale(LC_ALL, $language);
// // Set the text domain as 'messages'
@gusrub
gusrub / index.php
Last active Apr 16, 2018
Internationalization PHP 1
View index.php
<?php
class Person
{
const VERSION = '1.0.0';
public $firstName;
public $lastName;
public $dob;
View mvc_controller_5.rb
class UsersController < ApplicationController
def create
service = CreateUserService.new(@user, params[:send_email])
if service.perform
render json: service.output, status: 200
else
render json service.errors, status: 400
end
end
View mvc_controller_4.rb
class UsersController < ApplicationController
def create
service = CreateUserService.new(@user, params[:send_email])
if service.perform
render json: service.output, status: 200
else
render json service.errors, status: 400
end
end
View create_user_service.rb
# service name hints that it does
class CreateUserService
# default attributes that all services should have
attr_reader :errors, :messages, :output
# custom attributes for what this service does
attr_reader :user, :send_email
# we receive all parameters in the initialization
def initialize(user:, send_email: true)
View token_provider.rb
class TokenProvider
def self.generate(email)
uri = URI("https://example.com/#{email}")
response = Net::HTTP.get_response(uri)
if response.code == '200'
return JSON.parse(response.body)[:token]
end
end
end
View mvc_controller_3.rb
class UsersController < ApplicationController
include UsersHelper
def create
if params[:token].blank?
@user.token = generate_token(@user)
end
if @user.save
send_welcome_email(@user) if params[:send_email]
render json: @user, status: 200
You can’t perform that action at this time.