Skip to content

Commit c6208a5

Browse files
committed
[IMP] core: Environment._ for translations
odoo/odoo#174844 closes #10421 Related: odoo/enterprise#67528 Signed-off-by: Krzysztof Magusiak (krma) <krma@odoo.com>
1 parent 16c6c0f commit c6208a5

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

content/contributing/development/coding_guidelines.rst

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -725,14 +725,10 @@ they can and will be removed !
725725
Use translation method correctly
726726
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
727727

728-
Odoo uses a GetText-like method named "underscore" ``_( )`` to indicate that
729-
a static string used in the code needs to be translated at runtime using the
730-
language of the context. This pseudo-method is accessed within your code by
731-
importing as follows:
732-
733-
.. code-block:: python
734-
735-
from odoo import _
728+
Odoo uses a GetText-like method named "underscore" ``_()`` to indicate that
729+
a static string used in the code needs to be translated at runtime.
730+
That method is available at ``self.env._`` using the language of the
731+
environment.
736732

737733
A few very important rules must be followed when using it, in order for it to
738734
work and to avoid filling the translations with useless junk.
@@ -744,10 +740,12 @@ field.
744740

745741
The method accepts optional positional or named parameter
746742
The rule is very simple: calls to the underscore method should always be in
747-
the form ``_('literal string')`` and nothing else:
743+
the form ``self.env._('literal string')`` and nothing else:
748744

749745
.. code-block:: python
750746
747+
_ = self.env._
748+
751749
# good: plain strings
752750
error = _('This record is locked!')
753751

content/developer/howtos/translations.rst

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,15 @@ code, Odoo cannot automatically export translatable terms so they
7474
must be marked explicitly for export. This is done by wrapping a literal
7575
string in a function call.
7676

77-
In Python, the wrapping function is :func:`odoo._`::
77+
In Python, the wrapping function is :func:`odoo.api.Environment._`
78+
and :func:`odoo.tools.translate._`:
7879

80+
.. code-block:: python
81+
82+
title = self.env._("Bank Accounts")
83+
84+
# old API for backward-compatibility
85+
from odoo.tools import _
7986
title = _("Bank Accounts")
8087
8188
In JavaScript, the wrapping function is generally :js:func:`odoo.web._t`:
@@ -90,11 +97,18 @@ In JavaScript, the wrapping function is generally :js:func:`odoo.web._t`:
9097
variables. For situations where strings are formatted, this means the
9198
format string must be marked, not the formatted string
9299

93-
The lazy version of `_` and `_t` is :func:`odoo._lt` in python and
94-
:js:func:`odoo.web._lt` in javascript. The translation lookup is executed only
100+
The lazy version of `_` and `_t` is the :class:`odoo.tools.translate.LazyTranslate`
101+
factory in python and :js:func:`odoo.web._lt` in javascript.
102+
The translation lookup is executed only
95103
at rendering and can be used to declare translatable properties in class methods
96104
of global variables.
97105

106+
.. code-block:: python
107+
108+
from odoo.tools import LazyTranslate
109+
_lt = LazyTranslate(__name__)
110+
LAZY_TEXT = _lt("some text")
111+
98112
.. note::
99113

100114
Translations of a module are **not** exposed to the front end by default and
@@ -115,6 +129,26 @@ of global variables.
115129
modules = super()._get_translation_frontend_modules_name()
116130
return modules + ['your_module']
117131

132+
Context
133+
-------
134+
135+
To translate, the translation function needs to know the *language* and the
136+
*module* name. When using ``Environment._`` the language is known and you
137+
may pass the module name as a parameter, otherwise it's extracted from the
138+
caller.
139+
140+
In case of ``odoo.tools.translate._``, the language and the module are
141+
extracted from the context. For this, we inspect the caller's local variables.
142+
The drawback of this method is that it is error-prone: we try to find the
143+
context variable or ``self.env``, however these may not exist if you use
144+
translations outside of model methods; i.e. it does not work inside regular
145+
functions or python comprehensions.
146+
147+
Lazy translations are bound to the module during their creation and the
148+
language is resolved when evaluating using ``str()``.
149+
Note that you can also pass a lazy translation to ``Envionrment._``
150+
to translate it without any magic language resolution.
151+
118152
Variables
119153
---------
120154

content/developer/reference/backend/orm/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Odoo version 18.0
1212
- New methods to check access rights and rules now combine both access rights
1313
and rules: `check_access`, `has_access` and `_filtered_access`.
1414
See `#179148 <https://github.com/odoo/odoo/pull/179148>`_.
15+
- Translations are made available from the `Environment` with `#174844 <https://github.com/odoo/odoo/pull/174844>`_.
1516

1617

1718
Odoo Online version 17.4

0 commit comments

Comments
 (0)