The translation is a crucial feature in Odoo. It helps users manage things efficiently and treat customers in their language. Here in this blog, we will discuss how translations work in Odoo and provide translation abilities to our custom module.
All the translation files are put in the i18n folder of your module. There will be a main translation template and separate translation files for each language.
Let’s see how to export the translation. For that, activate the developer mode and go to Settings -> Translations -> Export Translation.
A wizard will open
Leave the language field to the default one (new language/empty template)
Select File Format as PO file
Select your module. The selected module's translatable terms will be exported. Multiple selections are possible.
Then click on the Export button.
A wizard will open like this, where you can download the file and place it into the i18n folder of the module.
The download file name will be your_module_name.pot. This file is a PO template that lists all the translatable strings of the module, and actual translation can be added. You can create PO files by copying the template to a new file with the name language_iso_code.po, and adding actual translation of the string concerning that language.
We didn’t select any specific language while exporting translation in the above case. But we can also select the language during exporting.
Here the dutch language is selected. All the activated language in your database is available for selection. Now click export and download the file. It will be the nl.po file, which is the translation file of the Dutch language. All translatable strings will be present in the file, and we can add actual translation in the Dutch language.
Similarly, we can export language-specific PO files. There should be an i18n folder in your module where you place all the PO files, and it will be loaded automatically when the corresponding languages are installed. All loaded languages' translations are installed or updated while installing or updating a module.
There will be two terms in PO files. msgid and msgstr.
Like below,
msgid "Allow the cash rounding management"
msgstr ""
msgid represents the actual string that needs to be translated, and the msgstr represents the corresponding translated string. We can add the corresponding translated strings to msgstr for all msgids.
All the translated terms and their status are listed under Settings -> Translations -> Translated Terms. You can also generate missing terms by going to the Settings -> Translations -> Generate Missing Terms.
Implicit Exports
a) Odoo implicitly exports the translatable string from the data contents.
b) From non-Qweb views, all the text nodes, including the content of the string, placeholder, sum, help, and confirm imported.
c) In Qweb templates, all the contents inside the t-translation=”off” block will be exported. In QWeb views, translation can be restricted using the t-translation=”off” block.
d) For Fields, the string, help, list of selections if the field is a selection field are exported. For translatable fields all of their existing values exported
e) Restrict translation for fields by specifying _translate = False
Class ModelName(models.Model):
_name = ‘new.model.name’
_description = ‘Model Description’
_translate = False
f) help/error messages of _constraints and _sql_constraints are exported
Explicit Imports
Some strings are not implicitly exported, so you have to mark them explicitly using the wrapper function.
In Python, the wrapping function in odoo._():
Raise UserError(_(“Raise Error”))
In JavaScript, the wrapping function in odoo.web._t():
var title = _t(“This date is on the future.”);
Please consider these points in the explicit translation
Set dynamic variables as parameters in translation lookup
a) raise UserError(_('No language with code "%s" exists') % lang_code)
b) Rather than splitting translation into multiple blocks, keep it in a single block.
c) Plural words may vary with languages, so do not use them in translation