Internationalization, loading language files, preventing "copy-paste" |
[eluser]Bikun[/eluser]
I just implemented internationalization and it works quite well, but I have one question. Here is a guide I was dealing with while implementing feature: http://maestric.com/en/doc/php/codeigniter_i18n There is such lines in About Controller: Code: // load language file And here is a question. I have 1 file for every language. It doesn't seem to be very clever to load language file in every function of concrete controller before calling view. I tried to call language file from Super Controller, but then language doesn't change. Also tried to load file from Constructor of About controller. Language doesn't change either. Is there a place where to load this file once and for every function of every controller?
[eluser]whitey5759[/eluser]
Not sure if you have tried this but what I did was extend the Controller class and in the constructor I loaded the language. I too had just one Language file for the whole system, called 'core'. So something like this. class MY_Controller extends Controller { function MY_Controller() { parent::Controller(); //Get the language for the site and load it (ready for use) $language = XXXXX; //Get the language however you want $this->lang->load("core", $language); } } Then you just need to make sure you extend MY_Controller when you create your Controllers (as extending just Controller would circumvent your new MY_Controller class) and make sure you call the MY_Controller constructor, and viola the language will have already been loaded in the constructor of the MY_Controller. So like this: class UserAdmin extends MY_Controller { function UserAdmin() { parent::MY_Controller(); } }
[eluser]Bikun[/eluser]
Yep, I tried it. It loads default language and when I try to switch language, it doesn't switch But switching language works perfectly when I place language loading just before view load. Is there a way of calling some function every time just before calling constructor function?
[eluser]whitey5759[/eluser]
Hmm that' weird coz it works for me. Are you sure you are specifying the right value in your call to $this->lang->load? In my example $language is gotten from the users session variables. So to change language all I do is change the session variable, then when the user visits a Controller the session variable will be gotten, assigned to $language, and it works.
[eluser]Bikun[/eluser]
Found out that hooks were not enabled in the application configuration on the server Basically it works now with setting language in a constructor. Thanks. |
Welcome Guest, Not a member yet? Register Sign In |