base_export_language.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import base64
  4. import contextlib
  5. import io
  6. from odoo import api, fields, models, tools, _
  7. NEW_LANG_KEY = '__new__'
  8. class BaseLanguageExport(models.TransientModel):
  9. _name = "base.language.export"
  10. _description = 'Language Export'
  11. @api.model
  12. def _get_languages(self):
  13. langs = self.env['res.lang'].get_installed()
  14. return [(NEW_LANG_KEY, _('New Language (Empty translation template)'))] + \
  15. langs
  16. name = fields.Char('File Name', readonly=True)
  17. lang = fields.Selection(_get_languages, string='Language', required=True, default=NEW_LANG_KEY)
  18. format = fields.Selection([('csv','CSV File'), ('po','PO File'), ('tgz', 'TGZ Archive')],
  19. string='File Format', required=True, default='po')
  20. modules = fields.Many2many('ir.module.module', 'rel_modules_langexport', 'wiz_id', 'module_id',
  21. string='Apps To Export', domain=[('state','=','installed')])
  22. data = fields.Binary('File', readonly=True, attachment=False)
  23. state = fields.Selection([('choose', 'choose'), ('get', 'get')], # choose language or get the file
  24. default='choose')
  25. def act_getfile(self):
  26. this = self[0]
  27. lang = this.lang if this.lang != NEW_LANG_KEY else False
  28. mods = sorted(this.mapped('modules.name')) or ['all']
  29. with contextlib.closing(io.BytesIO()) as buf:
  30. tools.trans_export(lang, mods, buf, this.format, self._cr)
  31. out = base64.encodebytes(buf.getvalue())
  32. filename = 'new'
  33. if lang:
  34. filename = tools.get_iso_codes(lang)
  35. elif len(mods) == 1:
  36. filename = mods[0]
  37. extension = this.format
  38. if not lang and extension == 'po':
  39. extension = 'pot'
  40. name = "%s.%s" % (filename, extension)
  41. this.write({'state': 'get', 'data': out, 'name': name})
  42. return {
  43. 'type': 'ir.actions.act_window',
  44. 'res_model': 'base.language.export',
  45. 'view_mode': 'form',
  46. 'res_id': this.id,
  47. 'views': [(False, 'form')],
  48. 'target': 'new',
  49. }