transifex_translation.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import werkzeug.urls
  4. from configparser import ConfigParser
  5. from os import pardir
  6. from os.path import isfile, join as opj
  7. import odoo
  8. from odoo import models, tools
  9. class TransifexTranslation(models.AbstractModel):
  10. _name = "transifex.translation"
  11. _description = "Transifex Translation"
  12. @tools.ormcache()
  13. def _get_transifex_projects(self):
  14. """ get the transifex project name for each module
  15. .tx/config files contains the project reference
  16. first section is [main], after '[odoo-16.sale]'
  17. :rtype: dict
  18. :return: {module_name: tx_project_name}
  19. """
  20. tx_config_file = ConfigParser()
  21. projects = {}
  22. for addon_path in odoo.addons.__path__:
  23. for tx_path in (
  24. opj(addon_path, '.tx', 'config'),
  25. opj(addon_path, pardir, '.tx', 'config'),
  26. ):
  27. if isfile(tx_path):
  28. tx_config_file.read(tx_path)
  29. for sec in tx_config_file.sections()[1:]:
  30. if len(sec.split(":")) != 6:
  31. # old format ['main', 'odoo-16.base', ...]
  32. tx_project, tx_mod = sec.split(".")
  33. else:
  34. # tx_config_file.sections(): ['main', 'o:odoo:p:odoo-16:r:base', ...]
  35. _, _, _, tx_project, _, tx_mod = sec.split(':')
  36. projects[tx_mod] = tx_project
  37. return projects
  38. def _update_transifex_url(self, translations):
  39. """ Update translations' Transifex URL
  40. :param translations: the translations to update, may be a recordset or a list of dicts.
  41. The elements of `translations` must have the fields/keys 'source', 'module', 'lang',
  42. and the field/key 'transifex_url' is updated on them.
  43. """
  44. # e.g. 'https://www.transifex.com/odoo/'
  45. base_url = self.env['ir.config_parameter'].sudo().get_param('transifex.project_url')
  46. if not base_url:
  47. return
  48. base_url = base_url.rstrip('/')
  49. res_langs = self.env['res.lang'].search([])
  50. lang_to_iso = {l.code: l.iso_code for l in res_langs}
  51. if not lang_to_iso:
  52. return
  53. projects = self._get_transifex_projects()
  54. if not projects:
  55. return
  56. for translation in translations:
  57. if not translation['source'] or translation['lang'] == 'en_US':
  58. continue
  59. lang_iso = lang_to_iso.get(translation['lang'])
  60. if not lang_iso:
  61. continue
  62. project = projects.get(translation['module'])
  63. if not project:
  64. continue
  65. # e.g. https://www.transifex.com/odoo/odoo-16/translate/#fr_FR/sale/42?q=text:'Sale+Order'
  66. # 42 is an arbitrary number to satisfy the transifex URL format
  67. source = werkzeug.urls.url_quote_plus(translation['source'][:50].replace("\n", "").replace("'", "\\'"))
  68. source = f"'{source}'" if "+" in source else source
  69. translation['transifex_url'] = f"{base_url}/{project}/translate/#{lang_iso}/{translation['module']}/42?q=text%3A{source}"