i18n


Description


The I18n class makes it easy to create a multilingual web application. You can add as many languages as you want.

The language packs are located in the app/resources/i18n directory and the files for each language are located in a subdirectory.

The name of a language pack should use the following convention: en_US (ISO 639-1 language code and ISO 3166-1 alpha-2 country code).


Language files


Mako language files are just simple arrays where the array key is the original language and the value is the translation.


<?php

return array
(
	'I have 10 apples'  => 'Jeg har 10 epler',
	'I have %d apples'  => 'Jeg har %d epler',
	'Hi! My name is %s' => 'Hei! Jeg heter %s',
);

Methods


language([string $language = null])


Setting the default language you want to use use is done by calling the language method. The method also returns the name of the default language.


I18n::language('no_NO'); // Setting the language to Norwegian

$language = I18n::language(); // The value of $language is now "no_NO"

translate(string $string [, array $vars = array() [, string $language = null]])


The translate method returns the translated string. If no translation is found then the original string is returned. You can also use the __ convenience function.


// Set default language to Norwegian

I18n::language('no_NO');

// Will print "Jeg har 10 epler"

echo I18n::translate('I have 10 apples');

// Will print "Jeg har 20 epler"

echo I18n::translate('I have %d apples', array(20));

// Will print "Hei! Jeg heter Iroquois Pliskin"

echo __('Hi! My name is %s', array('Iroquois Pliskin'));

pluralize(string $word [, int $count = null [, string $language = null]])


The pluralize method returns the plural form of the chosen noun. Unlike most other frameworks Mako will use language based inflection rules when pluralizing words.


The pluralize method will only work if the chosen language pack includes inflection rules.

echo I18n::pluralize('woman'); // Will print "women"

echo I18n::pluralize('woman', 1); // Will print "woman"

echo I18n::pluralize('woman', 2); // Will print "women"

You can also pluralize words in translated strings:


// In a language file
 
return array
(
	'new_messages' => 'You have %1$u new <pluralize:%1$u>message</pluralize>.',
);
 
// In a view (using the template syntax)
 
{{__('new_messages', array(1)}} // Will print "You have 1 new message."

{{__('new_messages', array(10)}} // Will print "You have 10 new messages."