Internationalization

Each module can provide its own translations within the i18n directory, by having files named LANG.po where LANG is the locale code for the language, or the language and country combination when they differ (e.g. pt.po or pt_BR.po). Translations will be loaded automatically by Odoo for all enabled languages. Developers always use English when creating a module, then export the module terms using Odoo's gettext POT export feature (Settings ‣ Translations ‣ Import/Export ‣ Export Translationwithout specifying a language), to create the module template POT file, and then derive the translated PO files. Many IDE's have plugins or modes for editing and merging PO/POT files.

Tip

The Portable Object files generated by Odoo are published onTransifex, making it easy to translate the software.

|- idea/ # The module directory
   |- i18n/ # Translation files
      | - idea.pot # Translation Template (exported from Odoo)
      | - fr.po # French translation
      | - pt_BR.po # Brazilian Portuguese translation
      | (...)

Tip

By default Odoo's POT export only extracts labels inside XML files or inside field definitions in Python code, but any Python string can be translated this way by surrounding it with the functionodoo._()(e.g._("Label"))

Exercise

Translate a module

Choose a second language for your Odoo installation. Translate your module using the facilities provided by Odoo.

  1. Create a directory openacademy/i18n/
  2. Install whichever language you want (Administration ‣ Translations ‣ Load an Official Translation)
  3. Synchronize translatable terms (Administration ‣ Translations ‣ Application Terms ‣ Synchronize Translations)
  4. Create a template translation file by exporting (Administration ‣ Translations -> Import/Export ‣ Export Translation) without specifying a language, save inopenacademy/i18n/
  5. Create a translation file by exporting (Administration ‣ Translations ‣ Import/Export ‣ Export Translation) and specifying a language. Save it in openacademy/i18n/
  6. Open the exported translation file (with a basic text editor or a dedicated PO-file editor e.g. POEdit and translate the missing terms
  7. In models.py, add an import statement for the functionodoo._and mark missing strings as translatable
  8. Repeat steps 3-6

openacademy/models.py

# -*- coding: utf-8 -*-

from datetime import timedelta
from odoo import models, fields, api, exceptions, _

class Course(models.Model):
    _name = 'openacademy.course'
        default = dict(default or {})

        copied_count = self.search_count(
            [('name', '=like', _(u"Copy of {}%").format(self.name))])
        if not copied_count:
            new_name = _(u"Copy of {}").format(self.name)
        else:
            new_name = _(u"Copy of {} ({})").format(self.name, copied_count)

        default['name'] = new_name
        return super(Course, self).copy(default)
        if self.seats < 0:
            return {
                'warning': {
                    'title': _("Incorrect 'seats' value"),
                    'message': _("The number of available seats may not be negative"),
                },
            }
        if self.seats < len(self.attendee_ids):
            return {
                'warning': {
                    'title': _("Too many attendees"),
                    'message': _("Increase seats or remove excess attendees"),
                },
            }
    def _check_instructor_not_in_attendees(self):
        for r in self:
            if r.instructor_id and r.instructor_id in r.attendee_ids:
                raise exceptions.ValidationError(_("A session's instructor can't be an attendee"))

results matching ""

    No results matching ""