Yii and PHP Gettext

2019-03-03 09:49发布

问题:

I want to make my yii app multilanguage. To do this I want to use gettext (because its much simpler than yii messages).

To this I used this yii extension, I configured the PO files, I made the translations, etc.

The big problem: nothing happened. Nothing was translated.

回答1:

I can advice you this awesome multilanguage extension!

http://www.yiiframework.com/extension/tstranslation/



回答2:

To use gettext without any extension follow those steps. In config/main.php set yout target language like this:

'language' = 'ru',

Set messages component to use CGettextMessageSource:

'messages' => array(
    'class' => 'CGettextMessageSource',
),

Create messages.po file in protected/messages/ru folder (note: folder name is same as language code). If poedit is used messages.po file must have appropriate headers. Example:

msgid ""
msgstr ""
"Project-Id-Version: FOO BAR 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2011-11-11 11:11+0300\n"
"PO-Revision-Date: \n"
"Language: ru\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\n"
"X-Poedit-SourceCharset: utf-8\n"
"X-Poedit-Basepath: .\n"
"X-Poedit-KeywordsList: _ngettext:1,2;t:1c,2\n"
"X-Poedit-SearchPath-0: ../..\n"

Note t:1c,2. This means, that first parameter from function Yii::app() will be used as context (see msgctx) and second as actual string to translate. Without this your i18n will not work!

Now just open messages.po in poedit → Update → do translations → Save. And messages.mo file will be created and used by Yii.

For you language plurals string see gettext help.