Welcome Guest, Not a member yet? Register   Sign In
Changing site language on-the-fly
#11

[eluser]xwero[/eluser]
Inside the extended language file you don't have to fetch the is_loaded array from the CI object, you can directly get it from the parent class so $this-> is sufficient. The $this->lang->is_loaded is to check the files in a controller.

I just checked the code again and it works for me so i don't know why you guys are having problems. It's a pretty basic function;

- set the new idiom
- get all loaded files from previous idiom
- empty the loaded files variable
- add all files from the new idiom
#12

[eluser]baalwww[/eluser]
In answer to the earlier question from xwero about 'when' I call it, I call it in my application controller... this context is my admin panel, and I have links at the bottom of it with the language names, so I have a function 'language' in my 'admin' controller, and call it there.
#13

[eluser]baalwww[/eluser]
So here's what I did. I changed the line of code from
Code:
$loaded=$this->is_loaded;
to
Code:
$loaded=$CI->lang->is_loaded;

which sets $loaded to the array containing the currently loaded language files. I checked the array and it has the correct files in it.

The problem is that the load line resolves to (based on the error message) language/file_lang.php instead of, for example, language/spanish/file_lang.php

I verified that $idiom contains the language name. I also tried changing the load to have $idiom as a second argument, which should force the use of the language, but it doesn't work.
#14

[eluser]LuckyFella73[/eluser]
Finally it works for me.

My mistake was typing
Code:
$this->language->switch_to('english');

instead of
Code:
$this->lang->switch_to('english');

I thought I have to write "language" because ussually
you write
Code:
$this->class_name->function_of_that_class($params);

But a look at the user guide told me ...

@ xwero
Code:
print_r($this->lang->is_loaded);

works when written in my controller. I first tested in the
switch_to function. Thanks for your support!
#15

[eluser]baalwww[/eluser]
Glad you got it working...I'm still broken. Here is what my code looks like now.

application/libraries/My_Language.php
Code:
class MY_Language extends CI_Language
{
  function MY_Language()
    {
        parent::CI_Language();
    }
    /**
    * Makes switching between languages easier
    *
    *  example : http://ellislab.com/forums/viewreply/339962/
    */
    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));
            }
        }
    }
}

the line in my controller
Code:
$this->lang->switch_to($lang);
with $lang being verified as 'spanish'. Now that I have 'lang' instead of 'language' based on the response above, I *do* have values in the is_loaded array...but

the error message I receive now is:
Code:
Unable to load the requested language file: language/messages_lang.php

which I assume should really be 'language/spanish/messages_lang.php
#16

[eluser]LuckyFella73[/eluser]
Maybe there is something wrong with your languages files?

In system/languages I have two folders. The ci basic "english"
lang folder and for testing a additional "german" folder.
To check if all is working I placed a "test_lang.php" and
"test2_lang.php" file in both language folders.

In autoload.php I have:
Code:
$autoload['libraries'] = array('language');
# and
$autoload['language'] = array('test','test2');

My test-controller (the standard "welcome" slightly extended):
Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();
        $this->lang->switch_to('german');
    }
    
    function index()
    {
        print_r($this->lang->is_loaded);
        $this->load->view('welcome_message');
    }
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */

In my config.php I have defined "english" as the standard language,
so I can test with switching to german in the controller. I just wanted
to get this to work, so I just edited the welcome controller ...

In my view I have:
Code:
<?php echo($this->lang->line('test_say'));?>
So I can see if the right string comes out.

Hope you get it to run as well!


EDIT:
PS: I didn't change anything in the My_language class - took it like xwero posted
#17

[eluser]baalwww[/eluser]
I changed my My_language back to xwero's code after I read your post about lang instead of language. That fixed the problem that had caused me to change the code.

The difference I see between your code and mine is two things:

1. My language files are in the application/language directory instead of the system/language. This doesn't seem to stop them from being loaded, or from is_loaded finding them, but if that could in some way be my problem.

2. I use lang( to retrieve my strings instead of $this->lang->line
#18

[eluser]LuckyFella73[/eluser]
hmm, hard to say what is wrong. Do you get an error message or
is the language string not shown correctly?

I have to leave office now and will be back on monday.
If you want, I send you my application then and you can compare the code bedder!
#19

[eluser]baalwww[/eluser]
I get the message I put above... that 'language/filename_lang.php is not found (this is on the load command). I think it should be language/spanish/filename_lang.php




Theme © iAndrew 2016 - Forum software by © MyBB