transifex_code_translation.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import psycopg2
  4. from odoo import api, models, fields
  5. from odoo.tools.translate import CodeTranslations
  6. class TransifexCodeTranslation(models.Model):
  7. _name = "transifex.code.translation"
  8. _description = "Code Translation"
  9. _log_access = False
  10. source = fields.Text(string='Code')
  11. value = fields.Text(string='Translation Value')
  12. module = fields.Char(help="Module this term belongs to")
  13. lang = fields.Selection(selection='_get_languages', string='Language', validate=False)
  14. transifex_url = fields.Char("Transifex URL", compute='_compute_transifex_url',
  15. help="Propose a modification in the official version of Odoo")
  16. def _get_languages(self):
  17. return self.env['res.lang'].get_installed()
  18. def _compute_transifex_url(self):
  19. self.transifex_url = False
  20. self.env['transifex.translation']._update_transifex_url(self)
  21. def _load_code_translations(self, module_names=None, langs=None):
  22. try:
  23. # the table lock promises translations for a (module, language) will only be created once
  24. self.env.cr.execute(f'LOCK TABLE {self._table} IN EXCLUSIVE MODE NOWAIT')
  25. if module_names is None:
  26. module_names = self.env['ir.module.module'].search([('state', '=', 'installed')]).mapped('name')
  27. if langs is None:
  28. langs = [lang for lang, _ in self._get_languages() if lang != 'en_US']
  29. self.env.cr.execute(f'SELECT DISTINCT module, lang FROM {self._table}')
  30. loaded_code_translations = set(self.env.cr.fetchall())
  31. create_value_list = [
  32. {
  33. 'source': src,
  34. 'value': value,
  35. 'module': module_name,
  36. 'lang': lang,
  37. }
  38. for module_name in module_names
  39. for lang in langs
  40. if (module_name, lang) not in loaded_code_translations
  41. for src, value in CodeTranslations._get_code_translations(module_name, lang, lambda x: True).items()
  42. ]
  43. self.sudo().create(create_value_list)
  44. except psycopg2.errors.LockNotAvailable:
  45. return False
  46. return True
  47. def _open_code_translations(self):
  48. self._load_code_translations()
  49. return {
  50. 'name': 'Code Translations',
  51. 'type': 'ir.actions.act_window',
  52. 'res_model': 'transifex.code.translation',
  53. 'view_mode': 'list',
  54. }
  55. @api.model
  56. def reload(self):
  57. self.env.cr.execute(f'DELETE FROM {self._table}')
  58. return self._load_code_translations()