# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import codecs
import fnmatch
import functools
import inspect
import io
import itertools
import json
import locale
import logging
import os
from tokenize import generate_tokens, STRING, NEWLINE, INDENT, DEDENT
import polib
import re
import tarfile
import threading
import warnings
from collections import defaultdict, namedtuple
from contextlib import suppress
from datetime import datetime
from os.path import join
from pathlib import Path
from babel.messages import extract
from lxml import etree, html
from markupsafe import escape, Markup
from psycopg2.extras import Json
import odoo
from odoo.exceptions import UserError
from . import config, pycompat
from .misc import file_open, file_path, get_iso_codes, SKIPPED_ELEMENT_TYPES
_logger = logging.getLogger(__name__)
PYTHON_TRANSLATION_COMMENT = 'odoo-python'
# translation used for javascript code in web client
JAVASCRIPT_TRANSLATION_COMMENT = 'odoo-javascript'
# used to notify web client that these translations should be loaded in the UI
# deprecated comment since Odoo 16.0
WEB_TRANSLATION_COMMENT = "openerp-web"
SKIPPED_ELEMENTS = ('script', 'style', 'title')
_LOCALE2WIN32 = {
'af_ZA': 'Afrikaans_South Africa',
'sq_AL': 'Albanian_Albania',
'ar_SA': 'Arabic_Saudi Arabia',
'eu_ES': 'Basque_Spain',
'be_BY': 'Belarusian_Belarus',
'bs_BA': 'Bosnian_Bosnia and Herzegovina',
'bg_BG': 'Bulgarian_Bulgaria',
'ca_ES': 'Catalan_Spain',
'hr_HR': 'Croatian_Croatia',
'zh_CN': 'Chinese_China',
'zh_TW': 'Chinese_Taiwan',
'cs_CZ': 'Czech_Czech Republic',
'da_DK': 'Danish_Denmark',
'nl_NL': 'Dutch_Netherlands',
'et_EE': 'Estonian_Estonia',
'fa_IR': 'Farsi_Iran',
'ph_PH': 'Filipino_Philippines',
'fi_FI': 'Finnish_Finland',
'fr_FR': 'French_France',
'fr_BE': 'French_France',
'fr_CH': 'French_France',
'fr_CA': 'French_France',
'ga': 'Scottish Gaelic',
'gl_ES': 'Galician_Spain',
'ka_GE': 'Georgian_Georgia',
'de_DE': 'German_Germany',
'el_GR': 'Greek_Greece',
'gu': 'Gujarati_India',
'he_IL': 'Hebrew_Israel',
'hi_IN': 'Hindi',
'hu': 'Hungarian_Hungary',
'is_IS': 'Icelandic_Iceland',
'id_ID': 'Indonesian_Indonesia',
'it_IT': 'Italian_Italy',
'ja_JP': 'Japanese_Japan',
'kn_IN': 'Kannada',
'km_KH': 'Khmer',
'ko_KR': 'Korean_Korea',
'lo_LA': 'Lao_Laos',
'lt_LT': 'Lithuanian_Lithuania',
'lat': 'Latvian_Latvia',
'ml_IN': 'Malayalam_India',
'mi_NZ': 'Maori',
'mn': 'Cyrillic_Mongolian',
'no_NO': 'Norwegian_Norway',
'nn_NO': 'Norwegian-Nynorsk_Norway',
'pl': 'Polish_Poland',
'pt_PT': 'Portuguese_Portugal',
'pt_BR': 'Portuguese_Brazil',
'ro_RO': 'Romanian_Romania',
'ru_RU': 'Russian_Russia',
'sr_CS': 'Serbian (Cyrillic)_Serbia and Montenegro',
'sk_SK': 'Slovak_Slovakia',
'sl_SI': 'Slovenian_Slovenia',
#should find more specific locales for Spanish countries,
#but better than nothing
'es_AR': 'Spanish_Spain',
'es_BO': 'Spanish_Spain',
'es_CL': 'Spanish_Spain',
'es_CO': 'Spanish_Spain',
'es_CR': 'Spanish_Spain',
'es_DO': 'Spanish_Spain',
'es_EC': 'Spanish_Spain',
'es_ES': 'Spanish_Spain',
'es_GT': 'Spanish_Spain',
'es_HN': 'Spanish_Spain',
'es_MX': 'Spanish_Spain',
'es_NI': 'Spanish_Spain',
'es_PA': 'Spanish_Spain',
'es_PE': 'Spanish_Spain',
'es_PR': 'Spanish_Spain',
'es_PY': 'Spanish_Spain',
'es_SV': 'Spanish_Spain',
'es_UY': 'Spanish_Spain',
'es_VE': 'Spanish_Spain',
'sv_SE': 'Swedish_Sweden',
'ta_IN': 'English_Australia',
'th_TH': 'Thai_Thailand',
'tr_TR': 'Turkish_Turkey',
'uk_UA': 'Ukrainian_Ukraine',
'vi_VN': 'Vietnamese_Viet Nam',
'tlh_TLH': 'Klingon',
}
# these direct uses of CSV are ok.
import csv # pylint: disable=deprecated-module
class UNIX_LINE_TERMINATOR(csv.excel):
lineterminator = '\n'
csv.register_dialect("UNIX", UNIX_LINE_TERMINATOR)
# FIXME: holy shit this whole thing needs to be cleaned up hard it's a mess
def encode(s):
assert isinstance(s, str)
return s
# which elements are translated inline
TRANSLATED_ELEMENTS = {
'abbr', 'b', 'bdi', 'bdo', 'br', 'cite', 'code', 'data', 'del', 'dfn', 'em',
'font', 'i', 'ins', 'kbd', 'keygen', 'mark', 'math', 'meter', 'output',
'progress', 'q', 'ruby', 's', 'samp', 'small', 'span', 'strong', 'sub',
'sup', 'time', 'u', 'var', 'wbr', 'text', 'select', 'option',
}
# Which attributes must be translated. This is a dict, where the value indicates
# a condition for a node to have the attribute translatable.
TRANSLATED_ATTRS = dict.fromkeys({
'string', 'add-label', 'help', 'sum', 'avg', 'confirm', 'placeholder', 'alt', 'title', 'aria-label',
'aria-keyshortcuts', 'aria-placeholder', 'aria-roledescription', 'aria-valuetext',
'value_label', 'data-tooltip', 'data-editor-message', 'label',
}, lambda e: True)
def translate_attrib_value(node):
# check if the value attribute of a node must be translated
classes = node.attrib.get('class', '').split(' ')
return (
(node.tag == 'input' and node.attrib.get('type', 'text') == 'text')
and 'datetimepicker-input' not in classes
or (node.tag == 'input' and node.attrib.get('type') == 'hidden')
and 'o_translatable_input_hidden' in classes
)
TRANSLATED_ATTRS.update(
value=translate_attrib_value,
text=lambda e: (e.tag == 'field' and e.attrib.get('widget', '') == 'url'),
**{f't-attf-{attr}': cond for attr, cond in TRANSLATED_ATTRS.items()},
)
avoid_pattern = re.compile(r"\s* element
div = etree.Element('div')
div.text = (node[pos-1].tail if pos else node.text) or ''
while pos < len(node) and translatable(node[pos]):
div.append(node[pos])
# translate the content of the
element as a whole
content = serialize(div)[5:-6]
original = content.strip()
translated = callback(original)
if translated:
result = content.replace(original, translated)
# is used to auto fix crapy result
result_elem = parse_html(f"
{result}
")
# change the tag to which is one of TRANSLATED_ELEMENTS
# so that 'result_elem' can be checked by translatable and hastext
result_elem.tag = 'span'
if translatable(result_elem) and hastext(result_elem):
div = result_elem
if pos:
node[pos-1].tail = div.text
else:
node.text = div.text
# move the content of the
element back inside node
while len(div) > 0:
node.insert(pos, div[0])
pos += 1
if pos >= len(node):
break
# node[pos] is not translatable as a whole, process it recursively
process(node[pos])
pos += 1
# translate the attributes of the node
for key, val in node.attrib.items():
if nonspace(val) and key in TRANSLATED_ATTRS and TRANSLATED_ATTRS[key](node):
node.set(key, callback(val.strip()) or val)
process(node)
return node
def parse_xml(text):
return etree.fromstring(text)
def serialize_xml(node):
return etree.tostring(node, method='xml', encoding='unicode')
MODIFIER_ATTRS = {"invisible", "readonly", "required", "column_invisible", "attrs", "states"}
def xml_term_adapter(term_en):
"""
Returns an `adapter(term)` function that will ensure the modifiers are copied
from the base `term_en` to the translated `term` when the XML structure of
both terms match. `term_en` and any input `term` to the adapter must be valid
XML terms. Using the adapter only makes sense if `term_en` contains some tags
from TRANSLATED_ELEMENTS.
"""
orig_node = parse_xml(f"
{term_en}
")
def same_struct_iter(left, right):
if left.tag != right.tag:
raise ValueError("Non matching struct")
yield left, right
left_iter = left.iterchildren()
right_iter = right.iterchildren()
for lc, rc in zip(left_iter, right_iter):
yield from same_struct_iter(lc, rc)
if next(left_iter, None) is not None or next(right_iter, None) is not None:
raise ValueError("Non matching struct")
def adapter(term):
new_node = parse_xml(f"
{term}
")
try:
for orig_n, new_n in same_struct_iter(orig_node, new_node):
removed_attrs = [k for k in new_n.attrib if k in MODIFIER_ATTRS and k not in orig_n.attrib]
for k in removed_attrs:
del new_n.attrib[k]
keep_attrs = {k: v for k, v in orig_n.attrib.items() if k in MODIFIER_ATTRS}
new_n.attrib.update(keep_attrs)
except ValueError: # non-matching structure
return term
# remove tags
and
from result
return serialize_xml(new_node)[5:-6]
return adapter
_HTML_PARSER = etree.HTMLParser(encoding='utf8')
def parse_html(text):
try:
parse = html.fragment_fromstring(text, parser=_HTML_PARSER)
except TypeError as e:
raise UserError(_("Error while parsing view:\n\n%s") % e) from e
return parse
def serialize_html(node):
return etree.tostring(node, method='html', encoding='unicode')
def xml_translate(callback, value):
""" Translate an XML value (string), using `callback` for translating text
appearing in `value`.
"""
if not value:
return value
try:
root = parse_xml(value)
result = translate_xml_node(root, callback, parse_xml, serialize_xml)
return serialize_xml(result)
except etree.ParseError:
# fallback for translated terms: use an HTML parser and wrap the term
root = parse_html(u"
from result
return serialize_xml(result)[5:-6]
def xml_term_converter(value):
""" Convert the HTML fragment ``value`` to XML if necessary
"""
# wrap value inside a div and parse it as HTML
div = f"
{value}
"
root = etree.fromstring(div, etree.HTMLParser())
# root is html > body > div
# serialize div as XML and discard surrounding tags
return etree.tostring(root[0][0], encoding='unicode')[5:-6]
def html_translate(callback, value):
""" Translate an HTML value (string), using `callback` for translating text
appearing in `value`.
"""
if not value:
return value
try:
# value may be some HTML fragment, wrap it into a div
root = parse_html("
from result
value = serialize_html(result)[5:-6]
except ValueError:
_logger.exception("Cannot translate malformed HTML, using source value instead")
return value
def html_term_converter(value):
""" Convert the HTML fragment ``value`` to XML if necessary
"""
# wrap value inside a div and parse it as HTML
div = f"
{value}
"
root = etree.fromstring(div, etree.HTMLParser())
# root is html > body > div
# serialize div as HTML and discard surrounding tags
return etree.tostring(root[0][0], encoding='unicode', method='html')[5:-6]
def get_text_content(term):
""" Return the textual content of the given term. """
content = html.fromstring(term).text_content()
return " ".join(content.split())
def is_text(term):
""" Return whether the term has only text. """
return len(html.fromstring(f"<_>{term}")) == 0
xml_translate.get_text_content = get_text_content
html_translate.get_text_content = get_text_content
xml_translate.term_converter = xml_term_converter
html_translate.term_converter = html_term_converter
xml_translate.is_text = is_text
html_translate.is_text = is_text
xml_translate.term_adapter = xml_term_adapter
def translate_sql_constraint(cr, key, lang):
cr.execute("""
SELECT COALESCE(c.message->>%s, c.message->>'en_US') as message
FROM ir_model_constraint c
WHERE name=%s and type='u'
""", (lang, key))
return cr.fetchone()[0]
class GettextAlias(object):
def _get_db(self):
# find current DB based on thread/worker db name (see netsvc)
db_name = getattr(threading.current_thread(), 'dbname', None)
if db_name:
return odoo.sql_db.db_connect(db_name)
def _get_cr(self, frame, allow_create=True):
# try, in order: cr, cursor, self.env.cr, self.cr,
# request.env.cr
if 'cr' in frame.f_locals:
return frame.f_locals['cr'], False
if 'cursor' in frame.f_locals:
return frame.f_locals['cursor'], False
s = frame.f_locals.get('self')
if hasattr(s, 'env'):
return s.env.cr, False
if hasattr(s, 'cr'):
return s.cr, False
try:
from odoo.http import request
return request.env.cr, False
except RuntimeError:
pass
if allow_create:
# create a new cursor
db = self._get_db()
if db is not None:
return db.cursor(), True
return None, False
def _get_uid(self, frame):
# try, in order: uid, user, self.env.uid
if 'uid' in frame.f_locals:
return frame.f_locals['uid']
if 'user' in frame.f_locals:
return int(frame.f_locals['user']) # user may be a record
s = frame.f_locals.get('self')
return s.env.uid
def _get_lang(self, frame):
# try, in order: context.get('lang'), kwargs['context'].get('lang'),
# self.env.lang, self.localcontext.get('lang'), request.env.lang
lang = None
if frame.f_locals.get('context'):
lang = frame.f_locals['context'].get('lang')
if not lang:
kwargs = frame.f_locals.get('kwargs', {})
if kwargs.get('context'):
lang = kwargs['context'].get('lang')
if not lang:
s = frame.f_locals.get('self')
if hasattr(s, 'env'):
lang = s.env.lang
if not lang:
if hasattr(s, 'localcontext'):
lang = s.localcontext.get('lang')
if not lang:
try:
from odoo.http import request
lang = request.env.lang
except RuntimeError:
pass
if not lang:
# Last resort: attempt to guess the language of the user
# Pitfall: some operations are performed in sudo mode, and we
# don't know the original uid, so the language may
# be wrong when the admin language differs.
(cr, dummy) = self._get_cr(frame, allow_create=False)
uid = self._get_uid(frame)
if cr and uid:
env = odoo.api.Environment(cr, uid, {})
lang = env['res.users'].context_get()['lang']
return lang
def __call__(self, source, *args, **kwargs):
translation = self._get_translation(source)
assert not (args and kwargs)
if args or kwargs:
if any(isinstance(a, Markup) for a in itertools.chain(args, kwargs.values())):
translation = escape(translation)
try:
return translation % (args or kwargs)
except (TypeError, ValueError, KeyError):
bad = translation
# fallback: apply to source before logging exception (in case source fails)
translation = source % (args or kwargs)
_logger.exception('Bad translation %r for string %r', bad, source)
return translation
def _get_translation(self, source, module=None):
try:
frame = inspect.currentframe().f_back.f_back
lang = self._get_lang(frame)
if lang and lang != 'en_US':
if not module:
path = inspect.getfile(frame)
path_info = odoo.modules.get_resource_from_path(path)
module = path_info[0] if path_info else 'base'
return code_translations.get_python_translations(module, lang).get(source, source)
else:
_logger.debug('no translation language detected, skipping translation for "%r" ', source)
except Exception:
_logger.debug('translation went wrong for "%r", skipped', source)
# if so, double-check the root/base translations filenames
return source
@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', '_args', '_module']
def __init__(self, source, *args, **kwargs):
self._source = source
assert not (args and kwargs)
self._args = args or kwargs
frame = inspect.currentframe().f_back
path = inspect.getfile(frame)
path_info = odoo.modules.get_resource_from_path(path)
self._module = path_info[0] if path_info else 'base'
def __str__(self):
# Call _._get_translation() like _() does, so that we have the same number
# of stack frames calling _get_translation()
translation = _._get_translation(self._source, self._module)
if self._args:
try:
return translation % self._args
except (TypeError, ValueError, KeyError):
bad = translation
# fallback: apply to source before logging exception (in case source fails)
translation = self._source % self._args
_logger.exception('Bad translation %r for string %r', bad, self._source)
return translation
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()
def __add__(self, other):
# Call _._get_translation() like _() does, so that we have the same number
# of stack frames calling _get_translation()
if isinstance(other, str):
return _._get_translation(self._source) + other
elif isinstance(other, _lt):
return _._get_translation(self._source) + _._get_translation(other._source)
return NotImplemented
def __radd__(self, other):
# Call _._get_translation() like _() does, so that we have the same number
# of stack frames calling _get_translation()
if isinstance(other, str):
return other + _._get_translation(self._source)
return NotImplemented
_ = GettextAlias()
def quote(s):
"""Returns quoted PO term string, with special PO characters escaped"""
assert r"\n" not in s, "Translation terms may not include escaped newlines ('\\n'), please use only literal newlines! (in '%s')" % s
return '"%s"' % s.replace('\\','\\\\') \
.replace('"','\\"') \
.replace('\n', '\\n"\n"')
re_escaped_char = re.compile(r"(\\.)")
re_escaped_replacements = {'n': '\n', 't': '\t',}
def _sub_replacement(match_obj):
return re_escaped_replacements.get(match_obj.group(1)[1], match_obj.group(1)[1])
def unquote(str):
"""Returns unquoted PO term string, with special PO characters unescaped"""
return re_escaped_char.sub(_sub_replacement, str[1:-1])
def TranslationFileReader(source, fileformat='po'):
""" Iterate over translation file to return Odoo translation entries """
if fileformat == 'csv':
return CSVFileReader(source)
if fileformat == 'po':
return PoFileReader(source)
_logger.info('Bad file format: %s', fileformat)
raise Exception(_('Bad file format: %s', fileformat))
class CSVFileReader:
def __init__(self, source):
_reader = codecs.getreader('utf-8')
self.source = csv.DictReader(_reader(source), quotechar='"', delimiter=',')
self.prev_code_src = ""
def __iter__(self):
for entry in self.source:
# determine . from res_id
if entry["res_id"] and entry["res_id"].isnumeric():
# res_id is an id or line number
entry["res_id"] = int(entry["res_id"])
elif not entry.get("imd_name"):
# res_id is an external id and must follow .
entry["module"], entry["imd_name"] = entry["res_id"].split(".")
entry["res_id"] = None
if entry["type"] == "model" or entry["type"] == "model_terms":
entry["imd_model"] = entry["name"].partition(',')[0]
if entry["type"] == "code":
if entry["src"] == self.prev_code_src:
# skip entry due to unicity constrain on code translations
continue
self.prev_code_src = entry["src"]
yield entry
class PoFileReader:
""" Iterate over po file to return Odoo translation entries """
def __init__(self, source):
def get_pot_path(source_name):
# when fileobj is a TemporaryFile, its name is an inter in P3, a string in P2
if isinstance(source_name, str) and source_name.endswith('.po'):
# Normally the path looks like /path/to/xxx/i18n/lang.po
# and we try to find the corresponding
# /path/to/xxx/i18n/xxx.pot file.
# (Sometimes we have 'i18n_extra' instead of just 'i18n')
path = Path(source_name)
filename = path.parent.parent.name + '.pot'
pot_path = path.with_name(filename)
return pot_path.exists() and str(pot_path) or False
return False
# polib accepts a path or the file content as a string, not a fileobj
if isinstance(source, str):
self.pofile = polib.pofile(source)
pot_path = get_pot_path(source)
else:
# either a BufferedIOBase or result from NamedTemporaryFile
self.pofile = polib.pofile(source.read().decode())
pot_path = get_pot_path(source.name)
if pot_path:
# Make a reader for the POT file
# (Because the POT comments are correct on GitHub but the
# PO comments tends to be outdated. See LP bug 933496.)
self.pofile.merge(polib.pofile(pot_path))
def __iter__(self):
for entry in self.pofile:
if entry.obsolete:
continue
# in case of moduleS keep only the first
match = re.match(r"(module[s]?): (\w+)", entry.comment)
_, module = match.groups()
comments = "\n".join([c for c in entry.comment.split('\n') if not c.startswith('module:')])
source = entry.msgid
translation = entry.msgstr
found_code_occurrence = False
for occurrence, line_number in entry.occurrences:
match = re.match(r'(model|model_terms):([\w.]+),([\w]+):(\w+)\.([^ ]+)', occurrence)
if match:
type, model_name, field_name, module, xmlid = match.groups()
yield {
'type': type,
'imd_model': model_name,
'name': model_name+','+field_name,
'imd_name': xmlid,
'res_id': None,
'src': source,
'value': translation,
'comments': comments,
'module': module,
}
continue
match = re.match(r'(code):([\w/.]+)', occurrence)
if match:
type, name = match.groups()
if found_code_occurrence:
# unicity constrain on code translation
continue
found_code_occurrence = True
yield {
'type': type,
'name': name,
'src': source,
'value': translation,
'comments': comments,
'res_id': int(line_number),
'module': module,
}
continue
match = re.match(r'(selection):([\w.]+),([\w]+)', occurrence)
if match:
_logger.info("Skipped deprecated occurrence %s", occurrence)
continue
match = re.match(r'(sql_constraint|constraint):([\w.]+)', occurrence)
if match:
_logger.info("Skipped deprecated occurrence %s", occurrence)
continue
_logger.error("malformed po file: unknown occurrence: %s", occurrence)
def TranslationFileWriter(target, fileformat='po', lang=None):
""" Iterate over translation file to return Odoo translation entries """
if fileformat == 'csv':
return CSVFileWriter(target)
if fileformat == 'po':
return PoFileWriter(target, lang=lang)
if fileformat == 'tgz':
return TarFileWriter(target, lang=lang)
raise Exception(_('Unrecognized extension: must be one of '
'.csv, .po, or .tgz (received .%s).') % fileformat)
class CSVFileWriter:
def __init__(self, target):
self.writer = pycompat.csv_writer(target, dialect='UNIX')
# write header first
self.writer.writerow(("module","type","name","res_id","src","value","comments"))
def write_rows(self, rows):
for module, type, name, res_id, src, trad, comments in rows:
comments = '\n'.join(comments)
self.writer.writerow((module, type, name, res_id, src, trad, comments))
class PoFileWriter:
""" Iterate over po file to return Odoo translation entries """
def __init__(self, target, lang):
self.buffer = target
self.lang = lang
self.po = polib.POFile()
def write_rows(self, rows):
# we now group the translations by source. That means one translation per source.
grouped_rows = {}
modules = set()
for module, type, name, res_id, src, trad, comments in rows:
row = grouped_rows.setdefault(src, {})
row.setdefault('modules', set()).add(module)
if not row.get('translation') and trad != src:
row['translation'] = trad
row.setdefault('tnrs', []).append((type, name, res_id))
row.setdefault('comments', set()).update(comments)
modules.add(module)
for src, row in sorted(grouped_rows.items()):
if not self.lang:
# translation template, so no translation value
row['translation'] = ''
elif not row.get('translation'):
row['translation'] = ''
self.add_entry(sorted(row['modules']), sorted(row['tnrs']), src, row['translation'], sorted(row['comments']))
import odoo.release as release
self.po.header = "Translation of %s.\n" \
"This file contains the translation of the following modules:\n" \
"%s" % (release.description, ''.join("\t* %s\n" % m for m in modules))
now = datetime.utcnow().strftime('%Y-%m-%d %H:%M+0000')
self.po.metadata = {
'Project-Id-Version': "%s %s" % (release.description, release.version),
'Report-Msgid-Bugs-To': '',
'POT-Creation-Date': now,
'PO-Revision-Date': now,
'Last-Translator': '',
'Language-Team': '',
'MIME-Version': '1.0',
'Content-Type': 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding': '',
'Plural-Forms': '',
}
# buffer expects bytes
self.buffer.write(str(self.po).encode())
def add_entry(self, modules, tnrs, source, trad, comments=None):
entry = polib.POEntry(
msgid=source,
msgstr=trad,
)
plural = len(modules) > 1 and 's' or ''
entry.comment = "module%s: %s" % (plural, ', '.join(modules))
if comments:
entry.comment += "\n" + "\n".join(comments)
code = False
for typy, name, res_id in tnrs:
if typy == 'code':
code = True
res_id = 0
if isinstance(res_id, int) or res_id.isdigit():
# second term of occurrence must be a digit
# occurrence line at 0 are discarded when rendered to string
entry.occurrences.append((u"%s:%s" % (typy, name), str(res_id)))
else:
entry.occurrences.append((u"%s:%s:%s" % (typy, name, res_id), ''))
if code:
# TODO 17.0: remove the flag python-format in all PO/POT files
# The flag is used in a wrong way. It marks all code translations even for javascript translations.
entry.flags.append("python-format")
self.po.append(entry)
class TarFileWriter:
def __init__(self, target, lang):
self.tar = tarfile.open(fileobj=target, mode='w|gz')
self.lang = lang
def write_rows(self, rows):
rows_by_module = defaultdict(list)
for row in rows:
module = row[0]
rows_by_module[module].append(row)
for mod, modrows in rows_by_module.items():
with io.BytesIO() as buf:
po = PoFileWriter(buf, lang=self.lang)
po.write_rows(modrows)
buf.seek(0)
info = tarfile.TarInfo(
join(mod, 'i18n', '{basename}.{ext}'.format(
basename=self.lang or mod,
ext='po' if self.lang else 'pot',
)))
# addfile will read bytes from the buffer so
# size *must* be set first
info.size = len(buf.getvalue())
self.tar.addfile(info, fileobj=buf)
self.tar.close()
# Methods to export the translation file
def trans_export(lang, modules, buffer, format, cr):
reader = TranslationModuleReader(cr, modules=modules, lang=lang)
writer = TranslationFileWriter(buffer, fileformat=format, lang=lang)
writer.write_rows(reader)
# pylint: disable=redefined-builtin
def trans_export_records(lang, model_name, ids, buffer, format, cr):
reader = TranslationRecordReader(cr, model_name, ids, lang=lang)
writer = TranslationFileWriter(buffer, fileformat=format, lang=lang)
writer.write_rows(reader)
def _push(callback, term, source_line):
""" Sanity check before pushing translation terms """
term = (term or "").strip()
# Avoid non-char tokens like ':' '...' '.00' etc.
if len(term) > 8 or any(x.isalpha() for x in term):
callback(term, source_line)
def _extract_translatable_qweb_terms(element, callback):
""" Helper method to walk an etree document representing
a QWeb template, and call ``callback(term)`` for each
translatable term that is found in the document.
:param etree._Element element: root of etree document to extract terms from
:param Callable callback: a callable in the form ``f(term, source_line)``,
that will be called for each extracted term.
"""
# not using elementTree.iterparse because we need to skip sub-trees in case
# the ancestor element had a reason to be skipped
for el in element:
if isinstance(el, SKIPPED_ELEMENT_TYPES): continue
if (el.tag.lower() not in SKIPPED_ELEMENTS
and "t-js" not in el.attrib
and not ("t-jquery" in el.attrib and "t-operation" not in el.attrib)
and not (el.tag == 'attribute' and el.get('name') not in TRANSLATED_ATTRS)
and el.get("t-translation", '').strip() != "off"):
_push(callback, el.text, el.sourceline)
# Do not export terms contained on the Component directive of OWL
# attributes in this context are most of the time variables,
# not real HTML attributes.
# Node tags starting with a capital letter are considered OWL Components
# and a widespread convention and good practice for DOM tags is to write
# them all lower case.
# https://www.w3schools.com/html/html5_syntax.asp
# https://github.com/odoo/owl/blob/master/doc/reference/component.md#composition
if not el.tag[0].isupper() and 't-component' not in el.attrib and 't-set-slot' not in el.attrib:
for att in TRANSLATED_ATTRS:
if att in el.attrib:
_push(callback, el.attrib[att], el.sourceline)
_extract_translatable_qweb_terms(el, callback)
_push(callback, el.tail, el.sourceline)
def babel_extract_qweb(fileobj, keywords, comment_tags, options):
"""Babel message extractor for qweb template files.
:param fileobj: the file-like object the messages should be extracted from
:param keywords: a list of keywords (i.e. function names) that should
be recognized as translation functions
:param comment_tags: a list of translator tags to search for and
include in the results
:param options: a dictionary of additional options (optional)
:return: an iterator over ``(lineno, funcname, message, comments)``
tuples
:rtype: Iterable
"""
result = []
def handle_text(text, lineno):
result.append((lineno, None, text, []))
tree = etree.parse(fileobj)
_extract_translatable_qweb_terms(tree.getroot(), handle_text)
return result
def extract_formula_terms(formula):
"""Extract strings in a spreadsheet formula which are arguments to '_t' functions
>>> extract_formula_terms('=_t("Hello") + _t("Raoul")')
["Hello", "Raoul"]
"""
tokens = generate_tokens(io.StringIO(formula).readline)
tokens = (token for token in tokens if token.type not in {NEWLINE, INDENT, DEDENT})
for t1 in tokens:
if not t1.string == '_t':
continue
t2 = next(tokens, None)
if t2 and t2.string == '(':
t3 = next(tokens, None)
t4 = next(tokens, None)
if t4 and t4.string == ')' and t3 and t3.type == STRING:
yield t3.string[1:][:-1] # strip leading and trailing quotes
def extract_spreadsheet_terms(fileobj, keywords, comment_tags, options):
"""Babel message extractor for spreadsheet data files.
:param fileobj: the file-like object the messages should be extracted from
:param keywords: a list of keywords (i.e. function names) that should
be recognized as translation functions
:param comment_tags: a list of translator tags to search for and
include in the results
:param options: a dictionary of additional options (optional)
:return: an iterator over ``(lineno, funcname, message, comments)``
tuples
"""
terms = []
data = json.load(fileobj)
for sheet in data.get('sheets', []):
for cell in sheet['cells'].values():
content = cell.get('content', '')
if content.startswith('='):
terms += extract_formula_terms(content)
else:
markdown_link = re.fullmatch(r'\[(.+)\]\(.+\)', content)
if markdown_link:
terms.append(markdown_link[1])
for figure in sheet['figures']:
terms.append(figure['data']['title'])
if 'baselineDescr' in figure['data']:
terms.append(figure['data']['baselineDescr'])
pivots = data.get('pivots', {}).values()
lists = data.get('lists', {}).values()
for data_source in itertools.chain(lists, pivots):
if 'name' in data_source:
terms.append(data_source['name'])
for global_filter in data.get('globalFilters', []):
terms.append(global_filter['label'])
return (
(0, None, term, [])
for term in terms
if any(x.isalpha() for x in term)
)
ImdInfo = namedtuple('ExternalId', ['name', 'model', 'res_id', 'module'])
class TranslationReader:
def __init__(self, cr, lang=None):
self._cr = cr
self._lang = lang or 'en_US'
self.env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
self._to_translate = []
def __iter__(self):
for module, source, name, res_id, ttype, comments, _record_id, value in self._to_translate:
yield (module, ttype, name, res_id, source, encode(odoo.tools.ustr(value)), comments)
def _push_translation(self, module, ttype, name, res_id, source, comments=None, record_id=None, value=None):
""" Insert a translation that will be used in the file generation
In po file will create an entry
#: ::
#,
msgid "