PinoyTech.org

CodeIgniter, Kohana, Mootools, jQuery and CSS

Kohana 2.4 I18N (internationalization and localization) Library

Posted by teejay on April 24, 2010

I absolutely love Kohana 2.x versions. Although, I've been reading up on Kohana 3.x, I still can't hide my love. I've been reading up on the source of the Kohana 2.4 new I18N Library. It's quite different from the 2.3 version, but not too much.

Usage:

First of all, you'd want to set_locale, so that the system knows which language you'd be using. The default is 'en_US' as indicated in the Kohana Class. I'd put it in a Base Controller so that all Controllers inherit it.

I18n::set_locale('tl_PH');

Now, we set up files to contain your translations. It is important to know that you can set up 2 files for each language. One would be for internalization and the other would be for localizations.

For this example we'd be writing one for the tagalog language. We create a file in the i18N folder and name it, tl.php. We create an array named $translations. The keys will be the key for phrases you'd want to translate.

<?php

$translations['good_morning'] = 'Magandang Umaga';

We can now use this in the controller or view like so:

__('good_morning'); // would produce 'Magandang Umaga'

It is important to know that unless you change your locale other than 'en_US', it will use the key.

// base_controller
I18n::set_locale('en_US');
// translations
$translations['good_morning'] = 'Magandang Umaga';

__('good_morning'); // would produce 'good_morning'

To get rid of this problem, it is recommended to use the real words as the key:

// base_controller
I18n::set_locale('en_US');
// translations
$translations['Good Morning'] = 'Magandang Umaga';

__('Good Morning'); // would produce 'Good Morning'

Here's a more practical example on how to use the the Il8n Library:

<?php
__(':good_morning, teejay', array(':good_morning' => 'Good Morning')); // would produce 'Good Morning, teejay'

I hope this would be useful to someone else. Happy coding!

Categories: Web Development

Tags: kohana

1 Comments

Teejay

Hi David, from my example, I created a file named, tl.php. You can add a US.php and a GB.php file for $translations for those countries.

May 4th 2010

Comments are not allowed