Skip to content
Snippets Groups Projects
README.rst 3.35 KiB

ICU Format

Coverage report pylint score Alpha License: AGPL-3 Black Prettier

Provides methods that encapsulates PyICU.

ICU provides handling of plural and many other things that are not present in the gettext implementation of Odoo.

This module provides icu_format that can be called from anywhere:

from odoo.addons.icuformat import icu_format

icu_format("en", _("A message: {msg}"), {"msg": self.id})

There is also one that is linked to models and extract the language to use from the context, defaulting to the current user lang:

self.icu_format(_("A message: {msg}"), {"msg": self.id})

The icu_format methods accepts various types in its dictionary, but some of them are converted to avoid errors in ICU:

  • dates are converted to datetime.
  • bool are converted to strings.

The module also add icu_format_translate to the list of method used in babel to generate the module catalogs.

It also adds TRANSLATORS: as a tag used to comment translated strings.

Example:

# TRANSLATORS: b is a boolean, either True or False
self.icu_format_translate("{b,select,True{This is true.}other{This is false.}}", {"b": value}))

ICU provides a way to format a list of elements, it is also exposed:

self.icu_list_format(["a", "b", "c"])
from odoo.addons.icuformat import icu_list_format

icu_list_format("en_US", self.ids)

There is also a currency formatter helper:

self.env.ref("base.EUR").icu_format(
    _("Your total is {total, number, currency}"), {"total": 1.25}
)
self.currency_id.icu_format_translate(
    "Your total is {total, number, currency}", {"total": 1.25}
)

There is also a logger to use ICU in log messages: