diff options
| author | Martin Trigaux <mat@odoo.com> | 2019-08-02 06:55:55 +0000 |
|---|---|---|
| committer | Martin Trigaux <mat@odoo.com> | 2019-08-02 06:57:01 +0000 |
| commit | 294a857f4f95c4d2fa75e7516a2e386dea2c6548 (patch) | |
| tree | a27481d161a74bbc768b96788c2b86abb33df45f | |
| parent | f17f2e8336d3ad986d09a43455b1a9b01d0ff2f3 (diff) | |
[IMP] doc: add reference to _lt method
Was added at odoo/odoo#31211
closes odoo/odoo#35393
Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
| -rw-r--r-- | doc/reference/translations.rst | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/doc/reference/translations.rst b/doc/reference/translations.rst index 2acdaf48f67..23641f3b895 100644 --- a/doc/reference/translations.rst +++ b/doc/reference/translations.rst @@ -88,6 +88,10 @@ In JavaScript, the wrapping function is generally :js:func:`odoo.web._t`: variables. For situations where strings are formatted, this means the format string must be marked, not the formatted string +The lazy version of `_` and `_t` is :func:`odoo._lt` in python and +:js:func:`odoo.web._lt` in javascript. The translation lookup is executed only +at rendering and can be used to declare translatable properties in class methods +of global variables. Variables ^^^^^^^^^ @@ -144,8 +148,8 @@ Read vs Run Time ERROR_MESSAGE = { # bad, evaluated at server launch with no user language - access_error: _('Access Error'), - missing_error: _('Missing Record'), + 'access_error': _('Access Error'), + 'missing_error': _('Missing Record'), } class Record(models.Model): @@ -163,10 +167,25 @@ Read vs Run Time missing_error: _t('Missing Record'), }; -**Do** evaluate dynamically the translatable content:: + +**Do** use lazy translation lookup method:: + + ERROR_MESSAGE = { + 'access_error': _lt('Access Error'), + 'missing_error': _lt('Missing Record'), + } + + class Record(models.Model): + + def _raise_error(self, code): + # translation lookup executed at error rendering + raise UserError(ERROR_MESSAGE[code]) + + +or **do** evaluate dynamically the translatable content:: # good, evaluated at run time - def _get_error_message(): + def _get_error_message(self): return { access_error: _('Access Error'), missing_error: _('Missing Record'), |
