Welcome Guest, Not a member yet? Register   Sign In
Translation as in Laravel
#1

Hi everyone, I did such a translation of files as in Laravel. You can see how I do a deal, someone can tell you how to do it better.

I created a library and in it I connect files in .json format


PHP Code:
class Language {

    protected 
$CI;
    public 
$deft_lang;
    public 
$json;
    public 
$patch;

    public function 
__construct()
    {
        
$this->CI =& get_instance();
        
$this->CI->load->helper('lang');
        
$this->CI->load->helper('cookie');
        
$lan substr($this->CI->config->item('language'), 02);
        
$this->deft_lang = (get_cookie('lang') != $lan) ? get_cookie('lang') : $lan;
        
set_cookie('lang'$this->deft_lang,93062220);
        
$this->patch =  APPPATH.'/lang/'.$this->deft_lang.'.json';
        if(
file_exists($this->patch))
        {
            
$json file_get_contents($this->patch);
            
$this->json json_decode($jsontrue);
        }
    }
    public function 
search($message,$par NULL)
    {
        if(!empty(
$this->json))
        {
            if (
array_key_exists($message$this->json))
                
$mes $this->json[$message];
            else 
$mes $message;
        }
        else 
$mes $message;

        return 
str_replace("%%s%%"$par$mes);
    }

after that I loaded the language helper.php helper through autoload.php and take the following
PHP Code:
function __($mess$var '')
{
    
$lang = new Language();
    return 
$lang->search($mess,$var);

and in the template it looks like this

Code:
<h1><?php echo __('Welcome to CodeIgniter!');?></h1>
but with the parameter

Code:
<p><?php echo __('If you are exploring CodeIgniter for the very first time, you should start by reading the %%s%%.','<a href="user_guide/">'.__('User Guide').'</a>');?></p>
I am very confused that I, in the helper, at each function call creates a class can somehow be different. I just did as I know, but I'm sure you can do a lot better.

and here is the .json file


Code:
{
    "Welcome to CodeIgniter!": "Добро пожаловать в CodeIgniter!",
    "The page you are looking at is being generated dynamically by CodeIgniter.": "Страница, которую вы просматриваете, динамически генерируется CodeIgniter.",
    "If you would like to edit this page you'll find it located at:": "Если вы хотите отредактировать эту страницу, вы найдете ее по адресу:",
    "The corresponding controller for this page is found at:": "Соответствующий контроллер для этой страницы находится по адресу:",
    "If you are exploring CodeIgniter for the very first time, you should start by reading the %%s%%.": "Если вы впервые исследуете CodeIgniter, вам следует начать с чтения %%s%%.",
    "User Guide": "Гид пользователя"
}
Reply


Messages In This Thread
Translation as in Laravel - by midav - 10-05-2019, 12:24 PM



Theme © iAndrew 2016 - Forum software by © MyBB