[IMP] mail: You have one unread message
[odoo/odoo.git] / addons / base_gengo / ir_translation.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Business Applications
5 #    Copyright (C) 2004-2012 OpenERP S.A. (<http://openerp.com>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp.osv import fields, osv
23 from openerp.tools.translate import _
24
25 LANG_CODE_MAPPING = {
26     'ar_SA': ('ar', 'Arabic'),
27     'id_ID': ('id', 'Indonesian'),
28     'nl_NL': ('nl', 'Dutch'),
29     'fr_CA': ('fr-ca', 'French (Canada)'),
30     'pl_PL': ('pl', 'Polish'),
31     'zh_TW': ('zh-tw', 'Chinese (Traditional)'),
32     'sv_SE': ('sv', 'Swedish'),
33     'ko_KR': ('ko', 'Korean'),
34     'pt_PT': ('pt', 'Portuguese (Europe)'),
35     'en_US': ('en', 'English'),
36     'ja_JP': ('ja', 'Japanese'),
37     'es_ES': ('es', 'Spanish (Spain)'),
38     'zh_CN': ('zh', 'Chinese (Simplified)'),
39     'de_DE': ('de', 'German'),
40     'fr_FR': ('fr', 'French'),
41     'fr_BE': ('fr', 'French'),
42     'ru_RU': ('ru', 'Russian'),
43     'it_IT': ('it', 'Italian'),
44     'pt_BR': ('pt-br', 'Portuguese (Brazil)')
45 }
46
47 class ir_translation(osv.Model):
48     _name = "ir.translation"
49     _inherit = "ir.translation"
50     _columns = {
51         'gengo_comment': fields.text("Comments & Activity Linked to Gengo"),
52         'job_id': fields.char('Gengo Job ID', size=32),
53         "gengo_translation": fields.selection([('machine', 'Translation By Machine'),
54                                             ('standard', 'Standard'),
55                                             ('pro', 'Pro'),
56                                             ('ultra', 'Ultra')], "Gengo Translation Service Level", help='You can select here the service level you want for an automatic translation using Gengo.'),
57     }
58
59     def _get_all_supported_languages(self, cr, uid, context=None):
60         flag, gengo = self.pool.get('base.gengo.translations').gengo_authentication(cr, uid, context=context)
61         if not flag:
62             raise osv.except_osv(_('Gengo Authentication Error'), gengo)
63         supported_langs = {}
64         lang_pair = gengo.getServiceLanguagePairs(lc_src='en')
65         if lang_pair['opstat'] == 'ok':
66             for g_lang in lang_pair['response']:
67                 if g_lang['lc_tgt'] not in supported_langs:
68                     supported_langs[g_lang['lc_tgt']] = []
69                 supported_langs[g_lang['lc_tgt']] += [g_lang['tier']]
70         return supported_langs
71
72     def _get_gengo_corresponding_language(cr, lang):
73         return lang in LANG_CODE_MAPPING and LANG_CODE_MAPPING[lang][0] or lang
74
75     def _check_lang_support(self, cr, uid, ids, context=None):
76         for term in self.browse(cr, uid, ids, context=context):
77             if term.gengo_translation:
78                 supported_langs = self._get_all_supported_languages(cr, uid, context=context)
79                 if supported_langs:
80                     tier = "nonprofit" if term.gengo_translation == 'machine' else term.gengo_translation
81                     language = self._get_gengo_corresponding_language(term.lang)
82                     if tier not in supported_langs.get(language,[]):
83                         return False
84         return True
85
86     _constraints = [
87         (_check_lang_support, 'The Gengo translation service selected is not supported for this language.', ['gengo_translation'])
88     ]