e33365fcfca9f3f1f71fddab04582f0daf80f0e5
[odoo/odoo.git] / bin / addons / base / ir / ir_translation.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.     
19 #
20 ##############################################################################
21
22 from osv import fields, osv
23 import tools
24
25 TRANSLATION_TYPE = [
26     ('field', 'Field'),
27     ('model', 'Object'),
28     ('rml', 'RML'),
29     ('selection', 'Selection'),
30     ('view', 'View'),
31     ('wizard_button', 'Wizard Button'),
32     ('wizard_field', 'Wizard Field'),
33     ('wizard_view', 'Wizard View'),
34     ('xsl', 'XSL'),
35     ('help', 'Help'),
36     ('code', 'Code'),
37     ('constraint', 'Constraint'),
38     ('sql_constraint', 'SQL Constraint')    
39 ]
40
41 class ir_translation(osv.osv):
42     _name = "ir.translation"
43     _log_access = False
44
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)],
48                 context=context)
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:
53                 res.append(lang_dict)
54         return res
55
56     _columns = {
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'),
63     }
64
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',))
68         if not cr.fetchone():
69             cr.execute('CREATE INDEX ir_translation_ltns ON ir_translation (lang, type, name, src)')
70             cr.commit()
71
72         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltn',))
73         if not cr.fetchone():
74             cr.execute('CREATE INDEX ir_translation_ltn ON ir_translation (lang, type, name)')
75             cr.commit()
76
77         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_lts',))
78         if not cr.fetchone():
79             cr.execute('CREATE INDEX ir_translation_lts ON ir_translation (lang, type, src)')
80             cr.commit()
81
82     @tools.cache(skiparg=3, multi='ids')
83     def _get_ids(self, cr, uid, name, tt, lang, ids):
84         translations = dict.fromkeys(ids, False)
85         if ids:
86             cr.execute('select res_id,value ' \
87                     'from ir_translation ' \
88                     'where lang=%s ' \
89                         'and type=%s ' \
90                         'and name=%s ' \
91                         'and res_id in ('+','.join(map(str, ids))+')',
92                     (lang,tt,name))
93             for res_id, value in cr.fetchall():
94                 translations[res_id] = value
95         return translations
96
97     def _set_ids(self, cr, uid, name, tt, lang, ids, value, src=None):
98         # clear the caches
99         tr = self._get_ids(cr, uid, name, tt, lang, ids)
100         for res_id in tr:
101             if tr[res_id]:
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)
105
106         cr.execute('delete from ir_translation ' \
107                 'where lang=%s ' \
108                     'and type=%s ' \
109                     'and name=%s ' \
110                     'and res_id in ('+','.join(map(str, ids))+')',
111                 (lang,tt,name))
112         cr.commit()
113         for id in ids:
114             self.create(cr, uid, {
115                 'lang':lang,
116                 'type':tt,
117                 'name':name,
118                 'res_id':id,
119                 'value':value,
120                 'src':src,
121                 })
122         return len(ids)
123
124     @tools.cache(skiparg=3)
125     def _get_source(self, cr, uid, name, tt, lang, source=None):
126         if source:
127             #if isinstance(source, unicode):
128             #   source = source.encode('utf8')
129             cr.execute('select value ' \
130                     'from ir_translation ' \
131                     'where lang=%s ' \
132                         'and type=%s ' \
133                         'and name=%s ' \
134                         'and src=%s',
135                     (lang, tt, tools.ustr(name), source))
136         else:
137             cr.execute('select value ' \
138                     'from ir_translation ' \
139                     'where lang=%s ' \
140                         'and type=%s ' \
141                         'and name=%s',
142                     (lang, tt, tools.ustr(name)))
143         res = cr.fetchone()
144         trad = res and res[0] or ''
145         return trad
146         
147     def create(self, cursor, user, vals, context=None):
148         if not context:
149             context = {}
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']])
154         return ids    
155
156     def write(self, cursor, user, ids, vals, context=None):
157         if not context:
158             context = {}
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']])
163         return result
164
165     def unlink(self, cursor, user, ids, context=None):
166         if not context:
167             context = {}
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)
172         return result    
173
174 ir_translation()
175
176 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
177