Merge branch 'master' of https://github.com/odoo/odoo
[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_SY': ('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     'th_TH': ('th', 'Thai'),
46     'nb_NO': ('no', 'Norwegian'),
47     'ro_RO': ('ro', 'Romanian'),
48     'tr_TR': ('tr', 'Turkish'),
49     'bg_BG': ('bg', 'Bulgarian'),
50     'da_DK': ('da', 'Danish'),
51     'en_GB': ('en-gb', 'English (British)'),
52     'el_GR': ('el', 'Greek'),
53     'vi_VN': ('vi', 'Vietnamese'),
54     'he_IL': ('he', 'Hebrew'),
55     'hu_HU': ('hu', 'Hungarian'),
56     'fi_FI': ('fi', 'Finnish')
57 }
58
59
60 class ir_translation(osv.Model):
61     _name = "ir.translation"
62     _inherit = "ir.translation"
63
64     _columns = {
65         'gengo_comment': fields.text("Comments & Activity Linked to Gengo"),
66         'order_id': fields.char('Gengo Order ID', size=32),
67         "gengo_translation": fields.selection([('machine', 'Translation By Machine'),
68                                              ('standard', 'Standard'),
69                                              ('pro', 'Pro'),
70                                              ('ultra', 'Ultra')], "Gengo Translation Service Level", help='You can select here the service level you want for an automatic translation using Gengo.'),
71     }
72
73     def _get_all_supported_languages(self, cr, uid, context=None):
74         flag, gengo = self.pool.get('base.gengo.translations').gengo_authentication(cr, uid, context=context)
75         if not flag:
76             raise osv.except_osv(_('Gengo Authentication Error'), gengo)
77         supported_langs = {}
78         lang_pair = gengo.getServiceLanguagePairs(lc_src='en')
79         if lang_pair['opstat'] == 'ok':
80             for g_lang in lang_pair['response']:
81                 if g_lang['lc_tgt'] not in supported_langs:
82                     supported_langs[g_lang['lc_tgt']] = []
83                 supported_langs[g_lang['lc_tgt']] += [g_lang['tier']]
84         return supported_langs
85
86     def _get_gengo_corresponding_language(cr, lang):
87         return lang in LANG_CODE_MAPPING and LANG_CODE_MAPPING[lang][0] or lang
88
89     def _get_source_query(self, cr, uid, name, types, lang, source, res_id):
90         query, params = super(ir_translation, self)._get_source_query(cr, uid, name, types, lang, source, res_id)
91
92         query += """
93                     ORDER BY
94                         CASE
95                             WHEN gengo_translation=%s then 10
96                             WHEN gengo_translation=%s then 20
97                             WHEN gengo_translation=%s then 30
98                             WHEN gengo_translation=%s then 40
99                             ELSE 0
100                         END DESC
101                  """
102         params += ('machine', 'standard', 'ultra', 'pro',)
103         return (query, params)