-->

Yii2 best practices translating dynamic content

2019-05-10 13:23发布

问题:

Can anyone share own experience and best practices implementing multilingual sites with Yii2? I want translate user input that is stored in database. For example article, that may have its name in three different languages, body and some translatable attributes as well.

Does Yii2 have built in features to translate the dynamic content? Or should I use third party extensions like these ones below:

https://github.com/creocoder/yii2-translateable

https://github.com/LAV45/yii2-translated-behavior

https://github.com/lajax/yii2-translate-manager

Your help would be appreciated.

回答1:

Well, I can give you my point of view only based on what I have done.

There are to places to work translation

  1. The non dynamic strings managed with i18n and messages system from yii, that will help you with static content.
  2. Working the translated routes dynamically with a bootstrapped class, that allows you to build this routs when the app is built.
  3. And working with tables that have columns that support the translation like 'title_en, title_es', and as many as you need to translate. Actually in your admin interface you may want to use something like yandex to help you translating the content to this fields.

Now I will explain:

The i18n Message Translation is based on translating strings in your views, models, and in some cases like on the bootstrapped class.

You will en using Yii::t('app/main', 'Your name is {0}' as an example to translate strings that are stored on message php files.

Now if you translate stings you will want to translate the routes so you will en with routes like /articles and /articulos when you change the language. for this purpose you will like to build a class that implements BootstrapInterface and that will be called from the process of bootstrapping your app.

So this is an example of my settings.php that I use for this

namespace app\base;

use Yii;
use yii\base\BootstrapInterface;

class settings implements BootstrapInterface {

    public function __construct() { }

    public function bootstrap($app) {

        /// Dynamic translated routes

        $t_articles = Yii::t('app/route', 'articles');

        $app->getUrlManager()->addRules([
            '/'.$t_articles => '/articles',
        ], false);
    }
}

And remember to bootstrap the class in your config file «i.e. web.php»

'bootstrap' => [
    'log',
    'app\base\settings',
],

And finally to translate text from the database you may want to make a table that supports the translated text like:

CREATE TABLE articles (
   id INT,
   title_en VARCHAR(20),
   title_es VARCHAR(20)
);

So when you call your app you can pull your data using something like the following on the action (only a simple example):

$articles = ArticlesA::find()->where(['id' => 1])->one();
$lang = $this->module->language;
return $thi

s->render('index',['articles'=>$articles, 'lang'=>$lang]);

or in the view as:

<p class="lead"><?=$articles['title_'.$lang]?></p>

I hope this explains the way I have been translating my apps.



回答2:

Use a Google translator API or Yandex API to for smooth translations for multiple languages. Few links that i have found on git

https://github.com/borodulin/yii2-i18n-google

Tutorial

RichWeber/yii2-google-translate-api

Google Api is a paid service however you can get free credit for 12 months if your a first time user