GetText and Yii

Hello :)

Did anybody use gettext in Yii applications?

Is it possible to use:


_("MessageID")

instead of:


Yii::t("Category", "MessageID")

?

And where I can find tutorial about setting up and using gettext with Yii applications?

how can i configure yii to use gettext for translation?

I’m no Yii-ninja (yet :lol:) and like you I had a hole lot of trouble to find help on this, but here’s my solution:

I’m using a __() wrapper function (note the double underscore) which makes calls to Yii::t(). I’m using Poedit to generate and edit my .po files. I still haven’t managed to get the .mo files to work with Yii, so I’m setting the useMoFile flag FALSE.

I’m storing my .po in protected/messages/pt/messages.po (pt is for Portuguese language)

Hope that helps ;)

protected/config/main.php




// Custom Includes

require_once(dirname(__FILE__).'/../includes/localization.php');


return array(


	'language' => 'pt',


	'components' =>array(

		"messages" => array(

			"class" => "CGettextMessageSource",

			"useMoFile" => FALSE,

		),

        )


        (...)

)



protected/includes/localization.php




/**

 * Wrapper function for Yii::t()

 */

function __($string, $params = array(), $category = "") {

	return Yii::t($category, $string, $params);

}



I just created an experimental component which prepares php’s gettext extension (it doesn’t use Yii’s translation mechanisms or messages at all)

git it here: github.com/acerix/yii-gettext

An example translation file is included which can be used as a template (use poedit to create/edit translations)

Any feedback is welcome, and I am quite new to Yii and git, so use this at your own risk and let me know if I did something wrong.

Cheers

Why not just use this existing extension? http://www.yiiframework.com/extension/pophpcommand/