Internationalization is the process of designing a software application to support various languages.
Internationalization (I18N) is very basic requirement of any application because the potential users may be worldwide so we should add internationalization in an application as soon as we start the development.
Before applying Internationalization, one should know about what is Locale.
Locale is a set of parameters that defines the user’s language and region/country. It is usually identified by an ID consisting of a language ID and a region ID or we can say country code in many cases. For example: “en-US” stands for the locale “english-united states”, “hi-IN” stands for “hindi-india”.
As we can see, a locale format looks like <ll>-<cc>. where ll is two or three letter lowercase language code and cc is a two letter country code. You can find out ISO language codes here
Yii provides built-in I18N features to add internationalization in an application. Here i am listing steps to set up internationalization in Yii2-advanced.
Step 1:Configure message source
add the i18n component in common/config/main.php
'i18n' => [ 'translations' => [ 'app*' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@common/messages', 'fileMap' => [ 'app' => 'app.php', 'app/error' => 'error.php', ], ], ], ],
Here basePath is “message source file directory path” where all message source file will resides. There will be a separate message source file for every language that our application will use.
Note* @common is set by default in common/config/bootstrap.php when we initialize yii project. If you didn’t found this in common/config/bootstrap.php or want to configure different message source directory, you can set this in common/config/bootstrap.php by following code:
Step 2: Create message source file
Now we have to create message source file for every language that our application will use. Here i am creating two source files, one for globally used language English and one source file for Hindi.
create message directory
mkdir -p common/messages/en-Us
mkdir -p common/messages/hi
Note* -p option is to created nested directory at once. Now our directory structure will looks like:
common - messages -- en-Us -- hi
Create language source file
Create a language source file “app.php” in common/messages/en-US
add following code in common/messages/en-US/app.php
<?php return [ 'Sample Text' => 'Sample Text in English' ]; ?>
Repeat same steps for hindi language. Lets quickly create a language source file “app.php” in common/messages/hi
add following code in common/messages/hi/app.php
<?php return [ 'Sample Text' => 'Sample Text in Hindi' ]; ?>
Step 3: Message translation
To access language variable from source file, we can use following Yii syntax.
yii::t('app', 'Sample Text')
Here first parameter ‘app’ refers to message source file name, “app.php” file in our case. and second parameter is the key in app.php to access respective translated message.
Now the question is that how we can set which language file will be used to get translated message or we can say how to set target language. Here is yii syntax to change target language used in application:
\Yii::$app->language = 'hi'; //Here 'hi' is language code for Hindi
Note* Default target language in yii is ‘en-US’. That means if we didn’t set target language, app.php from en-US will be called. Also we should set target language before yii::t() call.
That’s all what we need to setup basic internationalization in yii2-advanced. If you want to translate messages with parameters, you can visit https://www.yiiframework.com/doc/guide/2.0/en/tutorial-i18n