Use gettext tools to ease translations

The PO file format is part of the gettext internationalization and localization system commonly used in Unix-like systems. This system includes tools to ease the translation work.

This recipe demonstrates how to use these tools to help translate our addon modules. We want to use it on a custom module, so my_module created in Chapter 3, Creating Odoo Modules is a good candidate. But feel free to replace it with some other custom module you have at hand, replacing the recipe's my_module references as appropriate.

How to do it...

To manage the translation from the command line, assuming that your Odoo installation is at ~/odoo-work/odoo, follow these steps:

  1. Create a compendium of translation terms for the target language, for example, Spanish. If we name our compendium file odoo_es.po we should write the following code:
    $ cd ~/odoo-work/odoo  # Use the path to your Odoo installation
    $ find ./ -name es_ES.po | xargs msgcat --use-first | msgattrib --translated  --no-fuzzy -o ./odoo_es.po
    
  2. Export from the Odoo command line interface the translation template file for the addon module and place it in the module's expected location:
    $ ./odoo.py -d mydb --i18n-export=my_module.po --modules=my_module
    $ mv my_module.po ./addons/my_module/i18n/my_module.pot
    
  3. If no translation file is available yet for the target language, create the PO translation file, reusing the terms already found translated in the compendium:
    $ msgmerge --compendium ./odoo_es.po 
     -o ./addons/my_module/i18n/es_ES.po 
     /dev/null ./addons/my_module/i18n/my_module.pot
    
  4. If a translation file exists, add the translations that can be found in the compendium:
    $ mv ./addons/my_module/i18n/es_ES.po /tmp/my_module_es_old.po
    $ msgmerge --compendium ./odoo_es.po 
    -o./addons/my_module/i18n/es_ES.po   
    /tmp/my_module_es_old.po ./addons/my_module/i18n/my_module.pot
    $ rm /tmp/my_module_es_old.po
    
  5. To have a peek at the untranslated terms in a PO file:
    $ msgattrib --untranslated ./addons/my_module/i18n/es_ES.po
    
  6. Use your favorite editor to complete the translation.

How it works...

Step 1 of the preceding section calls odoo.py with the --i18n-export option. You need to specify a database on the command line, even if one is specified in the configuration file and the --modules option, with a comma separated list of modules to export the translation for.

Step 2 uses commands from the gettext toolbox to create a translation compendium for the chosen language, Spanish in our case. It works by finding all the es_ES.po files in the Odoo code base, and passing them to the msgcat command. We use the --use-first flag to avoid conflicting translations (there are a few in the Odoo code base). The result is passed to the msgattrib filter. We use the --translated option to filter out the untranslated entries and the --no-fuzzy option to remove the fuzzy translations. We then save the result in odoo_es.po.

Note

In the gettext world, fuzzy translations are those created automatically by the msgmerge command (or other tools) by using a proximity match on the source string. We want to avoid these in the compendium.

Step 3 creates a translation file using the translated values for texts already found in the compendium. The msgmerge command is used with the --compendium option to find in the compendium files the msgid lines matching those in the translation template file generated in Step 1. The result is saved in the es_ES.po file.

If you have a pre-existing .po file for your addon with translations you would like to preserve, then you should rename it and replace the /dev/null argument with this file. The rename is required to avoid using the same file both for input and output.

There's more...

This recipe only skims the rich tools of the GNU gettext toolbox. Full coverage is well beyond the scope of this book. If you are interested, the GNU gettext documentation contains a wealth of precious information about PO file manipulation, and is available at http://www.gnu.org/software/gettext/manual/gettext.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.10.246