I'm working with Yii2 Framework, and already configured i18n component using this guide: http://thecodeninja.net/2014/12/i18n-with-yii-2-advanced-template/
So, I can now translate strings within my php files using Yii::t()
function. Translatable strings are extracted using $ ./yii message/extract
console command, which generates proper translation files.
I now need to display translations for strings stored in the database.
I could use Yii:t()
with a variable instead of a string as an argument like this
echo Yii:t('app', $some_string_from_db );
and make a new php file with some code like this
<?php
function dbStringsToTranslate() {
$o = Yii::t('app','db english string 1');
$o.= Yii::t('app','db english string 2');
$o.= Yii::t('app','db english string 3');
return $o;
}
This way $ ./yii message/extract
command will find the needed translations.
This is working Ok, but of course $ ./yii message/extract
is throwing some warnings anywhere I use Yii:t()
with variables.
Skipping line 39. Make sure both category and message are static strings.
I think this is not a big deal, but well, here is my question:
Is this a right way to translate strings stored in a database?
Is there a better way to accomplish this?
You can check out this extension. https://github.com/creocoder/yii2-translateable it allows for attaching behaviors to models to support multiple languages.
I am using it now in a projects and it is easy to use.