1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 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
30 ('selection', 'Selection'),
32 ('wizard_button', 'Wizard Button'),
33 ('wizard_field', 'Wizard Field'),
34 ('wizard_view', 'Wizard View'),
38 ('constraint', '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):
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, {
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 ''
148 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: