Internationalization and Partial Rendering in the CodeIgniter Framework
As experienced Web developers, it won’t shock you to find literally thousands of comparisons between CodeIgniter and the other popular frameworks you may be considering for your site. Each framework has its good and, ummm… less good points, but let’s say you’re wedded to CodeIgniter for whatever reason, and you’ve got a few languages you’re tasked with supporting so you’re getting set to work on internationalization. So you go back and refresh your memory of just how to do that, and you begin coming across certain sites saying that CodeIgniter doesn’t support internationalization. So you begin to ponder ditching CodeIgniter, since you’re aces in a couple other frameworks, but that would be a mistake. CodeIgniter is a genius framework, and well optimized for so many other tasks that it’s worth sticking with. Plus, we have a nifty solution to your CodeIgniter internationalization issues.
If you’re going to be developing a multi-language site, then when you’re setting up your CMS, from the beginning be sure to go for a CodeIgniter CMS like CI-CMS or Pyro CMS in order to minimize languages issues down the road. {The Zend framework, with its strong partial rendering, may seem like a better option in a pinch, but CodeIgniter trumps Zend in other crucial areas, so it’s worth sticking with.}
With CodeIgniter it makes great sense to build in modular separation (you can find the library here), though at first glace I’ll agree it looks like a tough implementation.
As you scan the library article, the lines to put most emphasis on will naturally deal with switching languages.
For example, you’ll find in the Notes section of the above library site:
<?php
anchor($this->lang->switch_uri('fr'),'Display current page in French');
$lang['about.gender'] = "Je suis un homme";
?>
Plug the lines below into your route.php and config.php files.
In config/route.php :
<?php $route['^fr/(.+)$'] = "$1"; $route['^en/(.+)$'] = "$1"; // '/en' and '/fr' URIs -> use default controller $route['^fr$'] = $route['default_controller']; $route['^en$'] = $route['default_controller']; ?>
In config/config.php,
$config['base_url'] must correspond to your configuration. For example,
<?php $config['index_page'] = ""; ?>
Creating the language file in system/application/language/english/about_lang.php was already clear to you.
<?php $lang['about.gender'] = "I'm a man"; $lang['about.gender'] = "Je suis un homme"; ?>
The controller part at
system/application/controllers/about.php should also be clear to you.
In the index function
<?php
$this->load->helper('language');
$this->load->helper('url');
// load language file‚
$this->lang->load('about');
$this->load->view('about');
?>
Once you’re done with these, browse to the welcome module on your local machine at http://localhost/ci/fr/welcome/test.
You should find the language lines show up as expected.
Be sure to hit the CodeIgniter resource site for some really useful, ready made down loadable translation files in zip format. This’ll save you heaps of time and work. Once you’ve extracted these zip archives, you’ll find files named as in the about_lang.php as above.
Next, while looking into partial rendering, we come across this site, which brings us more clarity.
<?php
$this->view->layout = 'master_layout_file'; // or leave this empty to render partials only
//Add a partial file and (optional) $data‚
$header = $this->view->load('header','header_file',$data);
// subheader‚
$header->load('sub_header','sub_header_file',$data);
$header->set($data);
$this->view->render();
?>
Now let’s render the partials in our master template:
<?php $header->render();?>;
And that’s about it. Pretty simple, right?
Before closing, just a brief note: PyroCMS is a multilingual CodeIgniter CMS with built-in language configuration options you can tailor to your site. You can find language settings inside the admin panel, and while the implementation will be similar to the approach we’ve outlined above it may not be an exact match.
Well, that’s it! Thanks for reading, and I hope this helps. If you have any questions about this specific tutorial or any other issues re: web development with CodeIgniter, Zend, or whatever else, drop us a line any time from our contact page!
Prakash R. Mohanty
Senior Software Engineer
Sourcebits Technologies
Tags: codeigniter, localization, multi-language, php, Web, Web Development, Zend Framework


