1 # -*- coding: 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'),
38 ('sql_constraint', 'SQL Constraint')
41 class ir_translation(osv.osv):
42 _name = "ir.translation"
45 def _get_language(self, cr, uid, context):
46 lang_obj = self.pool.get('res.lang')
47 lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)],
49 langs = lang_obj.browse(cr, uid, lang_ids, context=context)
50 res = [(lang.code, lang.name) for lang in langs]
51 for lang_dict in tools.scan_languages():
52 if lang_dict not in res:
57 'name': fields.char('Field Name', size=128, required=True),
58 'res_id': fields.integer('Resource ID', select=True),
59 'lang': fields.selection(_get_language, string='Language', size=5),
60 'type': fields.selection(TRANSLATION_TYPE, string='Type', size=16, select=True),
61 'src': fields.text('Source'),
62 'value': fields.text('Translation Value'),
65 def _auto_init(self, cr, context={}):
66 super(ir_translation, self)._auto_init(cr, context)
67 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltns',))
69 cr.execute('CREATE INDEX ir_translation_ltns ON ir_translation (lang, type, name, src)')
72 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltn',))
74 cr.execute('CREATE INDEX ir_translation_ltn ON ir_translation (lang, type, name)')
77 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_lts',))
79 cr.execute('CREATE INDEX ir_translation_lts ON ir_translation (lang, type, src)')
82 @tools.cache(skiparg=3, multi='ids')
83 def _get_ids(self, cr, uid, name, tt, lang, ids):
84 translations = dict.fromkeys(ids, False)
86 cr.execute('select res_id,value ' \
87 'from ir_translation ' \
91 'and res_id in ('+','.join(map(str, ids))+')',
93 for res_id, value in cr.fetchall():
94 translations[res_id] = value
97 def _set_ids(self, cr, uid, name, tt, lang, ids, value, src=None):
99 tr = self._get_ids(cr, uid, name, tt, lang, ids)
102 self._get_source.clear_cache(cr.dbname, uid, name, tt, lang, tr[res_id])
103 self._get_source.clear_cache(cr.dbname, uid, name, tt, lang)
104 self._get_ids.clear_cache(cr.dbname, uid, name, tt, lang, ids)
106 cr.execute('delete from ir_translation ' \
110 'and res_id in ('+','.join(map(str, ids))+')',
114 self.create(cr, uid, {
124 @tools.cache(skiparg=3)
125 def _get_source(self, cr, uid, name, tt, lang, source=None):
127 #if isinstance(source, unicode):
128 # source = source.encode('utf8')
129 cr.execute('select value ' \
130 'from ir_translation ' \
135 (lang, tt, tools.ustr(name), source))
137 cr.execute('select value ' \
138 'from ir_translation ' \
142 (lang, tt, tools.ustr(name)))
144 trad = res and res[0] or ''
147 def create(self, cursor, user, vals, context=None):
150 ids = super(ir_translation, self).create(cursor, user, vals, context=context)
151 for trans_obj in self.read(cursor, user, [ids], ['name','type','res_id'], context=context):
152 self._get_source.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], lang=context.get('lang','en_US'))
153 self._get_ids.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], context.get('lang','en_US'), [trans_obj['res_id']])
156 def write(self, cursor, user, ids, vals, context=None):
159 result = super(ir_translation, self).write(cursor, user, ids, vals, context=context)
160 for trans_obj in self.read(cursor, user, ids, ['name','type','res_id'], context=context):
161 self._get_source.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], lang=context.get('lang','en_US'))
162 self._get_ids.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], context.get('lang','en_US'), [trans_obj['res_id']])
165 def unlink(self, cursor, user, ids, context=None):
168 for trans_obj in self.read(cursor, user, ids, ['name','type','res_id'], context=context):
169 self._get_source.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], lang=context.get('lang','en_US'))
170 self._get_ids.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], context.get('lang','en_US'), [trans_obj['res_id']])
171 result = super(ir_translation, self).unlink(cursor, user, ids, context=context)
176 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: