• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.