1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields, osv
29 ('selection', 'Selection'),
31 ('wizard_button', 'Wizard Button'),
32 ('wizard_field', 'Wizard Field'),
33 ('wizard_view', 'Wizard View'),
37 ('constraint', 'Constraint'),
40 class ir_translation(osv.osv):
41 _name = "ir.translation"
44 def _get_language(self, cr, uid, context):
45 lang_obj = self.pool.get('res.lang')
46 lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)],
48 langs = lang_obj.browse(cr, uid, lang_ids, context=context)
49 res = [(lang.code, lang.name) for lang in langs]
50 for lang_dict in tools.scan_languages():
51 if lang_dict not in res:
56 'name': fields.char('Field Name', size=128, required=True),
57 'res_id': fields.integer('Resource ID', select=True),
58 'lang': fields.selection(_get_language, string='Language', size=5),
59 'type': fields.selection(TRANSLATION_TYPE, string='Type', size=16, select=True),
60 'src': fields.text('Source'),
61 'value': fields.text('Translation Value'),
64 def _auto_init(self, cr, context={}):
65 super(ir_translation, self)._auto_init(cr, context)
66 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltns',))
68 cr.execute('CREATE INDEX ir_translation_ltns ON ir_translation (lang, type, name, src)')
71 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltn',))
73 cr.execute('CREATE INDEX ir_translation_ltn ON ir_translation (lang, type, name)')
76 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_lts',))
78 cr.execute('CREATE INDEX ir_translation_lts ON ir_translation (lang, type, src)')
81 @tools.cache(skiparg=3, multi='ids')
82 def _get_ids(self, cr, uid, name, tt, lang, ids):
83 translations = dict.fromkeys(ids, False)
85 cr.execute('select res_id,value ' \
86 'from ir_translation ' \
90 'and res_id in ('+','.join(map(str, ids))+')',
92 for res_id, value in cr.fetchall():
93 translations[res_id] = value
96 def _set_ids(self, cr, uid, name, tt, lang, ids, value, src=None):
98 tr = self._get_ids(cr, uid, name, tt, lang, ids)
101 self._get_source.clear_cache(cr.dbname, uid, name, tt, lang, tr[res_id])
102 self._get_source.clear_cache(cr.dbname, uid, name, tt, lang)
103 self._get_ids.clear_cache(cr.dbname, uid, name, tt, lang, ids)
105 cr.execute('delete from ir_translation ' \
109 'and res_id in ('+','.join(map(str,ids))+')',
113 self.create(cr, uid, {
123 @tools.cache(skiparg=3)
124 def _get_source(self, cr, uid, name, tt, lang, source=None):
126 #if isinstance(source, unicode):
127 # source = source.encode('utf8')
128 cr.execute('select value ' \
129 'from ir_translation ' \
134 (lang, tt, str(name), source))
136 cr.execute('select value ' \
137 'from ir_translation ' \
141 (lang, tt, str(name)))
143 trad = res and res[0] or ''
146 def create(self, cursor, user, vals, context=None):
149 ids = super(ir_translation, self).create(cursor, user, vals, context=context)
150 for trans_obj in self.read(cursor, user, [ids], ['name','type','res_id'], context=context):
151 self._get_source.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], lang=context.get('lang','en_US'))
152 self._get_ids.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], context.get('lang','en_US'), [trans_obj['res_id']])
155 def write(self, cursor, user, ids, vals, context=None):
158 result = super(ir_translation, self).write(cursor, user, ids, vals, context=context)
159 for trans_obj in self.read(cursor, user, ids, ['name','type','res_id'], context=context):
160 self._get_source.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], lang=context.get('lang','en_US'))
161 self._get_ids.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], context.get('lang','en_US'), [trans_obj['res_id']])
164 def unlink(self, cursor, user, ids, context=None):
167 for trans_obj in self.read(cursor, user, ids, ['name','type','res_id'], context=context):
168 self._get_source.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], lang=context.get('lang','en_US'))
169 self._get_ids.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], context.get('lang','en_US'), [trans_obj['res_id']])
170 result = super(ir_translation, self).unlink(cursor, user, ids, context=context)
175 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: