@@ -74,8 +74,15 @@ code, Odoo cannot automatically export translatable terms so they
74
74
must be marked explicitly for export. This is done by wrapping a literal
75
75
string in a function call.
76
76
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._ `:
78
79
80
+ .. code-block :: python
81
+
82
+ title = self .env._(" Bank Accounts" )
83
+
84
+ # old API for backward-compatibility
85
+ from odoo.tools import _
79
86
title = _(" Bank Accounts" )
80
87
81
88
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`:
90
97
variables. For situations where strings are formatted, this means the
91
98
format string must be marked, not the formatted string
92
99
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
95
103
at rendering and can be used to declare translatable properties in class methods
96
104
of global variables.
97
105
106
+ .. code-block :: python
107
+
108
+ from odoo.tools import LazyTranslate
109
+ _lt = LazyTranslate(__name__ )
110
+ LAZY_TEXT = _lt(" some text" )
111
+
98
112
.. note ::
99
113
100
114
Translations of a module are **not ** exposed to the front end by default and
@@ -115,6 +129,26 @@ of global variables.
115
129
modules = super()._get_translation_frontend_modules_name()
116
130
return modules + ['your_module']
117
131
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
+
118
152
Variables
119
153
---------
120
154
0 commit comments