When we need to switch to other language for different clients, we can use function SL() to set special language.
SL('zh_cn');
We switch to Simple Chinese above, which is the folder name as bebow:
.
|-- en
| `-- common.php
`-- zh_cn
`-- common.php
Of course, we can set the language according to the parameter from clients. Take $_GET['lan']
for example.
SL($_GET['lan']);
After set the language, we need to add the related translation file into folder ./Language
. That means, if we set language to en
, we need translation file ./Language/en/common.php
; if zh_cn
, then ./Language/zh_cn/common.php
.
The translation file is very simple. It is nothing but a mapping array. The keys are original content, while the value is translation.
Here is an example of Simple Chinese.
return array(
'Hi {name}, welcome to use PhalApi!' => '{name}您好,欢迎使用PhalApi!',
'user not exists' => '用户不存在',
);
After set the language and prepare the translation file, we can translate content with fast function T() in the project.
echo T('user not exists');
It will show:
用户不存在
Sometimes we have to display some dynamic data for different sutiation. Dynemic variable should be inside between with bracketing without any blank.
echo T('hello {name}', array('name' => 'dogstar'));
While the translation is:
'hello {name}' => '您好,{name}',
We will see:
您好,dogstar
If there are many variables, we can also use numeric index for convenience.
echo T('{0} I love you because {1}', array('PhalApi', 'no reasons'));
While the translation is:
'{0} I love you because {1}' => '{0} 我爱你因为{1}',
It will print:
PhalApi 我爱你因为no reasons
It is straightforward that when there is no translation, it will output the original content.
In addtion, if translation files are not under folder ./Language
but other fodler else, we can add the new folder by PhalApi_Translator::addMessage().
Take library User for example,
PhalApi_Translator::addMessage(API_ROOT . '/Library/User');
And under folder /path/to/Library/User
, it should be like this:
./Language/
└── zh_cn
└── common.php
1 directory, 1 file