I18n
Kohana拥有一个相当简单的、易于使用的I18n系统。在gettext后略微建模,但不是作为特征。如果你需要gettext的功能,请使用:) (原文#Kohana has a fairly simple and easy to use i18n system. It is slightly modeled after gettext, but is not as featureful. If you need the features of gettext, please use that :))
__()
Kohana中有一个()函数来为你做翻译。此函数仅为小部分的文字翻译,而不是为整个段落或网页的文本翻译。 (原文#Kohana 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!');?>
这将输出’Home’ ,除非你改变定义的语言,解释如下 (原文#This will echo ‘Home’ unless you’ve changed the defined language, which is explained below.)
更改显示语言 (原文#Changing the displayed language)
使用i18n::lang()的方法来更改显示语言: (原文#Use the I18n::lang() method to change the displayed language:)
I18n::lang('fr');
这将更改语言为’es-es’ This will change the language to ‘es-es’.
定义语言文件 (原文#Defining language files)
要为以上语言的更改定义语言文件,创建一个包含如下内容的i18n/fr.php
文件:
(原文#To define the language file for the above language change, create a i18n/fr.php
that contains:)
<?php
return array
(
'Hello, world!' => 'Bonjour, monde!',
);
现在,当你使用__('Hello, world!')
的时候,你将获得Bonjour, monde!
(原文#Now when you do __('Hello, world!')
, you will get Bonjour, monde!
)
I18n的变量 (原文#I18n variables)
你可以在你的()定义像这样来调用的变量: (原文#You can define variables in your () calls like so:)
echo __('Hello, :user', array(':user' => $username));
你在你的翻译文件中的i18n的键需要这样来定义: Your i18n key in your translation file will need to be defined as:
<?php
return array
(
'Hello, :user' => 'Bonjour, :user',
);
定义你自己的()函数 (原文#Defining your own () function)
通过简单地定义你自己的I18N类,你可以定义你自己的()函数: (原文#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.)