Koseven has a fairly simple and easy to use i18n (translation) system. It is slightly modeled after gettext, but is not as featureful.
Koseven has a __()
function to do your translations for you. This function is only meant for small sections of text,
not entire paragraphs or pages of translated text.
To echo a translated string:
<?php echo __('Hello, world!');?>
This will echo Hello, world!
unless you've changed the defined language, which is explained below.
Use the I18n::lang() method to change the displayed language:
I18n::lang('fr');
This will change the language to French.
To define the language file for the above language change, create a i18n/fr.php
that contains:
<?php
return array
(
'Hello, world!' => 'Bonjour, monde!',
);
Now when you do __('Hello, world!')
, you will get Bonjour, monde!
You can define variables in your __()
calls like so:
echo __('Hello, :user', [':user' => $username]);
Your i18n key in your translation file will need to be defined as:
<?php
return array
(
'Hello, :user' => 'Bonjour, :user',
);
You can define your own __()
function by simply defining your own i18n class:
<?php
class I18n extends Kohana_I18n
{
// Intentionally empty
}
function __($string, array $values = NULL, $lang = 'en-us')
{
// Your functionality here
}
This will cause the built-in __()
function to be ignored.