1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 from osv import fields, osv
24 from osv.osv import Cacheable
31 ('selection', 'Selection'),
33 ('wizard_button', 'Wizard Button'),
34 ('wizard_field', 'Wizard Field'),
35 ('wizard_view', 'Wizard View'),
39 ('constraint', 'Constraint'),
42 class ir_translation(osv.osv, Cacheable):
43 _name = "ir.translation"
46 def _get_language(self, cr, uid, context):
47 lang_obj = self.pool.get('res.lang')
48 lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)],
50 langs = lang_obj.browse(cr, uid, lang_ids, context=context)
51 res = [(lang.code, lang.name) for lang in langs]
52 for lang_dict in tools.scan_languages():
53 if lang_dict not in res:
58 'name': fields.char('Field Name', size=128, required=True),
59 'res_id': fields.integer('Resource ID', select=True),
60 'lang': fields.selection(_get_language, string='Language', size=5),
61 'type': fields.selection(TRANSLATION_TYPE, string='Type', size=16, select=True),
62 'src': fields.text('Source'),
63 'value': fields.text('Translation Value'),
66 def _auto_init(self, cr, context={}):
67 super(ir_translation, self)._auto_init(cr, context)
68 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltns',))
70 cr.execute('CREATE INDEX ir_translation_ltns ON ir_translation (lang, type, name, src)')
73 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltn',))
75 cr.execute('CREATE INDEX ir_translation_ltn ON ir_translation (lang, type, name)')
78 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_lts',))
80 cr.execute('CREATE INDEX ir_translation_lts ON ir_translation (lang, type, src)')
83 def _get_ids(self, cr, uid, name, tt, lang, ids):
84 translations, to_fetch = {}, []
86 trans = self.get((lang, name, id))
88 translations[id] = trans
92 cr.execute('select res_id,value ' \
93 'from ir_translation ' \
97 'and res_id in ('+','.join(map(str, to_fetch))+')',
99 for res_id, value in cr.fetchall():
100 self.add((lang, tt, name, res_id), value)
101 translations[res_id] = value
103 if res_id not in translations:
104 self.add((lang, tt, name, res_id), False)
105 translations[res_id] = False
108 def _set_ids(self, cr, uid, name, tt, lang, ids, value):
109 cr.execute('delete from ir_translation ' \
113 'and res_id in ('+','.join(map(str,ids))+')',
116 self.create(cr, uid, {
125 def _get_source(self, cr, uid, name, tt, lang, source=None):
126 trans = self.get((lang, tt, name, source))
127 if trans is not None:
131 #if isinstance(source, unicode):
132 # source = source.encode('utf8')
133 cr.execute('select value ' \
134 'from ir_translation ' \
139 (lang, tt, str(name), source))
141 cr.execute('select value ' \
142 'from ir_translation ' \
146 (lang, tt, str(name)))
149 trad = res and res[0] or ''
150 self.add((lang, tt, name, source), trad)
153 def unlink(self, cursor, user, ids, context=None):
155 return super(ir_translation, self).unlink(cursor, user, ids,
158 def create(self, cursor, user, vals, context=None):
160 return super(ir_translation, self).create(cursor, user, vals,
163 def write(self, cursor, user, ids, vals, context=None):
165 return super(ir_translation, self).write(cursor, user, ids, vals,
170 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: