Whether you are building a website or a full-fledged web application, making it accessible to a wider audience often requires it to be available in different languages and locales. Fundamental differences between most human languages make this anything but easy. The differences in grammar rules, language nuances, date formats, and more combine to make localization a unique and formidable challenge. Having a multi-lingual website for your business would mean you are expressing yourself to be truly international and versatile. It will increase your business reach exponentially and provide a local feeling to individuals. Yii is one of the most prominently used PHP Framework for the developments of such interactive web applications and leverages multilingual capacity.

Yii 2 is the latest version of Yii Framework that requires PHP 5.4.0 or higher which makes use of latest features of PHP i.e. new PHP Namespaces to encapsulate objects and traits to reuse sets of methods in different classes, anonymous functions, SPL classes and interfaces and late static bindings. Yii 2 framework is available in two templates, first one is basic and the other is advanced. Usually, most of the projects will require only the basic template unless you really want to use an advanced template. The advanced template provides multiple tires of application with individual application files sharing some common configuration. Basically, the advanced template will help to create multiple application with sharing some common config such as DB configuration.

Yii 2 Advanced Project Template is a skeleton Yii 2 application leading for developing complex Web applications using multiple tiers. The template includes three tiers: frontend, backend, and console, each of which is a separate Yii application. It supports deploying the application in different environments.

Following are the steps of implementation of Multilanguage functionality into Yii2 Advanced Application Template.

In order to enable the Multilanguage feature, you first have to create messages directory inside the common/backend/frontend/api directory.

Set default language into common/config/main.php by adding following line.

How To Implement Multi Language Functionality Into Yii2 Advanced Application Template

The Yii::t() method will call the i18n application component translate method to perform the actual translation work. For that set an i18n object into components array of common/config/main.php.

How To Implement Multi Language Functionality Into Yii2 Advanced Application Template

The component can be configured in the application configuration as follows,

Now, add messages/ge-GE/app.php file to your common/backend/frontend/api directory to get translated strings. Add following content into it.

How To Implement Multi Language Functionality Into Yii2 Advanced Application Template

We need to add all the translations of default language strings here.

Now, if frontend, backend, and API controllers directly extend yii\web\Controller, Create new BaseController.php and add following code into init() of it. If not, add following code into init() of parent controller.

How To Implement Multi Language Functionality Into Yii2 Advanced Application Template

Add following code at the starting of your header into view or controller of it.

How To Implement Multi Language Functionality Into Yii2 Advanced Application Template

Now, add a drop-down with anchor tag into header view file to change the language of your application.

How To Implement Multi Language Functionality Into Yii2 Advanced Application Template

In your frontend, backend and API code, use the following method to create i18n supported strings respectively.

How To Implement Multi Language Functionality Into Yii2 Advanced Application Template

PHP framework has come up with various up-gradations that grant some exceptional features to Yii 2 over Yii 1.1. It is easy to install, utilizes modern technologies, is highly extensible, simplifies security, gives better performance and reduces the development time. Yii framework assists in creating modern web applications quickly and easily. Yii becomes even more popular all around the world with each day proving remarkable results with each completed project. This striking software makes the process of web development faster and easier.