Language Management
The need for multilingual websites has become more and more important as sites are welcoming visitors from all around the world more and more frequently.
The AVRO framework recognizes that need and supports several features aimed at making this not only possible but as easy as possible.
Configuring Supported Languages
The first step for building multilingual applications with AVRO is to decide which languages will be supported by the application and then to configure that list in the /application/config/application.ini configuration file.
For the HTTP mode, specify allowed languages as follows:
http_frontend_default_language=hy
http_backend_default_language=hy
http_frontend_allowed_languages=en|ru|fr
http_backend_allowed_languages=en|ru|fr
Allowed languages are defined with language prefixes and separated with the "|" character. In this example is allowed to run with English, Russian and French languages.
The full list of language definitions in the file /framework/config/languages.ini will be loaded by default into the application instance. However, if you don't need to support all these languages, it is more efficient to make this list as short as possible since it will improve the performance of the application both in terms of speed and memory consumption.
The list provided in /application/config/languages.ini overrides the one defined in the framework. This list is the list of languages returned by the call:
$this->app->allowed_languages()
For example, the contents of your languages.ini could be:
en=English (English)
fr=French (Français)
Organizing Translation Files
To display messages in various languages, you need to provide AVRO with the various translations of these messages in the supported languages. While this can be a long work depending on the number of messages in your application and the number of languages you want to support, the process is nevertheless quite easy.
You create a translation file per language and write the messages you need translated in that language. These translation files have a name in the form language_prefix.ini. So if you have a file called en.ini, you know it will be used to hold messages in English, while a file called it.ini will contain messages in Italian. Language_prefix is the language coe in ISO 639-1:2002 standard.
Several translation files are used by the framework, and these are located in various directories:
- The
/framework/languages/*.inifiles will hold translations for messages used by the framework itself. - The
/application/languages/*.inifiles hold messages that are global to your application. These will override messages defined in the framework. - Additionally, each addon may have its own separate language files, since you may find it more convenient to manage these messages aside from the global application messages. These files are located either in the
/framework/addons/addon_name/languages/directory or in the/application/addons/addon_name/languages/directory. They also go by the namelanguage_prefix.ini.
More often, the message is just whatever string is suitable to display. However, occasionally, you may need to supply some value that doesn't need to be translated and is not known until the page is generated dynamically, such as the name of a returning visitor. In this case, the message you put in the translation file will have some placeholders that will be replaced by the parameters you provide when you want to output that message.
For example, a translation file for English could be:
welcome_message=Welcome, %s!
Then, in your code, you can use:
$this->app->language('welcome_message', $username);
This will output:
Welcome, John!
If you use $this->language language file must be in /addons/<my_addon>/language/ directory.
$this->language('welcome_message', $username);
This approach allows you to manage translations efficiently and keep your application adaptable to multiple languages.