Changed licencing
[odoo/odoo.git] / bin / addons / base / ir / ir_translation.py
1 # -*- encoding: 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 ]
39
40 class ir_translation(osv.osv):
41     _name = "ir.translation"
42     _log_access = False
43
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)],
47                 context=context)
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:
52                 res.append(lang_dict)
53         return res
54
55     _columns = {
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'),
62     }
63
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',))
67         if not cr.fetchone():
68             cr.execute('CREATE INDEX ir_translation_ltns ON ir_translation (lang, type, name, src)')
69             cr.commit()
70
71         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltn',))
72         if not cr.fetchone():
73             cr.execute('CREATE INDEX ir_translation_ltn ON ir_translation (lang, type, name)')
74             cr.commit()
75
76         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_lts',))
77         if not cr.fetchone():
78             cr.execute('CREATE INDEX ir_translation_lts ON ir_translation (lang, type, src)')
79             cr.commit()
80
81     @tools.cache(skiparg=3, multi='ids')
82     def _get_ids(self, cr, uid, name, tt, lang, ids):
83         translations = dict.fromkeys(ids, False)
84         if ids:
85             cr.execute('select res_id,value ' \
86                     'from ir_translation ' \
87                     'where lang=%s ' \
88                         'and type=%s ' \
89                         'and name=%s ' \
90                         'and res_id in ('+','.join(map(str, ids))+')',
91                     (lang,tt,name))
92             for res_id, value in cr.fetchall():
93                 translations[res_id] = value
94         return translations
95
96     def _set_ids(self, cr, uid, name, tt, lang, ids, value, src=None):
97         # clear the caches
98         tr = self._get_ids(cr, uid, name, tt, lang, ids)
99         for res_id in tr:
100             if tr[res_id]:
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)
104
105         cr.execute('delete from ir_translation ' \
106                 'where lang=%s ' \
107                     'and type=%s ' \
108                     'and name=%s ' \
109                     'and res_id in ('+','.join(map(str,ids))+')',
110                 (lang,tt,name))
111         cr.commit()
112         for id in ids:
113             self.create(cr, uid, {
114                 'lang':lang,
115                 'type':tt,
116                 'name':name,
117                 'res_id':id,
118                 'value':value,
119                 'src':src,
120                 })
121         return len(ids)
122
123     @tools.cache(skiparg=3)
124     def _get_source(self, cr, uid, name, tt, lang, source=None):
125         if source:
126             #if isinstance(source, unicode):
127             #   source = source.encode('utf8')
128             cr.execute('select value ' \
129                     'from ir_translation ' \
130                     'where lang=%s ' \
131                         'and type=%s ' \
132                         'and name=%s ' \
133                         'and src=%s',
134                     (lang, tt, str(name), source))
135         else:
136             cr.execute('select value ' \
137                     'from ir_translation ' \
138                     'where lang=%s ' \
139                         'and type=%s ' \
140                         'and name=%s',
141                     (lang, tt, str(name)))
142         res = cr.fetchone()
143         trad = res and res[0] or ''
144         return trad
145         
146     def create(self, cursor, user, vals, context=None):
147         if not context:
148             context = {}
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']])
153         return ids    
154
155     def write(self, cursor, user, ids, vals, context=None):
156         if not context:
157             context = {}
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']])
162         return result
163
164     def unlink(self, cursor, user, ids, context=None):
165         if not context:
166             context = {}
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)
171         return result    
172
173 ir_translation()
174
175 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
176