Every day, we meet more and more international companies, software products, and information resources that publish content on multiple languages. Yii2 provides built-in i18n support for making multilanguage applications.
In this recipe, we are translating the application interface to different languages.
Create a new yii2-app-basic
application using the composer, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.
views/layouts/main.php
file to use the Yii::t('app/nav', '...')
method:echo Nav::widget([ 'options' => ['class' => 'navbar-nav navbar-right'], 'items' => [ ['label' => Yii::t('app/nav', 'Home'), 'url' => ['/site/index']], ['label' => Yii::t('app/nav', 'About'), 'url' => ['/site/about']], ['label' => Yii::t('app/nav', 'Contact'), 'url' => ['/site/contact']], ... ], ]);
Yii::t('app, '...')
method:$this->title = Yii::t('app', 'Contact'); $this->params['breadcrumbs'][] = $this->title;
<div class="form-group"> <?= Html::submitButton(Yii::t('app', 'Submit'), ['class' => 'btn btn-primary'']) ?> </div>
Change other hard-coded messages as well:
<p> <?= Yii::t('app', 'The above error occurred while the Web server was processing your request.') ?> </p>
ContactForm
model:class LoginForm extends Model { ... public function attributeLabels() { return [ 'username' => Yii::t('app/user', 'Username'), 'password' => Yii::t('app/user', 'Password'), 'rememberMe' => Yii::t('app/user', 'Remember Me'), ]; } }
Also, change the attribute labels of the LoginForm
model:
class ContactForm extends Model { ... public function attributeLabels() { return [ 'name' => Yii::t('app/contact', 'Name'), 'email' => Yii::t('app/contact', 'Email'), 'subject' => Yii::t('app/contact', 'Subject'), 'body' => Yii::t('app/contact', 'Body'), 'verifyCode' => Yii::t('app', 'Verification Code'), ]; } }
It will output translated labels for the current language instead of originals.
messages
directory. Right now, we can create translation files for all needed languages. We can do it manually, but there is a helpful crawler that can scan all project files and extract all messages from Yii::t()
constructions. Let's use it../yii message/config-template config/messages.php
<?php return [ 'sourcePath' => '@app', 'languages' => ['de', 'fr'], 'translator' => 'Yii::t', 'sort' => false, 'removeUnused' => false, 'markUnused' => true, 'only' => ['*.php'], 'except' => [ '.svn', '.git', '.gitignore', '.gitkeep', '.hgignore', '.hgkeep', '/messages', '/vendor', ], 'format' => 'php', 'messagePath' => '@app/messages', 'overwrite' => true, 'ignoreCategories' => [ 'yii', ], ];
./yii message config/messages.php
messages ├── de │ ├── app │ │ ├── contact.php │ │ ├── nav.php │ │ └── user.php │ └── app.php └── fr ├── app │ ├── contact.php │ ├── nav.php │ └── user.php └── app.php
messages/de/app/contact
file contains the following content:<?php ... return [ 'Body' => '', 'Email' => '', 'Name' => '', 'Subject' => '', ];
<?php ... return [ 'Password' => 'Passwort', 'Remember Me' => 'Erinnere dich an mich', 'Username' => 'Benutzername', ];
i18n
component of application in the config/web.php
file:$config = [ 'id' => 'basic', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'components' => [ … 'i18n' => [ 'translations' => [ 'app*' => [ 'class' => 'yiii18nPhpMessageSource', 'sourceLanguage' => 'en-US', ], ], ], 'db' => require(__DIR__ . '/db.php'), ], 'params' => $params, ];
de
:$config = [ 'id' => 'basic', 'language' => 'de', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], ... ];
Then refresh the login page:
Yii2 provides the Yii::t()
method for translating interface messages via the i18n
component, which supports different types of sources. In this recipe, we use yiii18nhpMessageSource
, which stores translated messages in plain PHP files.
The framework does not have artificial intelligence and does not translate messages by itself. You must put prepared translations in files or in the database and framework to get the needed message from this message source.
You can set the current language manually:
$config = [ 'id' => 'basic', 'language' => 'de', ... ];
Instead of setting the language in the configuration file, you can switch the application language in runtime:
Yii::$app->language = 'fr';
For example, if you store the user language in the lang
field of the User
model, you can create the language loader:
<?php namespace appootstrap; use yiiaseBootstrapInterface; class LanguageBootstrap implements BootstrapInterface { public function bootstrap($app) { if (!$app->user->isGuest) { $app->language = $app->user->identity->lang; } } }
Register this class in the bootstrapping list:
$config = [ 'id' => 'basic', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log', 'app'bootstrapLanguageBoostrap'], ... ];
Now, every authenticated user will see the interface in their own language.
Also, you can override the yiiwebUrlManager
class for passing the current language as a GET parameter or as a prefix of a URL. Also, as an alternative you can store selected languages in browser cookies.
When you generate models and another code with Gii, you can check the following option:
All labels in the generated code will be embraced into the Yii::t()
calls.
We did not cover the translating of model content in this recipe. However, for example, you can store translated texts in separate tables (such as the post_lang
table for post model table) in a database and use the value of the Yii::$app->language
property to get the current language and extract needed content for your models by the value.
For more information about internationalization in Yii2, refer to http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html.
3.21.76.0