Welcome Guest, Not a member yet? Register   Sign In
Change language in config.php dynamic [RESOLVED]
#11

[eluser]ELRafael[/eluser]
OK. What you told me is not the solution, but clears my brain, thanks!!!

What i done wrong:

In the config/autoload.php, i had autoloaded the language, in
Code:
$autoload['language'] = array('app');

So, whatever i change in the controller, don't make any effect.

I removed the language in autoload.php and set like that in the controller:

Code:
$this->lang->load('app', 'pt-br'); //This line in class constructor :)

And what's happen? What i wanted!!

As i wrote in the first post, the solution was read better the userguide
Loading A Language File

In order to fetch a line from a particular file you must load the file first. Loading a language file is done with the following code:
$this->lang->load('filename', 'language');

Where filename is the name of the file you wish to load (without the file extension), and language is the language set containing it (ie, english). If the second parameter is missing, the default language set in your application/config/config.php file will be used.


and

Auto-loading Languages

If you find that you need a particular language globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload.php file and adding the language(s) to the autoload array.
#12

[eluser]xwero[/eluser]
It doesn't makes much sense that the auto loaded files don't change if you change the language then what is the point of having different directories?
so following the user guide you would have to have en_app_lang.php and br-pt_app_lang.php in both directories?

With the lang->load method it should be possible to overwrite the autoloaded stings but that doesn't work either.

It seems like the autoloading of languages files could need some more work.
#13

[eluser]ELRafael[/eluser]
[quote author="xwero" date="1194288743"]It doesn't makes much sense that the auto loaded files don't change if you change the language then what is the point of having different directories?
so following the user guide you would have to have en_app_lang.php and br-pt_app_lang.php in both directories?

With the lang->load method it should be possible to overwrite the autoloaded stings but that doesn't work either.

It seems like the autoloading of languages files could need some more work.[/quote]

so following the user guide you would have to have en_app_lang.php and br-pt_app_lang.php in both directories?
NO. The structure is that:

system/application/language/en/app_lang.php
system/application/language/it/app_lang.php
system/application/language/pt-br/app_lang.php

one dir for each language.
i copy this structure from bambooinvoice. and, to me, is fine!

I agree that makes no sense autoload. But worked (thanks God and Daileon).
If you wanna see the result (in fact, i'm making this personal app for a future video tutorial), http://projetos.elrafael.net/videos


To all that helped, thanks a lot! maybe someday i'll pay a true brazilian beer!
#14

[eluser]xwero[/eluser]
i've made an extended language method for switching to another language even if you have autoloaded files
Code:
class MY_Language extends CI_Language
{
    
    
    function MY_Language()
    {
        parent::CI_Language();
    }
    
    function switch_to($idiom)
    {
        $CI =& get_instance();
        $CI->config->set_item('language',$idiom);
        $loaded = $this->is_loaded;
    $this->is_loaded = array();
        
    foreach($loaded as $lang)
    {
        $this->load($lang);    
    }
    }
    
}
It's easier to make multi language sites i think.

I just can't understand why autoloaded files would have to remain bound to the static language configuration when you switch to another language.
#15

[eluser]mick_sp[/eluser]
I've been trying to get this to work, but it didn't. Of course extending the core Language-class is de cleanest solution, but when i didn't get that to work (i think there's some $this/$CI mixup in there somewhere) i just added an extra parameter to the Language->load method (i, know, i know, never change the core files) like so:
Code:
function load($langfile = '', $idiom = '', $return = FALSE, $force=false) {    
  $langfile = str_replace(EXT, '', str_replace('_lang.', '', $langfile)).'_lang'.EXT;
    if(!$force){
      if (in_array($langfile, $this->is_loaded, TRUE)){
        return;
      }        
    }
    ...
    ... rest of the method ...
}
I'd like a cleaner way of doing this, in fact i propose to do something like this in the future core files. I think its a bug that the class thinks the file is loaded when in fact it is loaded in a different language. (if there's one thing a language class should do, that would be knowing which language is loaded, not just which file Smile )

Michaƫl
#16

[eluser]xwero[/eluser]
@michael : Sorry for the late answer, your post got lost during dinner Wink

I just used it myself and it works great but i added an extra check
Code:
class MY_Language extends CI_Language
{
    
    
    function MY_Language()
    {
        parent::CI_Language();
    }
    
    function switch_to($idiom)
    {
        $CI =& get_instance();
        if(is_string($idiom) && $idiom != $CI->config->item('language'))
        {
            $CI->config->set_item('language',$idiom);
            $loaded = $this->is_loaded;
            $this->is_loaded = array();
                
            foreach($loaded as $file)
            {
                $this->load(str_replace('_lang.php','',$file));    
            }
        }
    }
    
}

i use it as follows
Code:
// controller
class Admin extends Controller
{
    
    function Admin()
    {
        parent::Controller();
        $this->lang->load('cms_form_msg');
        $this->lang->load('cms_form');
        $this->lang->switch_to($this->session->userdata('language'));
    }

        function langswitch()
    {
        $this->session->set_userdata('language',$this->uri->segment(3));
        redirect(str_replace('_','/',$this->uri->segment(4)));
    }
}
// view (the site_url function replaces the forward slashes with underscores)
<a href="&lt;?php echo site_url('admin/langswitch/english/'.this->uri->uri_string().'/'); ?&gt;">English</a>
I'm using a session variable but nothing stops you to use an url segment.
#17

[eluser]Sixer[/eluser]
Thanks for this. Works like a charm.

Note: It doesn't work as expected if $this->config->set_item('language','...') is not done before the language switch is used. So set the language in the config using $this->config->set_item() before you attempt to use $this->lang->switch_to().
#18

[eluser]Unknown[/eluser]
I had the same problem and it helped me a log! thanks
#19

[eluser]franboud[/eluser]
Thanks for all the tips! It helped me overcome a problem.
Thanks again.




Theme © iAndrew 2016 - Forum software by © MyBB