Part of the EllisLab Network
   
1 of 2
1
Change language in config.php dynamic [RESOLVED]
Posted: 01 November 2007 02:40 PM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  332
Joined  09-11-2007

How do i do this??

In the constructor of the controller

$this->config->set_item('language''pt-br'); 

But didn’t work… any ideas? (Read better the user_guide)

 Signature 

cool mad
Rafael
Last.fm
Twitter

“Because they’re stupid, that’s why. That’s why everybody does everything!”

Profile
 
 
Posted: 01 November 2007 09:54 PM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  4777
Joined  03-23-2006

what is your language folder called and where is it saved?

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design

Profile
MSG
 
 
Posted: 02 November 2007 05:26 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  1
Joined  11-02-2007

Hello

I have the same problem. I created 3 folders (de, en and fr) in system/language/
When I autoload 1 of the three folders there is no problem. But when i want to change then the language he don’t react on my changes.

I try to change the language (in the views) with this code:

$this->lang->load('site'$headinfo['accountinfo']['language']); 

In $headinfo[‘accountinfo’][‘language’] is the userlanguage stored (de, en or fr)

My config folder:

$config['language']    "de"

It would be great if someone could help me! Thanks!

Profile
 
 
Posted: 02 November 2007 06:21 AM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

@rafael : have you got an /language/pt-br directory? if it’s easier for you just name it brasilian smile

@flicker : you have to change the language configuration setting

$this->config->set_item('language'$headinfo['accountinfo']['language']); 

This should work

Profile
 
 
Posted: 02 November 2007 10:47 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  332
Joined  09-11-2007

i have the folder called pt-br

i tried in
system/language/pt-br and system/application/language/pt-br, but both didn’t work.

whats the diferrence of brazilian and pt-br? i know that brazil is the only country that speaks “another” kind of portuguese, but don’t matter if is pt-br, brazilian, brazuca, mixugues, or otherelse the name of folder-se!

if i changed “manual” the value from config.php, it works soo fine that i almost cry. but dynamic neither Daileon can resolved!!!!

 Signature 

cool mad
Rafael
Last.fm
Twitter

“Because they’re stupid, that’s why. That’s why everybody does everything!”

Profile
 
 
Posted: 02 November 2007 11:37 AM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

and where or how do you want to use the string?

I don’t know when you make custom files you can depend on the autoloading to load the file, probably you would have to do something like

$this->lang->load('custom_file_lang');
$this->lang->line('showstring'); 

It doesn’t matter if you call it pt-br or brazilian, it’s where you feel most comfortable with.

Profile
 
 
Posted: 05 November 2007 06:29 AM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  332
Joined  09-11-2007

allright, but the problem is:

i have the string inside the config.php (the main config file). i didn’t create a custom config file, or my own file. just changed the original, the main, the genesis one!

system/application/config/config.php. So i think that i don’t need to load this, right?

In fact, what i wish is:

The user clicks in link that changes the language of site. And all the language strings are inside another file.

As flicker (the dolphin?) told, the system doesn’t react when i wanna change the $config[‘language’] in runtime.

 Signature 

cool mad
Rafael
Last.fm
Twitter

“Because they’re stupid, that’s why. That’s why everybody does everything!”

Profile
 
 
Posted: 05 November 2007 06:43 AM   [ Ignore ]   [ # 7 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

I meant by custom files, custom language files.
When the configured language is changed for instance the validation errors show the correct language strings.

I haven’t used custom language files myself because it seems that with language change comes page changes too so i set up separate view directories.

Profile
 
 
Posted: 05 November 2007 06:58 AM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  332
Joined  09-11-2007

i didn’t put in different views folders….

just did that:

application/views/hello_view.php

<h1><?=$this->lang->line('hello_title');?></h1>

<
p><?=$this->lang->line('hello_text');?></p

application/language/pt-br/app_lang.php

$lang['hello_title''Olá Mundo';
$lang['hello_title''Esta é uma aplicação padrão, só pra teste'

application/language/en/app_lang.php

$lang['hello_title''Hello World';
$lang['hello_title''Just testing application, don t worry!'

and this is working… but change dynamic don’t mad

 Signature 

cool mad
Rafael
Last.fm
Twitter

“Because they’re stupid, that’s why. That’s why everybody does everything!”

Profile
 
 
Posted: 05 November 2007 07:19 AM   [ Ignore ]   [ # 9 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

That is strange. I looked at the validation class and they use

$this->CI->lang->load('validation'); 

to load their language file so i guess you will have to reload your file in your controller after changing the language.

$this->lang->load('app'); 
Profile
 
 
Posted: 05 November 2007 08:05 AM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  332
Joined  09-11-2007

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

$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:

$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.

 Signature 

cool mad
Rafael
Last.fm
Twitter

“Because they’re stupid, that’s why. That’s why everybody does everything!”

Profile
 
 
Posted: 05 November 2007 08:52 AM   [ Ignore ]   [ # 11 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

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.

Profile
 
 
Posted: 05 November 2007 10:06 AM   [ Ignore ]   [ # 12 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  332
Joined  09-11-2007
xwero - 05 November 2007 01:52 PM

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.

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!

 Signature 

cool mad
Rafael
Last.fm
Twitter

“Because they’re stupid, that’s why. That’s why everybody does everything!”

Profile
 
 
Posted: 05 November 2007 02:43 PM   [ Ignore ]   [ # 13 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

i’ve made an extended language method for switching to another language even if you have autoloaded files

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.

Profile
 
 
Posted: 25 December 2007 03:59 PM   [ Ignore ]   [ # 14 ]  
Summer Student
Avatar
Total Posts:  4
Joined  04-17-2007

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:

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_loadedTRUE)){
        
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

Profile
 
 
Posted: 16 January 2008 06:34 AM   [ Ignore ]   [ # 15 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

@michaël : 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

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

// 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="<?php echo site_url('admin/langswitch/english/'.this->uri->uri_string().'/'); ?>">English</a

I’m using a session variable but nothing stops you to use an url segment.

Profile
 
 
   
1 of 2
1