gooderp18绿色标准版
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

1901 lines
78KB

  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. # When using quotation marks in translation strings, please use curly quotes (“”)
  3. # instead of straight quotes (""). On Linux, the keyboard shortcuts are:
  4. # AltGr + V for the opening curly quotes “
  5. # AltGr + B for the closing curly quotes ”
  6. from __future__ import annotations
  7. import codecs
  8. import fnmatch
  9. import functools
  10. import inspect
  11. import io
  12. import itertools
  13. import json
  14. import locale
  15. import logging
  16. import os
  17. import polib
  18. import re
  19. import tarfile
  20. import typing
  21. import warnings
  22. from collections import defaultdict, namedtuple
  23. from contextlib import suppress
  24. from datetime import datetime
  25. from os.path import join
  26. from pathlib import Path
  27. from tokenize import generate_tokens, STRING, NEWLINE, INDENT, DEDENT
  28. from babel.messages import extract
  29. from lxml import etree, html
  30. from markupsafe import escape, Markup
  31. from psycopg2.extras import Json
  32. import odoo
  33. from odoo.exceptions import UserError
  34. from .config import config
  35. from .misc import file_open, file_path, get_iso_codes, OrderedSet, ReadonlyDict, SKIPPED_ELEMENT_TYPES
  36. __all__ = [
  37. "_",
  38. "LazyTranslate",
  39. "html_translate",
  40. "xml_translate",
  41. ]
  42. _logger = logging.getLogger(__name__)
  43. PYTHON_TRANSLATION_COMMENT = 'odoo-python'
  44. # translation used for javascript code in web client
  45. JAVASCRIPT_TRANSLATION_COMMENT = 'odoo-javascript'
  46. SKIPPED_ELEMENTS = ('script', 'style', 'title')
  47. _LOCALE2WIN32 = {
  48. 'af_ZA': 'Afrikaans_South Africa',
  49. 'sq_AL': 'Albanian_Albania',
  50. 'ar_SA': 'Arabic_Saudi Arabia',
  51. 'eu_ES': 'Basque_Spain',
  52. 'be_BY': 'Belarusian_Belarus',
  53. 'bs_BA': 'Bosnian_Bosnia and Herzegovina',
  54. 'bg_BG': 'Bulgarian_Bulgaria',
  55. 'ca_ES': 'Catalan_Spain',
  56. 'hr_HR': 'Croatian_Croatia',
  57. 'zh_CN': 'Chinese_China',
  58. 'zh_TW': 'Chinese_Taiwan',
  59. 'cs_CZ': 'Czech_Czech Republic',
  60. 'da_DK': 'Danish_Denmark',
  61. 'nl_NL': 'Dutch_Netherlands',
  62. 'et_EE': 'Estonian_Estonia',
  63. 'fa_IR': 'Farsi_Iran',
  64. 'ph_PH': 'Filipino_Philippines',
  65. 'fi_FI': 'Finnish_Finland',
  66. 'fr_FR': 'French_France',
  67. 'fr_BE': 'French_France',
  68. 'fr_CH': 'French_France',
  69. 'fr_CA': 'French_France',
  70. 'ga': 'Scottish Gaelic',
  71. 'gl_ES': 'Galician_Spain',
  72. 'ka_GE': 'Georgian_Georgia',
  73. 'de_DE': 'German_Germany',
  74. 'el_GR': 'Greek_Greece',
  75. 'gu': 'Gujarati_India',
  76. 'he_IL': 'Hebrew_Israel',
  77. 'hi_IN': 'Hindi',
  78. 'hu': 'Hungarian_Hungary',
  79. 'is_IS': 'Icelandic_Iceland',
  80. 'id_ID': 'Indonesian_Indonesia',
  81. 'it_IT': 'Italian_Italy',
  82. 'ja_JP': 'Japanese_Japan',
  83. 'kn_IN': 'Kannada',
  84. 'km_KH': 'Khmer',
  85. 'ko_KR': 'Korean_Korea',
  86. 'lo_LA': 'Lao_Laos',
  87. 'lt_LT': 'Lithuanian_Lithuania',
  88. 'lat': 'Latvian_Latvia',
  89. 'ml_IN': 'Malayalam_India',
  90. 'mi_NZ': 'Maori',
  91. 'mn': 'Cyrillic_Mongolian',
  92. 'no_NO': 'Norwegian_Norway',
  93. 'nn_NO': 'Norwegian-Nynorsk_Norway',
  94. 'pl': 'Polish_Poland',
  95. 'pt_PT': 'Portuguese_Portugal',
  96. 'pt_BR': 'Portuguese_Brazil',
  97. 'ro_RO': 'Romanian_Romania',
  98. 'ru_RU': 'Russian_Russia',
  99. 'sr_CS': 'Serbian (Cyrillic)_Serbia and Montenegro',
  100. 'sk_SK': 'Slovak_Slovakia',
  101. 'sl_SI': 'Slovenian_Slovenia',
  102. #should find more specific locales for Spanish countries,
  103. #but better than nothing
  104. 'es_AR': 'Spanish_Spain',
  105. 'es_BO': 'Spanish_Spain',
  106. 'es_CL': 'Spanish_Spain',
  107. 'es_CO': 'Spanish_Spain',
  108. 'es_CR': 'Spanish_Spain',
  109. 'es_DO': 'Spanish_Spain',
  110. 'es_EC': 'Spanish_Spain',
  111. 'es_ES': 'Spanish_Spain',
  112. 'es_GT': 'Spanish_Spain',
  113. 'es_HN': 'Spanish_Spain',
  114. 'es_MX': 'Spanish_Spain',
  115. 'es_NI': 'Spanish_Spain',
  116. 'es_PA': 'Spanish_Spain',
  117. 'es_PE': 'Spanish_Spain',
  118. 'es_PR': 'Spanish_Spain',
  119. 'es_PY': 'Spanish_Spain',
  120. 'es_SV': 'Spanish_Spain',
  121. 'es_UY': 'Spanish_Spain',
  122. 'es_VE': 'Spanish_Spain',
  123. 'sv_SE': 'Swedish_Sweden',
  124. 'ta_IN': 'English_Australia',
  125. 'th_TH': 'Thai_Thailand',
  126. 'tr_TR': 'Turkish_Türkiye',
  127. 'uk_UA': 'Ukrainian_Ukraine',
  128. 'vi_VN': 'Vietnamese_Viet Nam',
  129. 'tlh_TLH': 'Klingon',
  130. }
  131. # these direct uses of CSV are ok.
  132. import csv # pylint: disable=deprecated-module
  133. class UNIX_LINE_TERMINATOR(csv.excel):
  134. lineterminator = '\n'
  135. csv.register_dialect("UNIX", UNIX_LINE_TERMINATOR)
  136. # which elements are translated inline
  137. TRANSLATED_ELEMENTS = {
  138. 'abbr', 'b', 'bdi', 'bdo', 'br', 'cite', 'code', 'data', 'del', 'dfn', 'em',
  139. 'font', 'i', 'ins', 'kbd', 'keygen', 'mark', 'math', 'meter', 'output',
  140. 'progress', 'q', 'ruby', 's', 'samp', 'small', 'span', 'strong', 'sub',
  141. 'sup', 'time', 'u', 'var', 'wbr', 'text', 'select', 'option',
  142. }
  143. # Which attributes must be translated. This is a dict, where the value indicates
  144. # a condition for a node to have the attribute translatable.
  145. TRANSLATED_ATTRS = dict.fromkeys({
  146. 'string', 'add-label', 'help', 'sum', 'avg', 'confirm', 'placeholder', 'alt', 'title', 'aria-label',
  147. 'aria-keyshortcuts', 'aria-placeholder', 'aria-roledescription', 'aria-valuetext',
  148. 'value_label', 'data-tooltip', 'label',
  149. }, lambda e: True)
  150. def translate_attrib_value(node):
  151. # check if the value attribute of a node must be translated
  152. classes = node.attrib.get('class', '').split(' ')
  153. return (
  154. (node.tag == 'input' and node.attrib.get('type', 'text') == 'text')
  155. and 'datetimepicker-input' not in classes
  156. or (node.tag == 'input' and node.attrib.get('type') == 'hidden')
  157. and 'o_translatable_input_hidden' in classes
  158. )
  159. TRANSLATED_ATTRS.update(
  160. value=translate_attrib_value,
  161. text=lambda e: (e.tag == 'field' and e.attrib.get('widget', '') == 'url'),
  162. **{f't-attf-{attr}': cond for attr, cond in TRANSLATED_ATTRS.items()},
  163. )
  164. avoid_pattern = re.compile(r"\s*<!DOCTYPE", re.IGNORECASE | re.MULTILINE | re.UNICODE)
  165. space_pattern = re.compile(r"[\s\uFEFF]*") # web_editor uses \uFEFF as ZWNBSP
  166. def translate_xml_node(node, callback, parse, serialize):
  167. """ Return the translation of the given XML/HTML node.
  168. :param node:
  169. :param callback: callback(text) returns translated text or None
  170. :param parse: parse(text) returns a node (text is unicode)
  171. :param serialize: serialize(node) returns unicode text
  172. """
  173. def nonspace(text):
  174. """ Return whether ``text`` is a string with non-space characters. """
  175. return bool(text) and not space_pattern.fullmatch(text)
  176. def translatable(node):
  177. """ Return whether the given node can be translated as a whole. """
  178. return (
  179. # Some specific nodes (e.g., text highlights) have an auto-updated
  180. # DOM structure that makes them impossible to translate.
  181. # The introduction of a translation `<span>` in the middle of their
  182. # hierarchy breaks their functionalities. We need to force them to
  183. # be translated as a whole using the `o_translate_inline` class.
  184. "o_translate_inline" in node.attrib.get("class", "").split()
  185. or node.tag in TRANSLATED_ELEMENTS
  186. and not any(key.startswith("t-") for key in node.attrib)
  187. and all(translatable(child) for child in node)
  188. )
  189. def hastext(node, pos=0):
  190. """ Return whether the given node contains some text to translate at the
  191. given child node position. The text may be before the child node,
  192. inside it, or after it.
  193. """
  194. return (
  195. # there is some text before node[pos]
  196. nonspace(node[pos-1].tail if pos else node.text)
  197. or (
  198. pos < len(node)
  199. and translatable(node[pos])
  200. and (
  201. any( # attribute to translate
  202. val and key in TRANSLATED_ATTRS and TRANSLATED_ATTRS[key](node[pos])
  203. for key, val in node[pos].attrib.items()
  204. )
  205. # node[pos] contains some text to translate
  206. or hastext(node[pos])
  207. # node[pos] has no text, but there is some text after it
  208. or hastext(node, pos + 1)
  209. )
  210. )
  211. )
  212. def process(node):
  213. """ Translate the given node. """
  214. if (
  215. isinstance(node, SKIPPED_ELEMENT_TYPES)
  216. or node.tag in SKIPPED_ELEMENTS
  217. or node.get('t-translation', "").strip() == "off"
  218. or node.tag == 'attribute' and node.get('name') not in TRANSLATED_ATTRS
  219. or node.getparent() is None and avoid_pattern.match(node.text or "")
  220. ):
  221. return
  222. pos = 0
  223. while True:
  224. # check for some text to translate at the given position
  225. if hastext(node, pos):
  226. # move all translatable children nodes from the given position
  227. # into a <div> element
  228. div = etree.Element('div')
  229. div.text = (node[pos-1].tail if pos else node.text) or ''
  230. while pos < len(node) and translatable(node[pos]):
  231. div.append(node[pos])
  232. # translate the content of the <div> element as a whole
  233. content = serialize(div)[5:-6]
  234. original = content.strip()
  235. translated = callback(original)
  236. if translated:
  237. result = content.replace(original, translated)
  238. # <div/> is used to auto fix crapy result
  239. result_elem = parse_html(f"<div>{result}</div>")
  240. # change the tag to <span/> which is one of TRANSLATED_ELEMENTS
  241. # so that 'result_elem' can be checked by translatable and hastext
  242. result_elem.tag = 'span'
  243. if translatable(result_elem) and hastext(result_elem):
  244. div = result_elem
  245. if pos:
  246. node[pos-1].tail = div.text
  247. else:
  248. node.text = div.text
  249. # move the content of the <div> element back inside node
  250. while len(div) > 0:
  251. node.insert(pos, div[0])
  252. pos += 1
  253. if pos >= len(node):
  254. break
  255. # node[pos] is not translatable as a whole, process it recursively
  256. process(node[pos])
  257. pos += 1
  258. # translate the attributes of the node
  259. for key, val in node.attrib.items():
  260. if nonspace(val) and key in TRANSLATED_ATTRS and TRANSLATED_ATTRS[key](node):
  261. node.set(key, callback(val.strip()) or val)
  262. process(node)
  263. return node
  264. def parse_xml(text):
  265. return etree.fromstring(text)
  266. def serialize_xml(node):
  267. return etree.tostring(node, method='xml', encoding='unicode')
  268. MODIFIER_ATTRS = {"invisible", "readonly", "required", "column_invisible", "attrs"}
  269. def xml_term_adapter(term_en):
  270. """
  271. Returns an `adapter(term)` function that will ensure the modifiers are copied
  272. from the base `term_en` to the translated `term` when the XML structure of
  273. both terms match. `term_en` and any input `term` to the adapter must be valid
  274. XML terms. Using the adapter only makes sense if `term_en` contains some tags
  275. from TRANSLATED_ELEMENTS.
  276. """
  277. orig_node = parse_xml(f"<div>{term_en}</div>")
  278. def same_struct_iter(left, right):
  279. if left.tag != right.tag or len(left) != len(right):
  280. raise ValueError("Non matching struct")
  281. yield left, right
  282. left_iter = left.iterchildren()
  283. right_iter = right.iterchildren()
  284. for lc, rc in zip(left_iter, right_iter):
  285. yield from same_struct_iter(lc, rc)
  286. def adapter(term):
  287. new_node = parse_xml(f"<div>{term}</div>")
  288. try:
  289. for orig_n, new_n in same_struct_iter(orig_node, new_node):
  290. removed_attrs = [k for k in new_n.attrib if k in MODIFIER_ATTRS and k not in orig_n.attrib]
  291. for k in removed_attrs:
  292. del new_n.attrib[k]
  293. keep_attrs = {k: v for k, v in orig_n.attrib.items()}
  294. new_n.attrib.update(keep_attrs)
  295. except ValueError: # non-matching structure
  296. return None
  297. # remove tags <div> and </div> from result
  298. return serialize_xml(new_node)[5:-6]
  299. return adapter
  300. _HTML_PARSER = etree.HTMLParser(encoding='utf8')
  301. def parse_html(text):
  302. try:
  303. parse = html.fragment_fromstring(text, parser=_HTML_PARSER)
  304. except (etree.ParserError, TypeError) as e:
  305. raise UserError(_("Error while parsing view:\n\n%s") % e) from e
  306. return parse
  307. def serialize_html(node):
  308. return etree.tostring(node, method='html', encoding='unicode')
  309. def xml_translate(callback, value):
  310. """ Translate an XML value (string), using `callback` for translating text
  311. appearing in `value`.
  312. """
  313. if not value:
  314. return value
  315. try:
  316. root = parse_xml(value)
  317. result = translate_xml_node(root, callback, parse_xml, serialize_xml)
  318. return serialize_xml(result)
  319. except etree.ParseError:
  320. # fallback for translated terms: use an HTML parser and wrap the term
  321. root = parse_html(u"<div>%s</div>" % value)
  322. result = translate_xml_node(root, callback, parse_xml, serialize_xml)
  323. # remove tags <div> and </div> from result
  324. return serialize_xml(result)[5:-6]
  325. def xml_term_converter(value):
  326. """ Convert the HTML fragment ``value`` to XML if necessary
  327. """
  328. # wrap value inside a div and parse it as HTML
  329. div = f"<div>{value}</div>"
  330. root = etree.fromstring(div, etree.HTMLParser())
  331. # root is html > body > div
  332. # serialize div as XML and discard surrounding tags
  333. return etree.tostring(root[0][0], encoding='unicode')[5:-6]
  334. def html_translate(callback, value):
  335. """ Translate an HTML value (string), using `callback` for translating text
  336. appearing in `value`.
  337. """
  338. if not value:
  339. return value
  340. try:
  341. # value may be some HTML fragment, wrap it into a div
  342. root = parse_html("<div>%s</div>" % value)
  343. result = translate_xml_node(root, callback, parse_html, serialize_html)
  344. # remove tags <div> and </div> from result
  345. value = serialize_html(result)[5:-6].replace('\xa0', '&nbsp;')
  346. except ValueError:
  347. _logger.exception("Cannot translate malformed HTML, using source value instead")
  348. return value
  349. def html_term_converter(value):
  350. """ Convert the HTML fragment ``value`` to XML if necessary
  351. """
  352. # wrap value inside a div and parse it as HTML
  353. div = f"<div>{value}</div>"
  354. root = etree.fromstring(div, etree.HTMLParser())
  355. # root is html > body > div
  356. # serialize div as HTML and discard surrounding tags
  357. return etree.tostring(root[0][0], encoding='unicode', method='html')[5:-6]
  358. def get_text_content(term):
  359. """ Return the textual content of the given term. """
  360. content = html.fromstring(term).text_content()
  361. return " ".join(content.split())
  362. def is_text(term):
  363. """ Return whether the term has only text. """
  364. return len(html.fromstring(f"<_>{term}</_>")) == 0
  365. xml_translate.get_text_content = get_text_content
  366. html_translate.get_text_content = get_text_content
  367. xml_translate.term_converter = xml_term_converter
  368. html_translate.term_converter = html_term_converter
  369. xml_translate.is_text = is_text
  370. html_translate.is_text = is_text
  371. xml_translate.term_adapter = xml_term_adapter
  372. def translate_sql_constraint(cr, key, lang):
  373. cr.execute("""
  374. SELECT COALESCE(c.message->>%s, c.message->>'en_US') as message
  375. FROM ir_model_constraint c
  376. WHERE name=%s and type='u'
  377. """, (lang, key))
  378. return cr.fetchone()[0]
  379. def get_translation(module: str, lang: str, source: str, args: tuple | dict) -> str:
  380. """Translate and format using a module, language, source text and args."""
  381. # get the translation by using the language
  382. assert lang, "missing language for translation"
  383. if lang == 'en_US':
  384. translation = source
  385. else:
  386. assert module, "missing module name for translation"
  387. translation = code_translations.get_python_translations(module, lang).get(source, source)
  388. # skip formatting if we have no args
  389. if not args:
  390. return translation
  391. # we need to check the args for markup values and for lazy translations
  392. args_is_dict = isinstance(args, dict)
  393. if any(isinstance(a, Markup) for a in (args.values() if args_is_dict else args)):
  394. translation = escape(translation)
  395. if any(isinstance(a, LazyGettext) for a in (args.values() if args_is_dict else args)):
  396. if args_is_dict:
  397. args = {k: v._translate(lang) if isinstance(v, LazyGettext) else v for k, v in args.items()}
  398. else:
  399. args = tuple(v._translate(lang) if isinstance(v, LazyGettext) else v for v in args)
  400. # format
  401. try:
  402. return translation % args
  403. except (TypeError, ValueError, KeyError):
  404. bad = translation
  405. # fallback: apply to source before logging exception (in case source fails)
  406. translation = source % args
  407. _logger.exception('Bad translation %r for string %r', bad, source)
  408. return translation
  409. def get_translated_module(arg: str | int | typing.Any) -> str: # frame not represented as hint
  410. """Get the addons name.
  411. :param arg: can be any of the following:
  412. str ("name_of_module") returns itself;
  413. str (__name__) use to resolve module name;
  414. int is number of frames to go back to the caller;
  415. frame of the caller function
  416. """
  417. if isinstance(arg, str):
  418. if arg.startswith('odoo.addons.'):
  419. # get the name of the module
  420. return arg.split('.')[2]
  421. if '.' in arg or not arg:
  422. # module name is not in odoo.addons.
  423. return 'base'
  424. else:
  425. return arg
  426. else:
  427. if isinstance(arg, int):
  428. frame = inspect.currentframe()
  429. while arg > 0:
  430. arg -= 1
  431. frame = frame.f_back
  432. else:
  433. frame = arg
  434. if not frame:
  435. return 'base'
  436. if (module_name := frame.f_globals.get("__name__")) and module_name.startswith('odoo.addons.'):
  437. # just a quick lookup because `get_resource_from_path is slow compared to this`
  438. return module_name.split('.')[2]
  439. path = inspect.getfile(frame)
  440. path_info = odoo.modules.get_resource_from_path(path)
  441. return path_info[0] if path_info else 'base'
  442. def _get_cr(frame):
  443. # try, in order: cr, cursor, self.env.cr, self.cr,
  444. # request.env.cr
  445. if 'cr' in frame.f_locals:
  446. return frame.f_locals['cr']
  447. if 'cursor' in frame.f_locals:
  448. return frame.f_locals['cursor']
  449. if (local_self := frame.f_locals.get('self')) is not None:
  450. if (local_env := getattr(local_self, 'env', None)) is not None:
  451. return local_env.cr
  452. if (cr := getattr(local_self, 'cr', None)) is not None:
  453. return cr
  454. try:
  455. from odoo.http import request # noqa: PLC0415
  456. request_env = request.env
  457. if request_env is not None and (cr := request_env.cr) is not None:
  458. return cr
  459. except RuntimeError:
  460. pass
  461. return None
  462. def _get_uid(frame) -> int | None:
  463. # try, in order: uid, user, self.env.uid
  464. if 'uid' in frame.f_locals:
  465. return frame.f_locals['uid']
  466. if 'user' in frame.f_locals:
  467. return int(frame.f_locals['user']) # user may be a record
  468. if (local_self := frame.f_locals.get('self')) is not None:
  469. if hasattr(local_self, 'env') and (uid := local_self.env.uid):
  470. return uid
  471. return None
  472. def _get_lang(frame, default_lang='') -> str:
  473. # get from: context.get('lang'), kwargs['context'].get('lang'),
  474. if local_context := frame.f_locals.get('context'):
  475. if lang := local_context.get('lang'):
  476. return lang
  477. if (local_kwargs := frame.f_locals.get('kwargs')) and (local_context := local_kwargs.get('context')):
  478. if lang := local_context.get('lang'):
  479. return lang
  480. # get from self.env
  481. log_level = logging.WARNING
  482. local_self = frame.f_locals.get('self')
  483. local_env = local_self is not None and getattr(local_self, 'env', None)
  484. if local_env:
  485. if lang := local_env.lang:
  486. return lang
  487. # we found the env, in case we fail, just log in debug
  488. log_level = logging.DEBUG
  489. # get from request?
  490. try:
  491. from odoo.http import request # noqa: PLC0415
  492. request_env = request.env
  493. if request_env and (lang := request_env.lang):
  494. return lang
  495. except RuntimeError:
  496. pass
  497. # Last resort: attempt to guess the language of the user
  498. # Pitfall: some operations are performed in sudo mode, and we
  499. # don't know the original uid, so the language may
  500. # be wrong when the admin language differs.
  501. cr = _get_cr(frame)
  502. uid = _get_uid(frame)
  503. if cr and uid:
  504. env = odoo.api.Environment(cr, uid, {})
  505. if lang := env['res.users'].context_get().get('lang'):
  506. return lang
  507. # fallback
  508. if default_lang:
  509. _logger.debug('no translation language detected, fallback to %s', default_lang)
  510. return default_lang
  511. # give up
  512. _logger.log(log_level, 'no translation language detected, skipping translation %s', frame, stack_info=True)
  513. return ''
  514. def _get_translation_source(stack_level: int, module: str = '', lang: str = '', default_lang: str = '') -> tuple[str, str]:
  515. if not (module and lang):
  516. frame = inspect.currentframe()
  517. for _index in range(stack_level + 1):
  518. frame = frame.f_back
  519. lang = lang or _get_lang(frame, default_lang)
  520. if lang and lang != 'en_US':
  521. return get_translated_module(module or frame), lang
  522. else:
  523. # we don't care about the module for 'en_US'
  524. return module or 'base', 'en_US'
  525. def get_text_alias(source: str, *args, **kwargs):
  526. assert not (args and kwargs)
  527. assert isinstance(source, str)
  528. module, lang = _get_translation_source(1)
  529. return get_translation(module, lang, source, args or kwargs)
  530. @functools.total_ordering
  531. class LazyGettext:
  532. """ Lazy code translated term.
  533. Similar to get_text_alias but the translation lookup will be done only at
  534. __str__ execution.
  535. This eases the search for terms to translate as lazy evaluated strings
  536. are declared early.
  537. A code using translated global variables such as:
  538. ```
  539. _lt = LazyTranslate(__name__)
  540. LABEL = _lt("User")
  541. def _compute_label(self):
  542. env = self.with_env(lang=self.partner_id.lang).env
  543. self.user_label = env._(LABEL)
  544. ```
  545. works as expected (unlike the classic get_text_alias implementation).
  546. """
  547. __slots__ = ('_args', '_default_lang', '_module', '_source')
  548. def __init__(self, source, *args, _module='', _default_lang='', **kwargs):
  549. assert not (args and kwargs)
  550. assert isinstance(source, str)
  551. self._source = source
  552. self._args = args or kwargs
  553. self._module = get_translated_module(_module or 2)
  554. self._default_lang = _default_lang
  555. def _translate(self, lang: str = '') -> str:
  556. module, lang = _get_translation_source(2, self._module, lang, default_lang=self._default_lang)
  557. return get_translation(module, lang, self._source, self._args)
  558. def __repr__(self):
  559. """ Show for the debugger"""
  560. args = {'_module': self._module, '_default_lang': self._default_lang, '_args': self._args}
  561. return f"_lt({self._source!r}, **{args!r})"
  562. def __str__(self):
  563. """ Translate."""
  564. return self._translate()
  565. def __eq__(self, other):
  566. """ Prevent using equal operators
  567. Prevent direct comparisons with ``self``.
  568. One should compare the translation of ``self._source`` as ``str(self) == X``.
  569. """
  570. raise NotImplementedError()
  571. def __hash__(self):
  572. raise NotImplementedError()
  573. def __lt__(self, other):
  574. raise NotImplementedError()
  575. def __add__(self, other):
  576. if isinstance(other, str):
  577. return self._translate() + other
  578. elif isinstance(other, LazyGettext):
  579. return self._translate() + other._translate()
  580. return NotImplemented
  581. def __radd__(self, other):
  582. if isinstance(other, str):
  583. return other + self._translate()
  584. return NotImplemented
  585. class LazyTranslate:
  586. """ Lazy translation template.
  587. Usage:
  588. ```
  589. _lt = LazyTranslate(__name__)
  590. MYSTR = _lt('Translate X')
  591. ```
  592. You may specify a `default_lang` to fallback to a given language on error
  593. """
  594. module: str
  595. default_lang: str
  596. def __init__(self, module: str, *, default_lang: str = '') -> None:
  597. self.module = module = get_translated_module(module or 2)
  598. # set the default lang to en_US for lazy translations in the base module
  599. self.default_lang = default_lang or ('en_US' if module == 'base' else '')
  600. def __call__(self, source: str, *args, **kwargs) -> LazyGettext:
  601. return LazyGettext(source, *args, **kwargs, _module=self.module, _default_lang=self.default_lang)
  602. _ = get_text_alias
  603. _lt = LazyGettext
  604. def quote(s):
  605. """Returns quoted PO term string, with special PO characters escaped"""
  606. assert r"\n" not in s, "Translation terms may not include escaped newlines ('\\n'), please use only literal newlines! (in '%s')" % s
  607. return '"%s"' % s.replace('\\','\\\\') \
  608. .replace('"','\\"') \
  609. .replace('\n', '\\n"\n"')
  610. re_escaped_char = re.compile(r"(\\.)")
  611. re_escaped_replacements = {'n': '\n', 't': '\t',}
  612. def _sub_replacement(match_obj):
  613. return re_escaped_replacements.get(match_obj.group(1)[1], match_obj.group(1)[1])
  614. def unquote(str):
  615. """Returns unquoted PO term string, with special PO characters unescaped"""
  616. return re_escaped_char.sub(_sub_replacement, str[1:-1])
  617. def TranslationFileReader(source, fileformat='po'):
  618. """ Iterate over translation file to return Odoo translation entries """
  619. if fileformat == 'csv':
  620. return CSVFileReader(source)
  621. if fileformat == 'po':
  622. return PoFileReader(source)
  623. _logger.info('Bad file format: %s', fileformat)
  624. raise Exception(_('Bad file format: %s', fileformat))
  625. class CSVFileReader:
  626. def __init__(self, source):
  627. _reader = codecs.getreader('utf-8')
  628. self.source = csv.DictReader(_reader(source), quotechar='"', delimiter=',')
  629. self.prev_code_src = ""
  630. def __iter__(self):
  631. for entry in self.source:
  632. # determine <module>.<imd_name> from res_id
  633. if entry["res_id"] and entry["res_id"].isnumeric():
  634. # res_id is an id or line number
  635. entry["res_id"] = int(entry["res_id"])
  636. elif not entry.get("imd_name"):
  637. # res_id is an external id and must follow <module>.<name>
  638. entry["module"], entry["imd_name"] = entry["res_id"].split(".")
  639. entry["res_id"] = None
  640. if entry["type"] == "model" or entry["type"] == "model_terms":
  641. entry["imd_model"] = entry["name"].partition(',')[0]
  642. if entry["type"] == "code":
  643. if entry["src"] == self.prev_code_src:
  644. # skip entry due to unicity constrain on code translations
  645. continue
  646. self.prev_code_src = entry["src"]
  647. yield entry
  648. class PoFileReader:
  649. """ Iterate over po file to return Odoo translation entries """
  650. def __init__(self, source):
  651. def get_pot_path(source_name):
  652. # when fileobj is a TemporaryFile, its name is an inter in P3, a string in P2
  653. if isinstance(source_name, str) and source_name.endswith('.po'):
  654. # Normally the path looks like /path/to/xxx/i18n/lang.po
  655. # and we try to find the corresponding
  656. # /path/to/xxx/i18n/xxx.pot file.
  657. # (Sometimes we have 'i18n_extra' instead of just 'i18n')
  658. path = Path(source_name)
  659. filename = path.parent.parent.name + '.pot'
  660. pot_path = path.with_name(filename)
  661. return pot_path.exists() and str(pot_path) or False
  662. return False
  663. # polib accepts a path or the file content as a string, not a fileobj
  664. if isinstance(source, str):
  665. self.pofile = polib.pofile(source)
  666. pot_path = get_pot_path(source)
  667. else:
  668. # either a BufferedIOBase or result from NamedTemporaryFile
  669. self.pofile = polib.pofile(source.read().decode())
  670. pot_path = get_pot_path(source.name)
  671. if pot_path:
  672. # Make a reader for the POT file
  673. # (Because the POT comments are correct on GitHub but the
  674. # PO comments tends to be outdated. See LP bug 933496.)
  675. self.pofile.merge(polib.pofile(pot_path))
  676. def __iter__(self):
  677. for entry in self.pofile:
  678. if entry.obsolete:
  679. continue
  680. # in case of moduleS keep only the first
  681. match = re.match(r"(module[s]?): (\w+)", entry.comment)
  682. _, module = match.groups()
  683. comments = "\n".join([c for c in entry.comment.split('\n') if not c.startswith('module:')])
  684. source = entry.msgid
  685. translation = entry.msgstr
  686. found_code_occurrence = False
  687. for occurrence, line_number in entry.occurrences:
  688. match = re.match(r'(model|model_terms):([\w.]+),([\w]+):(\w+)\.([^ ]+)', occurrence)
  689. if match:
  690. type, model_name, field_name, module, xmlid = match.groups()
  691. yield {
  692. 'type': type,
  693. 'imd_model': model_name,
  694. 'name': model_name+','+field_name,
  695. 'imd_name': xmlid,
  696. 'res_id': None,
  697. 'src': source,
  698. 'value': translation,
  699. 'comments': comments,
  700. 'module': module,
  701. }
  702. continue
  703. match = re.match(r'(code):([\w/.]+)', occurrence)
  704. if match:
  705. type, name = match.groups()
  706. if found_code_occurrence:
  707. # unicity constrain on code translation
  708. continue
  709. found_code_occurrence = True
  710. yield {
  711. 'type': type,
  712. 'name': name,
  713. 'src': source,
  714. 'value': translation,
  715. 'comments': comments,
  716. 'res_id': int(line_number),
  717. 'module': module,
  718. }
  719. continue
  720. match = re.match(r'(selection):([\w.]+),([\w]+)', occurrence)
  721. if match:
  722. _logger.info("Skipped deprecated occurrence %s", occurrence)
  723. continue
  724. match = re.match(r'(sql_constraint|constraint):([\w.]+)', occurrence)
  725. if match:
  726. _logger.info("Skipped deprecated occurrence %s", occurrence)
  727. continue
  728. _logger.error("malformed po file: unknown occurrence: %s", occurrence)
  729. def TranslationFileWriter(target, fileformat='po', lang=None):
  730. """ Iterate over translation file to return Odoo translation entries """
  731. if fileformat == 'csv':
  732. return CSVFileWriter(target)
  733. if fileformat == 'po':
  734. return PoFileWriter(target, lang=lang)
  735. if fileformat == 'tgz':
  736. return TarFileWriter(target, lang=lang)
  737. raise Exception(_('Unrecognized extension: must be one of '
  738. '.csv, .po, or .tgz (received .%s).') % fileformat)
  739. _writer = codecs.getwriter('utf-8')
  740. class CSVFileWriter:
  741. def __init__(self, target):
  742. self.writer = csv.writer(_writer(target), dialect='UNIX')
  743. # write header first
  744. self.writer.writerow(("module","type","name","res_id","src","value","comments"))
  745. def write_rows(self, rows):
  746. for module, type, name, res_id, src, trad, comments in rows:
  747. comments = '\n'.join(comments)
  748. self.writer.writerow((module, type, name, res_id, src, trad, comments))
  749. class PoFileWriter:
  750. """ Iterate over po file to return Odoo translation entries """
  751. def __init__(self, target, lang):
  752. self.buffer = target
  753. self.lang = lang
  754. self.po = polib.POFile()
  755. def write_rows(self, rows):
  756. # we now group the translations by source. That means one translation per source.
  757. grouped_rows = {}
  758. modules = set()
  759. for module, type, name, res_id, src, trad, comments in rows:
  760. row = grouped_rows.setdefault(src, {})
  761. row.setdefault('modules', set()).add(module)
  762. if not row.get('translation') and trad != src:
  763. row['translation'] = trad
  764. row.setdefault('tnrs', []).append((type, name, res_id))
  765. row.setdefault('comments', set()).update(comments)
  766. modules.add(module)
  767. for src, row in sorted(grouped_rows.items()):
  768. if not self.lang:
  769. # translation template, so no translation value
  770. row['translation'] = ''
  771. elif not row.get('translation'):
  772. row['translation'] = ''
  773. self.add_entry(sorted(row['modules']), sorted(row['tnrs']), src, row['translation'], sorted(row['comments']))
  774. import odoo.release as release
  775. self.po.header = "Translation of %s.\n" \
  776. "This file contains the translation of the following modules:\n" \
  777. "%s" % (release.description, ''.join("\t* %s\n" % m for m in modules))
  778. now = datetime.utcnow().strftime('%Y-%m-%d %H:%M+0000')
  779. self.po.metadata = {
  780. 'Project-Id-Version': "%s %s" % (release.description, release.version),
  781. 'Report-Msgid-Bugs-To': '',
  782. 'POT-Creation-Date': now,
  783. 'PO-Revision-Date': now,
  784. 'Last-Translator': '',
  785. 'Language-Team': '',
  786. 'MIME-Version': '1.0',
  787. 'Content-Type': 'text/plain; charset=UTF-8',
  788. 'Content-Transfer-Encoding': '',
  789. 'Plural-Forms': '',
  790. }
  791. # buffer expects bytes
  792. self.buffer.write(str(self.po).encode())
  793. def add_entry(self, modules, tnrs, source, trad, comments=None):
  794. entry = polib.POEntry(
  795. msgid=source,
  796. msgstr=trad,
  797. )
  798. plural = len(modules) > 1 and 's' or ''
  799. entry.comment = "module%s: %s" % (plural, ', '.join(modules))
  800. if comments:
  801. entry.comment += "\n" + "\n".join(comments)
  802. occurrences = OrderedSet()
  803. for type_, *ref in tnrs:
  804. if type_ == "code":
  805. fpath, lineno = ref
  806. name = f"code:{fpath}"
  807. # lineno is set to 0 to avoid creating diff in PO files every
  808. # time the code is moved around
  809. lineno = "0"
  810. else:
  811. field_name, xmlid = ref
  812. name = f"{type_}:{field_name}:{xmlid}"
  813. lineno = None # no lineno for model/model_terms sources
  814. occurrences.add((name, lineno))
  815. entry.occurrences = list(occurrences)
  816. self.po.append(entry)
  817. class TarFileWriter:
  818. def __init__(self, target, lang):
  819. self.tar = tarfile.open(fileobj=target, mode='w|gz')
  820. self.lang = lang
  821. def write_rows(self, rows):
  822. rows_by_module = defaultdict(list)
  823. for row in rows:
  824. module = row[0]
  825. rows_by_module[module].append(row)
  826. for mod, modrows in rows_by_module.items():
  827. with io.BytesIO() as buf:
  828. po = PoFileWriter(buf, lang=self.lang)
  829. po.write_rows(modrows)
  830. buf.seek(0)
  831. info = tarfile.TarInfo(
  832. join(mod, 'i18n', '{basename}.{ext}'.format(
  833. basename=self.lang or mod,
  834. ext='po' if self.lang else 'pot',
  835. )))
  836. # addfile will read <size> bytes from the buffer so
  837. # size *must* be set first
  838. info.size = len(buf.getvalue())
  839. self.tar.addfile(info, fileobj=buf)
  840. self.tar.close()
  841. # Methods to export the translation file
  842. def trans_export(lang, modules, buffer, format, cr):
  843. reader = TranslationModuleReader(cr, modules=modules, lang=lang)
  844. writer = TranslationFileWriter(buffer, fileformat=format, lang=lang)
  845. writer.write_rows(reader)
  846. # pylint: disable=redefined-builtin
  847. def trans_export_records(lang, model_name, ids, buffer, format, cr):
  848. reader = TranslationRecordReader(cr, model_name, ids, lang=lang)
  849. writer = TranslationFileWriter(buffer, fileformat=format, lang=lang)
  850. writer.write_rows(reader)
  851. def _push(callback, term, source_line):
  852. """ Sanity check before pushing translation terms """
  853. term = (term or "").strip()
  854. # Avoid non-char tokens like ':' '...' '.00' etc.
  855. if len(term) > 8 or any(x.isalpha() for x in term):
  856. callback(term, source_line)
  857. def _extract_translatable_qweb_terms(element, callback):
  858. """ Helper method to walk an etree document representing
  859. a QWeb template, and call ``callback(term)`` for each
  860. translatable term that is found in the document.
  861. :param etree._Element element: root of etree document to extract terms from
  862. :param Callable callback: a callable in the form ``f(term, source_line)``,
  863. that will be called for each extracted term.
  864. """
  865. # not using elementTree.iterparse because we need to skip sub-trees in case
  866. # the ancestor element had a reason to be skipped
  867. for el in element:
  868. if isinstance(el, SKIPPED_ELEMENT_TYPES): continue
  869. if (el.tag.lower() not in SKIPPED_ELEMENTS
  870. and "t-js" not in el.attrib
  871. and not (el.tag == 'attribute' and el.get('name') not in TRANSLATED_ATTRS)
  872. and el.get("t-translation", '').strip() != "off"):
  873. _push(callback, el.text, el.sourceline)
  874. # heuristic: tags with names starting with an uppercase letter are
  875. # component nodes
  876. is_component = el.tag[0].isupper() or "t-component" in el.attrib or "t-set-slot" in el.attrib
  877. for attr in el.attrib:
  878. if (not is_component and attr in TRANSLATED_ATTRS) or (is_component and attr.endswith(".translate")):
  879. _push(callback, el.attrib[attr], el.sourceline)
  880. _extract_translatable_qweb_terms(el, callback)
  881. _push(callback, el.tail, el.sourceline)
  882. def babel_extract_qweb(fileobj, keywords, comment_tags, options):
  883. """Babel message extractor for qweb template files.
  884. :param fileobj: the file-like object the messages should be extracted from
  885. :param keywords: a list of keywords (i.e. function names) that should
  886. be recognized as translation functions
  887. :param comment_tags: a list of translator tags to search for and
  888. include in the results
  889. :param options: a dictionary of additional options (optional)
  890. :return: an iterator over ``(lineno, funcname, message, comments)``
  891. tuples
  892. :rtype: Iterable
  893. """
  894. result = []
  895. def handle_text(text, lineno):
  896. result.append((lineno, None, text, []))
  897. tree = etree.parse(fileobj)
  898. _extract_translatable_qweb_terms(tree.getroot(), handle_text)
  899. return result
  900. def extract_formula_terms(formula):
  901. """Extract strings in a spreadsheet formula which are arguments to '_t' functions
  902. >>> extract_formula_terms('=_t("Hello") + _t("Raoul")')
  903. ["Hello", "Raoul"]
  904. """
  905. tokens = generate_tokens(io.StringIO(formula).readline)
  906. tokens = (token for token in tokens if token.type not in {NEWLINE, INDENT, DEDENT})
  907. for t1 in tokens:
  908. if t1.string != '_t':
  909. continue
  910. t2 = next(tokens, None)
  911. if t2 and t2.string == '(':
  912. t3 = next(tokens, None)
  913. t4 = next(tokens, None)
  914. if t4 and t4.string == ')' and t3 and t3.type == STRING:
  915. yield t3.string[1:][:-1] # strip leading and trailing quotes
  916. def extract_spreadsheet_terms(fileobj, keywords, comment_tags, options):
  917. """Babel message extractor for spreadsheet data files.
  918. :param fileobj: the file-like object the messages should be extracted from
  919. :param keywords: a list of keywords (i.e. function names) that should
  920. be recognized as translation functions
  921. :param comment_tags: a list of translator tags to search for and
  922. include in the results
  923. :param options: a dictionary of additional options (optional)
  924. :return: an iterator over ``(lineno, funcname, message, comments)``
  925. tuples
  926. """
  927. terms = set()
  928. data = json.load(fileobj)
  929. for sheet in data.get('sheets', []):
  930. for cell in sheet['cells'].values():
  931. content = cell.get('content', '')
  932. if content.startswith('='):
  933. terms.update(extract_formula_terms(content))
  934. else:
  935. markdown_link = re.fullmatch(r'\[(.+)\]\(.+\)', content)
  936. if markdown_link:
  937. terms.add(markdown_link[1])
  938. for figure in sheet['figures']:
  939. if figure['tag'] == 'chart':
  940. title = figure['data']['title']
  941. if isinstance(title, str):
  942. terms.add(title)
  943. elif 'text' in title:
  944. terms.add(title['text'])
  945. if 'axesDesign' in figure['data']:
  946. terms.update(
  947. axes.get('title', {}).get('text', '') for axes in figure['data']['axesDesign'].values()
  948. )
  949. if 'baselineDescr' in figure['data']:
  950. terms.add(figure['data']['baselineDescr'])
  951. terms.update(global_filter['label'] for global_filter in data.get('globalFilters', []))
  952. return (
  953. (0, None, term, [])
  954. for term in terms
  955. if any(x.isalpha() for x in term)
  956. )
  957. ImdInfo = namedtuple('ExternalId', ['name', 'model', 'res_id', 'module'])
  958. class TranslationReader:
  959. def __init__(self, cr, lang=None):
  960. self._cr = cr
  961. self._lang = lang or 'en_US'
  962. self.env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
  963. self._to_translate = []
  964. def __iter__(self):
  965. for module, source, name, res_id, ttype, comments, _record_id, value in self._to_translate:
  966. yield (module, ttype, name, res_id, source, value, comments)
  967. def _push_translation(self, module, ttype, name, res_id, source, comments=None, record_id=None, value=None):
  968. """ Insert a translation that will be used in the file generation
  969. In po file will create an entry
  970. #: <ttype>:<name>:<res_id>
  971. #, <comment>
  972. msgid "<source>"
  973. record_id is the database id of the record being translated
  974. """
  975. # empty and one-letter terms are ignored, they probably are not meant to be
  976. # translated, and would be very hard to translate anyway.
  977. sanitized_term = (source or '').strip()
  978. # remove non-alphanumeric chars
  979. sanitized_term = re.sub(r'\W+', '', sanitized_term)
  980. if not sanitized_term or len(sanitized_term) <= 1:
  981. return
  982. self._to_translate.append((module, source, name, res_id, ttype, tuple(comments or ()), record_id, value))
  983. def _export_imdinfo(self, model: str, imd_per_id: dict[int, ImdInfo]):
  984. records = self._get_translatable_records(imd_per_id.values())
  985. if not records:
  986. return
  987. env = records.env
  988. for record in records.with_context(check_translations=True):
  989. module = imd_per_id[record.id].module
  990. xml_name = "%s.%s" % (module, imd_per_id[record.id].name)
  991. for field_name, field in record._fields.items():
  992. # ir_actions_actions.name is filtered because unlike other inherited fields,
  993. # this field is inherited as postgresql inherited columns.
  994. # From our business perspective, the parent column is no need to be translated,
  995. # but it is need to be set to jsonb column, since the child columns need to be translated
  996. # And export the parent field may make one value to be translated twice in transifex
  997. #
  998. # Some ir_model_fields.field_description are filtered
  999. # because their fields have falsy attribute export_string_translation
  1000. if (
  1001. not (field.translate and field.store)
  1002. or str(field) == 'ir.actions.actions.name'
  1003. or (str(field) == 'ir.model.fields.field_description'
  1004. and not env[record.model]._fields[record.name].export_string_translation)
  1005. ):
  1006. continue
  1007. name = model + "," + field_name
  1008. value_en = record[field_name] or ''
  1009. value_lang = record.with_context(lang=self._lang)[field_name] or ''
  1010. trans_type = 'model_terms' if callable(field.translate) else 'model'
  1011. try:
  1012. translation_dictionary = field.get_translation_dictionary(value_en, {self._lang: value_lang})
  1013. except Exception:
  1014. _logger.exception("Failed to extract terms from %s %s", xml_name, name)
  1015. continue
  1016. for term_en, term_langs in translation_dictionary.items():
  1017. term_lang = term_langs.get(self._lang)
  1018. self._push_translation(module, trans_type, name, xml_name, term_en, record_id=record.id, value=term_lang if term_lang != term_en else '')
  1019. def _get_translatable_records(self, imd_records):
  1020. """ Filter the records that are translatable
  1021. A record is considered as untranslatable if:
  1022. - it does not exist
  1023. - the model is flagged with _translate=False
  1024. - it is a field of a model flagged with _translate=False
  1025. - it is a selection of a field of a model flagged with _translate=False
  1026. :param records: a list of namedtuple ImdInfo belonging to the same model
  1027. """
  1028. model = next(iter(imd_records)).model
  1029. if model not in self.env:
  1030. _logger.error("Unable to find object %r", model)
  1031. return self.env["_unknown"].browse()
  1032. if not self.env[model]._translate:
  1033. return self.env[model].browse()
  1034. res_ids = [r.res_id for r in imd_records]
  1035. records = self.env[model].browse(res_ids).exists()
  1036. if len(records) < len(res_ids):
  1037. missing_ids = set(res_ids) - set(records.ids)
  1038. missing_records = [f"{r.module}.{r.name}" for r in imd_records if r.res_id in missing_ids]
  1039. _logger.warning("Unable to find records of type %r with external ids %s", model, ', '.join(missing_records))
  1040. if not records:
  1041. return records
  1042. if model == 'ir.model.fields.selection':
  1043. fields = defaultdict(list)
  1044. for selection in records:
  1045. fields[selection.field_id] = selection
  1046. for field, selection in fields.items():
  1047. field_name = field.name
  1048. field_model = self.env.get(field.model)
  1049. if (field_model is None or not field_model._translate or
  1050. field_name not in field_model._fields):
  1051. # the selection is linked to a model with _translate=False, remove it
  1052. records -= selection
  1053. elif model == 'ir.model.fields':
  1054. for field in records:
  1055. field_name = field.name
  1056. field_model = self.env.get(field.model)
  1057. if (field_model is None or not field_model._translate or
  1058. field_name not in field_model._fields):
  1059. # the field is linked to a model with _translate=False, remove it
  1060. records -= field
  1061. return records
  1062. class TranslationRecordReader(TranslationReader):
  1063. """ Retrieve translations for specified records, the reader will
  1064. 1. create external ids for records without external ids
  1065. 2. export translations for stored translated and inherited translated fields
  1066. :param cr: cursor to database to export
  1067. :param model_name: model_name for the records to export
  1068. :param ids: ids of the records to export
  1069. :param field_names: field names to export, if not set, export all translatable fields
  1070. :param lang: language code to retrieve the translations retrieve source terms only if not set
  1071. """
  1072. def __init__(self, cr, model_name, ids, field_names=None, lang=None):
  1073. super().__init__(cr, lang)
  1074. self._records = self.env[model_name].browse(ids)
  1075. self._field_names = field_names or list(self._records._fields.keys())
  1076. self._export_translatable_records(self._records, self._field_names)
  1077. def _export_translatable_records(self, records, field_names):
  1078. """ Export translations of all stored/inherited translated fields. Create external id if needed. """
  1079. if not records:
  1080. return
  1081. fields = records._fields
  1082. if records._inherits:
  1083. inherited_fields = defaultdict(list)
  1084. for field_name in field_names:
  1085. field = records._fields[field_name]
  1086. if field.translate and not field.store and field.inherited_field:
  1087. inherited_fields[field.inherited_field.model_name].append(field_name)
  1088. for parent_mname, parent_fname in records._inherits.items():
  1089. if parent_mname in inherited_fields:
  1090. self._export_translatable_records(records[parent_fname], inherited_fields[parent_mname])
  1091. if not any(fields[field_name].translate and fields[field_name].store for field_name in field_names):
  1092. return
  1093. records._BaseModel__ensure_xml_id()
  1094. model_name = records._name
  1095. query = """SELECT min(concat(module, '.', name)), res_id
  1096. FROM ir_model_data
  1097. WHERE model = %s
  1098. AND res_id = ANY(%s)
  1099. GROUP BY model, res_id"""
  1100. self._cr.execute(query, (model_name, records.ids))
  1101. imd_per_id = {
  1102. res_id: ImdInfo((tmp := module_xml_name.split('.', 1))[1], model_name, res_id, tmp[0])
  1103. for module_xml_name, res_id in self._cr.fetchall()
  1104. }
  1105. self._export_imdinfo(model_name, imd_per_id)
  1106. class TranslationModuleReader(TranslationReader):
  1107. """ Retrieve translated records per module
  1108. :param cr: cursor to database to export
  1109. :param modules: list of modules to filter the exported terms, can be ['all']
  1110. records with no external id are always ignored
  1111. :param lang: language code to retrieve the translations
  1112. retrieve source terms only if not set
  1113. """
  1114. def __init__(self, cr, modules=None, lang=None):
  1115. super().__init__(cr, lang)
  1116. self._modules = modules or ['all']
  1117. self._path_list = [(path, True) for path in odoo.addons.__path__]
  1118. self._installed_modules = [
  1119. m['name']
  1120. for m in self.env['ir.module.module'].search_read([('state', '=', 'installed')], fields=['name'])
  1121. ]
  1122. self._export_translatable_records()
  1123. self._export_translatable_resources()
  1124. def _export_translatable_records(self):
  1125. """ Export translations of all translated records having an external id """
  1126. query = """SELECT min(name), model, res_id, module
  1127. FROM ir_model_data
  1128. WHERE module = ANY(%s)
  1129. GROUP BY model, res_id, module
  1130. ORDER BY module, model, min(name)"""
  1131. if 'all' not in self._modules:
  1132. query_param = list(self._modules)
  1133. else:
  1134. query_param = self._installed_modules
  1135. self._cr.execute(query, (query_param,))
  1136. records_per_model = defaultdict(dict)
  1137. for (xml_name, model, res_id, module) in self._cr.fetchall():
  1138. records_per_model[model][res_id] = ImdInfo(xml_name, model, res_id, module)
  1139. for model, imd_per_id in records_per_model.items():
  1140. self._export_imdinfo(model, imd_per_id)
  1141. def _get_module_from_path(self, path):
  1142. for (mp, rec) in self._path_list:
  1143. mp = os.path.join(mp, '')
  1144. dirname = os.path.join(os.path.dirname(path), '')
  1145. if rec and path.startswith(mp) and dirname != mp:
  1146. path = path[len(mp):]
  1147. return path.split(os.path.sep)[0]
  1148. return 'base' # files that are not in a module are considered as being in 'base' module
  1149. def _verified_module_filepaths(self, fname, path, root):
  1150. fabsolutepath = join(root, fname)
  1151. frelativepath = fabsolutepath[len(path):]
  1152. display_path = "addons%s" % frelativepath
  1153. module = self._get_module_from_path(fabsolutepath)
  1154. if ('all' in self._modules or module in self._modules) and module in self._installed_modules:
  1155. if os.path.sep != '/':
  1156. display_path = display_path.replace(os.path.sep, '/')
  1157. return module, fabsolutepath, frelativepath, display_path
  1158. return None, None, None, None
  1159. def _babel_extract_terms(self, fname, path, root, extract_method="python", trans_type='code',
  1160. extra_comments=None, extract_keywords={'_': None}):
  1161. module, fabsolutepath, _, display_path = self._verified_module_filepaths(fname, path, root)
  1162. if not module:
  1163. return
  1164. extra_comments = extra_comments or []
  1165. src_file = file_open(fabsolutepath, 'rb')
  1166. options = {}
  1167. if extract_method == 'python':
  1168. options['encoding'] = 'UTF-8'
  1169. translations = code_translations.get_python_translations(module, self._lang)
  1170. else:
  1171. translations = code_translations.get_web_translations(module, self._lang)
  1172. translations = {tran['id']: tran['string'] for tran in translations['messages']}
  1173. try:
  1174. for extracted in extract.extract(extract_method, src_file, keywords=extract_keywords, options=options):
  1175. # Babel 0.9.6 yields lineno, message, comments
  1176. # Babel 1.3 yields lineno, message, comments, context
  1177. lineno, message, comments = extracted[:3]
  1178. value = translations.get(message, '')
  1179. self._push_translation(module, trans_type, display_path, lineno,
  1180. message, comments + extra_comments, value=value)
  1181. except Exception:
  1182. _logger.exception("Failed to extract terms from %s", fabsolutepath)
  1183. finally:
  1184. src_file.close()
  1185. def _export_translatable_resources(self):
  1186. """ Export translations for static terms
  1187. This will include:
  1188. - the python strings marked with _() or _lt()
  1189. - the javascript strings marked with _t() inside static/src/js/
  1190. - the strings inside Qweb files inside static/src/xml/
  1191. - the spreadsheet data files
  1192. """
  1193. # Also scan these non-addon paths
  1194. for bin_path in ['osv', 'report', 'modules', 'service', 'tools']:
  1195. self._path_list.append((os.path.join(config['root_path'], bin_path), True))
  1196. # non-recursive scan for individual files in root directory but without
  1197. # scanning subdirectories that may contain addons
  1198. self._path_list.append((config['root_path'], False))
  1199. _logger.debug("Scanning modules at paths: %s", self._path_list)
  1200. spreadsheet_files_regex = re.compile(r".*_dashboard(\.osheet)?\.json$")
  1201. for (path, recursive) in self._path_list:
  1202. _logger.debug("Scanning files of modules at %s", path)
  1203. for root, dummy, files in os.walk(path, followlinks=True):
  1204. for fname in fnmatch.filter(files, '*.py'):
  1205. self._babel_extract_terms(fname, path, root, 'python',
  1206. extra_comments=[PYTHON_TRANSLATION_COMMENT],
  1207. extract_keywords={'_': None, '_lt': None})
  1208. if fnmatch.fnmatch(root, '*/static/src*'):
  1209. # Javascript source files
  1210. for fname in fnmatch.filter(files, '*.js'):
  1211. self._babel_extract_terms(fname, path, root, 'javascript',
  1212. extra_comments=[JAVASCRIPT_TRANSLATION_COMMENT],
  1213. extract_keywords={'_t': None})
  1214. # QWeb template files
  1215. for fname in fnmatch.filter(files, '*.xml'):
  1216. self._babel_extract_terms(fname, path, root, 'odoo.tools.translate:babel_extract_qweb',
  1217. extra_comments=[JAVASCRIPT_TRANSLATION_COMMENT])
  1218. if fnmatch.fnmatch(root, '*/data/*'):
  1219. for fname in filter(spreadsheet_files_regex.match, files):
  1220. self._babel_extract_terms(fname, path, root, 'odoo.tools.translate:extract_spreadsheet_terms',
  1221. extra_comments=[JAVASCRIPT_TRANSLATION_COMMENT])
  1222. if not recursive:
  1223. # due to topdown, first iteration is in first level
  1224. break
  1225. def DeepDefaultDict():
  1226. return defaultdict(DeepDefaultDict)
  1227. class TranslationImporter:
  1228. """ Helper object for importing translation files to a database.
  1229. This class provides a convenient API to load the translations from many
  1230. files and import them all at once, which helps speeding up the whole import.
  1231. """
  1232. def __init__(self, cr, verbose=True):
  1233. self.cr = cr
  1234. self.verbose = verbose
  1235. self.env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
  1236. # {model_name: {field_name: {xmlid: {lang: value}}}}
  1237. self.model_translations = DeepDefaultDict()
  1238. # {model_name: {field_name: {xmlid: {src: {lang: value}}}}}
  1239. self.model_terms_translations = DeepDefaultDict()
  1240. def load_file(self, filepath, lang, xmlids=None):
  1241. """ Load translations from the given file path.
  1242. :param filepath: file path to open
  1243. :param lang: language code of the translations contained in the file;
  1244. the language must be present and activated in the database
  1245. :param xmlids: if given, only translations for records with xmlid in xmlids will be loaded
  1246. """
  1247. with suppress(FileNotFoundError), file_open(filepath, mode='rb', env=self.env) as fileobj:
  1248. _logger.info('loading base translation file %s for language %s', filepath, lang)
  1249. fileformat = os.path.splitext(filepath)[-1][1:].lower()
  1250. self.load(fileobj, fileformat, lang, xmlids=xmlids)
  1251. def load(self, fileobj, fileformat, lang, xmlids=None):
  1252. """Load translations from the given file object.
  1253. :param fileobj: buffer open to a translation file
  1254. :param fileformat: format of the `fielobj` file, one of 'po' or 'csv'
  1255. :param lang: language code of the translations contained in `fileobj`;
  1256. the language must be present and activated in the database
  1257. :param xmlids: if given, only translations for records with xmlid in xmlids will be loaded
  1258. """
  1259. if self.verbose:
  1260. _logger.info('loading translation file for language %s', lang)
  1261. if not self.env['res.lang']._lang_get(lang):
  1262. _logger.error("Couldn't read translation for lang '%s', language not found", lang)
  1263. return None
  1264. try:
  1265. fileobj.seek(0)
  1266. reader = TranslationFileReader(fileobj, fileformat=fileformat)
  1267. self._load(reader, lang, xmlids)
  1268. except IOError:
  1269. iso_lang = get_iso_codes(lang)
  1270. filename = '[lang: %s][format: %s]' % (iso_lang or 'new', fileformat)
  1271. _logger.exception("couldn't read translation file %s", filename)
  1272. def _load(self, reader, lang, xmlids=None):
  1273. if xmlids and not isinstance(xmlids, set):
  1274. xmlids = set(xmlids)
  1275. for row in reader:
  1276. if not row.get('value') or not row.get('src'): # ignore empty translations
  1277. continue
  1278. if row.get('type') == 'code': # ignore code translations
  1279. continue
  1280. model_name = row.get('imd_model')
  1281. module_name = row['module']
  1282. if model_name not in self.env:
  1283. continue
  1284. field_name = row['name'].split(',')[1]
  1285. field = self.env[model_name]._fields.get(field_name)
  1286. if not field or not field.translate or not field.store:
  1287. continue
  1288. xmlid = module_name + '.' + row['imd_name']
  1289. if xmlids and xmlid not in xmlids:
  1290. continue
  1291. if row.get('type') == 'model' and field.translate is True:
  1292. self.model_translations[model_name][field_name][xmlid][lang] = row['value']
  1293. elif row.get('type') == 'model_terms' and callable(field.translate):
  1294. self.model_terms_translations[model_name][field_name][xmlid][row['src']][lang] = row['value']
  1295. def save(self, overwrite=False, force_overwrite=False):
  1296. """ Save translations to the database.
  1297. For a record with 'noupdate' in ``ir_model_data``, its existing translations
  1298. will be overwritten if ``force_overwrite or (not noupdate and overwrite)``.
  1299. An existing translation means:
  1300. * model translation: the ``jsonb`` value in database has the language code as key;
  1301. * model terms translation: the term value in the language is different from the term value in ``en_US``.
  1302. """
  1303. if not self.model_translations and not self.model_terms_translations:
  1304. return
  1305. cr = self.cr
  1306. env = self.env
  1307. env.flush_all()
  1308. for model_name, model_dictionary in self.model_terms_translations.items():
  1309. Model = env[model_name]
  1310. model_table = Model._table
  1311. fields = Model._fields
  1312. # field_name, {xmlid: {src: {lang: value}}}
  1313. for field_name, field_dictionary in model_dictionary.items():
  1314. field = fields.get(field_name)
  1315. for sub_xmlids in cr.split_for_in_conditions(field_dictionary.keys()):
  1316. # [module_name, imd_name, module_name, imd_name, ...]
  1317. params = []
  1318. for xmlid in sub_xmlids:
  1319. params.extend(xmlid.split('.', maxsplit=1))
  1320. cr.execute(f'''
  1321. SELECT m.id, imd.module || '.' || imd.name, m."{field_name}", imd.noupdate
  1322. FROM "{model_table}" m, "ir_model_data" imd
  1323. WHERE m.id = imd.res_id
  1324. AND ({" OR ".join(["(imd.module = %s AND imd.name = %s)"] * (len(params) // 2))})
  1325. ''', params)
  1326. # [id, translations, id, translations, ...]
  1327. params = []
  1328. for id_, xmlid, values, noupdate in cr.fetchall():
  1329. if not values:
  1330. continue
  1331. _value_en = values.get('_en_US', values['en_US'])
  1332. if not _value_en:
  1333. continue
  1334. # {src: {lang: value}}
  1335. record_dictionary = field_dictionary[xmlid]
  1336. langs = {lang for translations in record_dictionary.values() for lang in translations.keys()}
  1337. translation_dictionary = field.get_translation_dictionary(
  1338. _value_en,
  1339. {
  1340. k: values.get(f'_{k}', v)
  1341. for k, v in values.items()
  1342. if k in langs
  1343. }
  1344. )
  1345. if force_overwrite or (not noupdate and overwrite):
  1346. # overwrite existing translations
  1347. for term_en, translations in record_dictionary.items():
  1348. translation_dictionary[term_en].update(translations)
  1349. else:
  1350. # keep existing translations
  1351. for term_en, translations in record_dictionary.items():
  1352. translations.update({k: v for k, v in translation_dictionary[term_en].items() if v != term_en})
  1353. translation_dictionary[term_en] = translations
  1354. for lang in langs:
  1355. # translate and confirm model_terms translations
  1356. values[lang] = field.translate(lambda term: translation_dictionary.get(term, {}).get(lang), _value_en)
  1357. values.pop(f'_{lang}', None)
  1358. params.extend((id_, Json(values)))
  1359. if params:
  1360. env.cr.execute(f"""
  1361. UPDATE "{model_table}" AS m
  1362. SET "{field_name}" = t.value
  1363. FROM (
  1364. VALUES {', '.join(['(%s, %s::jsonb)'] * (len(params) // 2))}
  1365. ) AS t(id, value)
  1366. WHERE m.id = t.id
  1367. """, params)
  1368. self.model_terms_translations.clear()
  1369. for model_name, model_dictionary in self.model_translations.items():
  1370. Model = env[model_name]
  1371. model_table = Model._table
  1372. for field_name, field_dictionary in model_dictionary.items():
  1373. for sub_field_dictionary in cr.split_for_in_conditions(field_dictionary.items()):
  1374. # [xmlid, translations, xmlid, translations, ...]
  1375. params = []
  1376. for xmlid, translations in sub_field_dictionary:
  1377. params.extend([*xmlid.split('.', maxsplit=1), Json(translations)])
  1378. if not force_overwrite:
  1379. value_query = f"""CASE WHEN {overwrite} IS TRUE AND imd.noupdate IS FALSE
  1380. THEN m."{field_name}" || t.value
  1381. ELSE t.value || m."{field_name}"END"""
  1382. else:
  1383. value_query = f'm."{field_name}" || t.value'
  1384. env.cr.execute(f"""
  1385. UPDATE "{model_table}" AS m
  1386. SET "{field_name}" = {value_query}
  1387. FROM (
  1388. VALUES {', '.join(['(%s, %s, %s::jsonb)'] * (len(params) // 3))}
  1389. ) AS t(imd_module, imd_name, value)
  1390. JOIN "ir_model_data" AS imd
  1391. ON imd."model" = '{model_name}' AND imd.name = t.imd_name AND imd.module = t.imd_module
  1392. WHERE imd."res_id" = m."id"
  1393. """, params)
  1394. self.model_translations.clear()
  1395. env.invalidate_all()
  1396. env.registry.clear_cache()
  1397. if self.verbose:
  1398. _logger.info("translations are loaded successfully")
  1399. def trans_load(cr, filepath, lang, verbose=True, overwrite=False):
  1400. warnings.warn('The function trans_load is deprecated in favor of TranslationImporter', DeprecationWarning)
  1401. translation_importer = TranslationImporter(cr, verbose=verbose)
  1402. translation_importer.load_file(filepath, lang)
  1403. translation_importer.save(overwrite=overwrite)
  1404. def trans_load_data(cr, fileobj, fileformat, lang, verbose=True, overwrite=False):
  1405. warnings.warn('The function trans_load_data is deprecated in favor of TranslationImporter', DeprecationWarning)
  1406. translation_importer = TranslationImporter(cr, verbose=verbose)
  1407. translation_importer.load(fileobj, fileformat, lang)
  1408. translation_importer.save(overwrite=overwrite)
  1409. def get_locales(lang=None):
  1410. if lang is None:
  1411. lang = locale.getlocale()[0]
  1412. if os.name == 'nt':
  1413. lang = _LOCALE2WIN32.get(lang, lang)
  1414. def process(enc):
  1415. ln = locale._build_localename((lang, enc))
  1416. yield ln
  1417. nln = locale.normalize(ln)
  1418. if nln != ln:
  1419. yield nln
  1420. for x in process('utf8'): yield x
  1421. prefenc = locale.getpreferredencoding()
  1422. if prefenc:
  1423. for x in process(prefenc): yield x
  1424. prefenc = {
  1425. 'latin1': 'latin9',
  1426. 'iso-8859-1': 'iso8859-15',
  1427. 'cp1252': '1252',
  1428. }.get(prefenc.lower())
  1429. if prefenc:
  1430. for x in process(prefenc): yield x
  1431. yield lang
  1432. def resetlocale():
  1433. # locale.resetlocale is bugged with some locales.
  1434. for ln in get_locales():
  1435. try:
  1436. return locale.setlocale(locale.LC_ALL, ln)
  1437. except locale.Error:
  1438. continue
  1439. def load_language(cr, lang):
  1440. """ Loads a translation terms for a language.
  1441. Used mainly to automate language loading at db initialization.
  1442. :param cr:
  1443. :param str lang: language ISO code with optional underscore (``_``) and
  1444. l10n flavor (ex: 'fr', 'fr_BE', but not 'fr-BE')
  1445. """
  1446. env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
  1447. lang_ids = env['res.lang'].with_context(active_test=False).search([('code', '=', lang)]).ids
  1448. installer = env['base.language.install'].create({'lang_ids': [(6, 0, lang_ids)]})
  1449. installer.lang_install()
  1450. def get_po_paths(module_name: str, lang: str, env: odoo.api.Environment | None = None):
  1451. lang_base = lang.split('_')[0]
  1452. # Load the base as a fallback in case a translation is missing:
  1453. po_names = [lang_base, lang]
  1454. # Exception for Spanish locales: they have two bases, es and es_419:
  1455. if lang_base == 'es' and lang not in ('es_ES', 'es_419'):
  1456. po_names.insert(1, 'es_419')
  1457. po_paths = [
  1458. join(module_name, dir_, filename + '.po')
  1459. for filename in OrderedSet(po_names)
  1460. for dir_ in ('i18n', 'i18n_extra')
  1461. ]
  1462. for path in po_paths:
  1463. with suppress(FileNotFoundError):
  1464. yield file_path(path, env=env)
  1465. class CodeTranslations:
  1466. def __init__(self):
  1467. # {(module_name, lang): {src: value}}
  1468. self.python_translations = {}
  1469. # {(module_name, lang): {'message': [{'id': src, 'string': value}]}
  1470. self.web_translations = {}
  1471. @staticmethod
  1472. def _read_code_translations_file(fileobj, filter_func):
  1473. """ read and return code translations from fileobj with filter filter_func
  1474. :param func filter_func: a filter function to drop unnecessary code translations
  1475. """
  1476. # current, we assume the fileobj is from the source code, which only contains the translation for the current module
  1477. # don't use it in the import logic
  1478. translations = {}
  1479. fileobj.seek(0)
  1480. reader = TranslationFileReader(fileobj, fileformat='po')
  1481. for row in reader:
  1482. if row.get('type') == 'code' and row.get('src') and filter_func(row):
  1483. translations[row['src']] = row['value']
  1484. return translations
  1485. @staticmethod
  1486. def _get_code_translations(module_name, lang, filter_func):
  1487. po_paths = get_po_paths(module_name, lang)
  1488. translations = {}
  1489. for po_path in po_paths:
  1490. try:
  1491. with file_open(po_path, mode='rb') as fileobj:
  1492. p = CodeTranslations._read_code_translations_file(fileobj, filter_func)
  1493. translations.update(p)
  1494. except IOError:
  1495. iso_lang = get_iso_codes(lang)
  1496. filename = '[lang: %s][format: %s]' % (iso_lang or 'new', 'po')
  1497. _logger.exception("couldn't read translation file %s", filename)
  1498. return translations
  1499. def _load_python_translations(self, module_name, lang):
  1500. def filter_func(row):
  1501. return row.get('value') and PYTHON_TRANSLATION_COMMENT in row['comments']
  1502. translations = CodeTranslations._get_code_translations(module_name, lang, filter_func)
  1503. self.python_translations[(module_name, lang)] = ReadonlyDict(translations)
  1504. def _load_web_translations(self, module_name, lang):
  1505. def filter_func(row):
  1506. return row.get('value') and JAVASCRIPT_TRANSLATION_COMMENT in row['comments']
  1507. translations = CodeTranslations._get_code_translations(module_name, lang, filter_func)
  1508. self.web_translations[(module_name, lang)] = ReadonlyDict({
  1509. "messages": tuple(
  1510. ReadonlyDict({"id": src, "string": value})
  1511. for src, value in translations.items())
  1512. })
  1513. def get_python_translations(self, module_name, lang):
  1514. if (module_name, lang) not in self.python_translations:
  1515. self._load_python_translations(module_name, lang)
  1516. return self.python_translations[(module_name, lang)]
  1517. def get_web_translations(self, module_name, lang):
  1518. if (module_name, lang) not in self.web_translations:
  1519. self._load_web_translations(module_name, lang)
  1520. return self.web_translations[(module_name, lang)]
  1521. code_translations = CodeTranslations()
  1522. def _get_translation_upgrade_queries(cr, field):
  1523. """ Return a pair of lists ``migrate_queries, cleanup_queries`` of SQL queries. The queries in
  1524. ``migrate_queries`` do migrate the data from table ``_ir_translation`` to the corresponding
  1525. field's column, while the queries in ``cleanup_queries`` remove the corresponding data from
  1526. table ``_ir_translation``.
  1527. """
  1528. from odoo.modules.registry import Registry # noqa: PLC0415
  1529. Model = Registry(cr.dbname)[field.model_name]
  1530. translation_name = f"{field.model_name},{field.name}"
  1531. migrate_queries = []
  1532. cleanup_queries = []
  1533. if field.translate is True:
  1534. emtpy_src = """'{"en_US": ""}'::jsonb"""
  1535. query = f"""
  1536. WITH t AS (
  1537. SELECT it.res_id as res_id, jsonb_object_agg(it.lang, it.value) AS value, bool_or(imd.noupdate) AS noupdate
  1538. FROM _ir_translation it
  1539. LEFT JOIN ir_model_data imd
  1540. ON imd.model = %s AND imd.res_id = it.res_id AND imd.module != '__export__'
  1541. WHERE it.type = 'model' AND it.name = %s AND it.state = 'translated'
  1542. GROUP BY it.res_id
  1543. )
  1544. UPDATE {Model._table} m
  1545. SET "{field.name}" = CASE WHEN m."{field.name}" IS NULL THEN {emtpy_src} || t.value
  1546. WHEN t.noupdate IS FALSE THEN t.value || m."{field.name}"
  1547. ELSE m."{field.name}" || t.value
  1548. END
  1549. FROM t
  1550. WHERE t.res_id = m.id
  1551. """
  1552. migrate_queries.append(cr.mogrify(query, [Model._name, translation_name]).decode())
  1553. query = "DELETE FROM _ir_translation WHERE type = 'model' AND name = %s"
  1554. cleanup_queries.append(cr.mogrify(query, [translation_name]).decode())
  1555. # upgrade model_terms translation: one update per field per record
  1556. if callable(field.translate):
  1557. cr.execute("SELECT code FROM res_lang WHERE active = 't'")
  1558. languages = {l[0] for l in cr.fetchall()}
  1559. query = f"""
  1560. SELECT t.res_id, m."{field.name}", t.value, t.noupdate
  1561. FROM t
  1562. JOIN "{Model._table}" m ON t.res_id = m.id
  1563. """
  1564. if translation_name == 'ir.ui.view,arch_db':
  1565. cr.execute("SELECT id from ir_module_module WHERE name = 'website' AND state='installed'")
  1566. if cr.fetchone():
  1567. query = f"""
  1568. SELECT t.res_id, m."{field.name}", t.value, t.noupdate, l.code
  1569. FROM t
  1570. JOIN "{Model._table}" m ON t.res_id = m.id
  1571. JOIN website w ON m.website_id = w.id
  1572. JOIN res_lang l ON w.default_lang_id = l.id
  1573. UNION
  1574. SELECT t.res_id, m."{field.name}", t.value, t.noupdate, 'en_US'
  1575. FROM t
  1576. JOIN "{Model._table}" m ON t.res_id = m.id
  1577. WHERE m.website_id IS NULL
  1578. """
  1579. cr.execute(f"""
  1580. WITH t0 AS (
  1581. -- aggregate translations by source term --
  1582. SELECT res_id, lang, jsonb_object_agg(src, value) AS value
  1583. FROM _ir_translation
  1584. WHERE type = 'model_terms' AND name = %s AND state = 'translated'
  1585. GROUP BY res_id, lang
  1586. ),
  1587. t AS (
  1588. -- aggregate translations by lang --
  1589. SELECT t0.res_id AS res_id, jsonb_object_agg(t0.lang, t0.value) AS value, bool_or(imd.noupdate) AS noupdate
  1590. FROM t0
  1591. LEFT JOIN ir_model_data imd
  1592. ON imd.model = %s AND imd.res_id = t0.res_id
  1593. GROUP BY t0.res_id
  1594. )""" + query, [translation_name, Model._name])
  1595. for id_, new_translations, translations, noupdate, *extra in cr.fetchall():
  1596. if not new_translations:
  1597. continue
  1598. # new_translations contains translations updated from the latest po files
  1599. src_value = new_translations.pop('en_US')
  1600. src_terms = field.get_trans_terms(src_value)
  1601. for lang, dst_value in new_translations.items():
  1602. terms_mapping = translations.setdefault(lang, {})
  1603. dst_terms = field.get_trans_terms(dst_value)
  1604. for src_term, dst_term in zip(src_terms, dst_terms):
  1605. if src_term == dst_term or noupdate:
  1606. terms_mapping.setdefault(src_term, dst_term)
  1607. else:
  1608. terms_mapping[src_term] = dst_term
  1609. new_values = {
  1610. lang: field.translate(terms_mapping.get, src_value)
  1611. for lang, terms_mapping in translations.items()
  1612. }
  1613. if "en_US" not in new_values:
  1614. new_values["en_US"] = field.translate(lambda v: None, src_value)
  1615. if extra and extra[0] not in new_values:
  1616. new_values[extra[0]] = field.translate(lambda v: None, src_value)
  1617. elif not extra:
  1618. missing_languages = languages - set(translations)
  1619. if missing_languages:
  1620. src_value = field.translate(lambda v: None, src_value)
  1621. for lang in sorted(missing_languages):
  1622. new_values[lang] = src_value
  1623. query = f'UPDATE "{Model._table}" SET "{field.name}" = %s WHERE id = %s'
  1624. migrate_queries.append(cr.mogrify(query, [Json(new_values), id_]).decode())
  1625. query = "DELETE FROM _ir_translation WHERE type = 'model_terms' AND name = %s"
  1626. cleanup_queries.append(cr.mogrify(query, [translation_name]).decode())
  1627. return migrate_queries, cleanup_queries
上海开阖软件有限公司 沪ICP备12045867号-1