summaryrefslogtreecommitdiff
path: root/odoo/tools/translate.py
diff options
context:
space:
mode:
authorMartin Trigaux <mat@odoo.com>2019-02-18 15:24:00 +0000
committerMartin Trigaux <mat@odoo.com>2019-08-01 12:39:56 +0000
commitf4319580e576e033b95db1627dd11f126c187323 (patch)
treeca4bed47ea8bef3d4be415efe60b8235bb338ab1 /odoo/tools/translate.py
parent0fc9a97d20a480a10dc5258dcc22bf5e52b48293 (diff)
[ADD] tools: lazy translation method
Introduces the method _lt The translation method is now evaluated lazily. It allows to declare global variables with translatable content e.g. this code will now work: LABEL = _lt("User") def _compute_label(self): context = {'lang': self.partner_id.lang} self.user_label = LABEL
Diffstat (limited to 'odoo/tools/translate.py')
-rw-r--r--odoo/tools/translate.py49
1 files changed, 48 insertions, 1 deletions
diff --git a/odoo/tools/translate.py b/odoo/tools/translate.py
index 56936cc6241..36a350ddaaf 100644
--- a/odoo/tools/translate.py
+++ b/odoo/tools/translate.py
@@ -2,6 +2,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import codecs
import fnmatch
+import functools
import inspect
import io
import locale
@@ -444,6 +445,9 @@ class GettextAlias(object):
return lang
def __call__(self, source):
+ return self._get_translation(source)
+
+ def _get_translation(self, source):
res = source
cr = None
is_new_cr = False
@@ -454,6 +458,9 @@ class GettextAlias(object):
frame = frame.f_back
if not frame:
return source
+ frame = frame.f_back
+ if not frame:
+ return source
lang = self._get_lang(frame)
if lang:
cr, is_new_cr = self._get_cr(frame)
@@ -473,6 +480,45 @@ class GettextAlias(object):
cr.close()
return res
+
+@functools.total_ordering
+class _lt:
+ """ Lazy code translation
+
+ Similar to GettextAlias but the translation lookup will be done only at
+ __str__ execution.
+
+ A code using translated global variables such as:
+
+ LABEL = _lt("User")
+
+ def _compute_label(self):
+ context = {'lang': self.partner_id.lang}
+ self.user_label = LABEL
+
+ works as expected (unlike the classic GettextAlias implementation).
+ """
+
+ __slots__ = ['_source']
+ def __init__(self, source):
+ self._source = source
+
+ def __str__(self):
+ # Call _._get_translation() like _() does, so that we have the same number
+ # of stack frames calling _get_translation()
+ return _._get_translation(self._source)
+
+ def __eq__(self, other):
+ """ Prevent using equal operators
+
+ Prevent direct comparisons with ``self``.
+ One should compare the translation of ``self._source`` as ``str(self) == X``.
+ """
+ raise NotImplementedError()
+
+ def __lt__(self, other):
+ raise NotImplementedError()
+
_ = GettextAlias()
@@ -974,7 +1020,8 @@ def trans_generate(lang, modules, cr):
_logger.debug("Scanning files of modules at %s", path)
for root, dummy, files in walksymlinks(path):
for fname in fnmatch.filter(files, '*.py'):
- babel_extract_terms(fname, path, root)
+ babel_extract_terms(fname, path, root,
+ extract_keywords={'_': None, '_lt': None})
# Javascript source files in the static/src/js directory, rest is ignored (libs)
if fnmatch.fnmatch(root, '*/static/src/js*'):
for fname in fnmatch.filter(files, '*.js'):