Welcome Guest, Not a member yet? Register   Sign In
Tell me how to correctly implement the translation in the likeness of Laravel
#1

I have expanded the core of Lang.php

PHP Code:
class MY_Lang extends CI_Lang
{
    public 
$config;
    public function 
__construct()
    {
        
parent::__construct();
        
$this->config =& get_config();
    }
    public function 
lo()
    {
        
$json_arr = array();
        
$lan substr($this->config['language'], 02);
        
$deft_lang = (get_cookie('lang') != $lan) ? get_cookie('lang') : $lan;
        
set_cookie('lang'$deft_lang,93062220);
        
$patch APPPATH.'language/'.$deft_lang.'.json';
        if(
file_exists($patch))
        {
            
$json file_get_contents($patch);
            
$json_arr json_decode($jsontrue);
        }
        return 
$json_arr;
    }

and created a helper and added it to the auto connection.

PHP Code:
if ( ! function_exists('__')) {
    function 
__($mess$var '')
    {
        
$CI =& get_instance();
        
$lang $CI->lang->lo();

        if (!empty(
$lang)) {
            if (
array_key_exists($mess$lang))
                
$mes $lang[$mess];
            else 
$mes $mess;
        } else 
$mes $mess;

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

and call me in the right place like this.

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

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>
can see how it can be implemented even better.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB