Setting up Yii2 for internationalization

I’m building some software and want to implement internationalization into it and have been reading the docs in Yii2 on how to do it, but my question is…

Is it suggested to build your first language files along with it when you do each piece of text? …or I guess you could just do a search files at a later stage to find all instances of code that needs translation and then make up your language file.

I only ask because I’ve never done anything like this before and the source language / locale we are using is ‘en-US’; I guess I could do up a language file for ‘en-GB’ along the way which will make getting translations done for other languages much easier.

…or do people generally just make a language file for the source language anyway such as languages files for ‘en-US’?

I thought this would be a pretty easy to answer question - no one else has set up internationalization before?

I did the latter. I wanted to focus on the project functionality/logic first and then worry about translations later. However I missed several phrases that should’ve been translated but weren’t.

Luckily, time and user contributions helped me find others. It’s up to you decide which is more important for your project.

Either way, it’s a pain in the ass - Tedious and boring.

Thanks for the response. Thus far I have just decided to go the former route, so I am making up "en-GB" language files.

It is recommended to use English for the messages in code, you do NOT need translation files for these because they will not change from English to English. You can run a Yii command to extract all the messages once you have finished the site. It will search for Yii::t()

Open a (windows) console go to the root of the site and type following command:

yii message/extract @common/config/i18n.php

1 Like

Awesome. Thanks for the info!

Ok… how do people usually go about translating the mail templates/layouts?

Like for example I have my mail templates in:





Sure you could just chuck in your translations in here, but to make it easier for the user that may want to edit the contents surely they are best separated by locale?

I guess you could do something like the below during the bootstrap process?

Yii::$app->mailer->viewPath .= '/' . Yii::$app->language;