Gettext and Poedit HOWTO

Example settings when using Gettext for i18n.

English language is used as source language and Lithuanian language lt is used as target language. In my example default Yii file names and paths are used, you can customize them later as you need. To use Gettext we must set CGettextMessageSource class for pre-declared application component named messages:

config/main.php
...
'language' => 'lt',
components => array(
	...
	'messages' => array(
		'class' => 'CGettextMessageSource',
	),
	...
),
...

In our code we use i18n-ready strings:

echo Yii::t( 'app', 'Login' ) ;
echo Yii::t( 'app', 'You have {quantity} new email|You have {quantity} new emails', array( 2, '{quantity}' => 2 ) ) ;

'app' is a message category, see t() method.

For first time we need to create and set up .PO file. Create a file named messages.po in protected/messages/lt folder. We will use Poedit as GUI. So open created file in text editor and put header: ~~~ msgid "" msgstr "" "Project-Id-Version: my-project.lt 1.0\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2014-11-15 14:38+0300\n" "PO-Revision-Date: \n" "Last-Translator: John Doe jd@example.com\n" "Language-Team: Translators without borders translators@example.com\n" "Language: lt\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" "%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-Generator: Poedit 1.5.4\n" "X-Poedit-SearchPath-0: ../..\n" ~~~ Some lines are self explanatory :) but some need more attention. ~~~ Language: lt ~~~ – language code exactly as in Yii::app()->language ~~~ Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2; ~~~ – plural forms for your language can be found here. Some to mention right now:

  • Lithuanian: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2;
  • Latviešu: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2;
  • Русский: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
    X-Poedit-KeywordsList: _ngettext:1,2;t:1c,2
    

    – two functions will be recognized as using gettext: _ngettext() and t().

t:1c,2 means that actual message for translation is second parameter in [Yii::t()] and first parameter is used as context. So in Yii::t( 'app', 'Login' ) the text to be translated is Login, and app will be generated as msgctxt ~~~ X-Poedit-SearchPath-0: ../.. ~~~ – means that poedit will scan files recursively starting from directory which is two levels up from messages.po file. It is frameworks protected folder.

Now we can close messages.po file in text editor and open it in poedit program (just double-click file). Press Update button and translatable strings will be collected. Put your translations in appropriate field. Press Save and messages.mo file will be created near to messages.po.

And that's all. See your application and you must see your translated strings.

0 0
2 followers
Viewed: 13 031 times
Version: 1.1
Category: Tutorials
Written by: Maug Lee
Last updated by: Maug Lee
Created on: Nov 15, 2014
Last updated: 9 years ago
Update Article

Revisions

View all history

Related Articles