1 ##############################################################################
3 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 ##############################################################################
30 from osv import fields, osv
31 from osv.osv import Cacheable
38 ('selection', 'Selection'),
40 ('wizard_button', 'Wizard Button'),
41 ('wizard_field', 'Wizard Field'),
42 ('wizard_view', 'Wizard View'),
46 ('constraint', 'Constraint'),
49 class ir_translation(osv.osv, Cacheable):
50 _name = "ir.translation"
53 def _get_language(self, cr, uid, context):
54 lang_obj = self.pool.get('res.lang')
55 lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)],
57 langs = lang_obj.browse(cr, uid, lang_ids, context=context)
58 res = [(lang.code, lang.name) for lang in langs]
59 for lang_dict in tools.scan_languages():
60 if lang_dict not in res:
65 'name': fields.char('Field Name', size=128, required=True),
66 'res_id': fields.integer('Resource ID'),
67 'lang': fields.selection(_get_language, string='Language', size=5),
68 'type': fields.selection(TRANSLATION_TYPE, string='Type', size=16),
69 'src': fields.text('Source'),
70 'value': fields.text('Translation Value'),
73 create index ir_translation_ltn on ir_translation (lang,type,name);
74 create index ir_translation_res_id on ir_translation (res_id);
77 def _get_ids(self, cr, uid, name, tt, lang, ids):
78 translations, to_fetch = {}, []
80 trans = self.get((lang, name, id))
82 translations[id] = trans
86 cr.execute('select res_id,value ' \
87 'from ir_translation ' \
91 'and res_id in ('+','.join(map(str, to_fetch))+')',
93 for res_id, value in cr.fetchall():
94 self.add((lang, tt, name, res_id), value)
95 translations[res_id] = value
97 if res_id not in translations:
98 self.add((lang, tt, name, res_id), False)
99 translations[res_id] = False
102 def _set_ids(self, cr, uid, name, tt, lang, ids, value):
103 cr.execute('delete from ir_translation ' \
107 'and res_id in ('+','.join(map(str,ids))+')',
110 self.create(cr, uid, {
119 def _get_source(self, cr, uid, name, tt, lang, source=None):
120 trans = self.get((lang, tt, name, source))
121 if trans is not None:
125 #if isinstance(source, unicode):
126 # source = source.encode('utf8')
127 cr.execute('select value ' \
128 'from ir_translation ' \
133 (lang, tt, str(name), source))
135 cr.execute('select value ' \
136 'from ir_translation ' \
140 (lang, tt, str(name)))
143 trad = res and res[0] or ''
144 self.add((lang, tt, name, source), trad)
147 def unlink(self, cursor, user, ids, context=None):
149 return super(ir_translation, self).unlink(cusor, user, ids,
152 def create(self, cursor, user, vals, context=None):
154 return super(ir_translation, self).create(cursor, user, vals,
157 def write(self, cursor, user, ids, vals, context=None):
159 return super(ir_translation, self).write(cursor, user, ids, vals,