CodeIgniter Internationalization with Templates

I found an easy way to use i18n with Templates. I think it’ not a REAL template system, but I got the job done. After the first config of my CodeIgniter setup I decided to implement a multilingual system. Following these steps I noticed that every translated page has it’s own view. This is not good, if my designer change one tag, one line, he would have to change every translated view.

The trick is simple, and I guess there is another elegant way to do this. The article I read said if I have a custom view called “myview”, I don’t need to rename this file to work with my default language, but my translated view must have a suffix, myview_pt.php for example. But in my case, even the default language must have the suffix, and the view without the suffix is my template.

The code bellow shows my custom controller, loading my view

[cc lang=”php”]
load_view(‘myview’); // Here I load my template that will be intercepted

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

My little modification begins here. I don’t need to call myview_pt from controller (checking what language first), because the modified controller from de article automaticaly does that. So, I simply create myview_en.php and myview_pt.php (assuming there is only those two languages), and the content of these files would be something like that:

[cc lang=”php”]
lang->load(‘myview’, ‘english’); ?>


On the first line, “myview” is a language file located on system/language/english/myview_lang.php.

This way I can put my whole html/css stuff on myview.php and work simulating a template. I hope someone could say in the comments “here’s the better way to do this” but until there I’ll use this tricky code to get things working.

That’s all!

Did you like this? Share it:


So, what do you think ?