Yii 1.1: gettext

Prepares php's built-in gettext extension
1 follower

Yii extension: php-gettext initializer

Prepares php gettext extension





*. Clone this package into protected/extensions/

$ git clone git://github.com/acerix/yii-gettext.git protected/extensions/gettext

*. Define component and preload in protected/config/main.php:





        'class' => 'ext.gettext.components.GetText',

        // specify language_locale (could be based on domain name, URI, cookie, etc.)
        'language' => 'fr_ca', // eg. french_canada



*. Use gettext functions such as _($message) whenever displaying or defining a message.


    <?php echo _('All Rights Reserved.') ?>

    <?php echo CHtml::submitButton(_('Login')); ?>

    <?php printf(_('Copyright &copy; %d %s.'), date('Y'), Yii::app()->name) ?>

Use ngettext() to handle plurals:

            <?php $n = rand(0,2); printf(ngettext('%s die', '%s dice', $n), $n); ?>

And strftime() for dates/times:

            <?php echo strftime(_('Generated on %A, %B %e, %Y at %l:%M:%S %P %Z')); ?>

php 5.4+ or shorttags allows this shorthand:

    <?=_('All Rights Reserved.')?>

Read about gettext and how to write messages that are easy to translate:


Read more about php's gettext functions:


*. Scan your .php files for _() messages, and add them to a translation files

This can be done automatically using poedit ( http://www.poedit.net/ ) or other gettext tools

Using poedit:

    Add a 'new translation project', then add the directory: protected/extensions/gettext/locale/

    The example translation (fr_CA...yii.po) should now be listed in the catalog, double-click it

    Use 'Catalog > Update Sources' to scan for new messages

    Use 'Save As' to create a copy of the template for each desired translation (each needs the same directory structure as the example)

    poedit automatically generates the .mo files in the same folder when saving

NOTE: Translations should be saved as UTF-8

Be the first person to leave a comment

Please to leave your comment.

Create extension