translation: allow export in .po format
[odoo/odoo.git] / bin / addons / base / ir / ir_translation.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # $Id$
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 #
28 ##############################################################################
29
30 from osv import fields, osv
31 from osv.osv  import Cacheable
32 import tools
33
34 TRANSLATION_TYPE = [
35         ('field', 'Field'),
36         ('model', 'Model'),
37         ('rml', 'RML'),
38         ('selection', 'Selection'),
39         ('view', 'View'),
40         ('wizard_button', 'Wizard Button'),
41         ('wizard_field', 'Wizard Field'),
42         ('wizard_view', 'Wizard View'),
43         ('xsl', 'XSL'),
44         ('help', 'Help'),
45         ('code', 'Code'),
46         ('constraint', 'Constraint'),
47 ]
48
49 class ir_translation(osv.osv, Cacheable):
50         _name = "ir.translation"
51         _log_access = False
52
53         def _get_language(self, cr, uid, context):
54                 lang_obj = self.pool.get('res.lang')
55                 lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)],
56                                 context=context)
57                 langs = lang_obj.browse(cr, uid, lang_ids, context=context)
58                 res = [(lang.code, lang.name) for lang in langs]
59                 for lang_dict in tools.scan_languages():
60                         if lang_dict not in res:
61                                 res.append(lang_dict)
62                 return res
63
64         _columns = {
65                 'name': fields.char('Field Name', size=128, required=True),
66                 'res_id': fields.integer('Resource ID'),
67                 'lang': fields.selection(_get_language, string='Language', size=5),
68                 'type': fields.selection(TRANSLATION_TYPE, string='Type', size=16),
69                 'src': fields.text('Source'),
70                 'value': fields.text('Translation Value'),
71         }
72         _sql = """
73                 create index ir_translation_ltn on ir_translation (lang,type,name);
74                 create index ir_translation_res_id on ir_translation (res_id);
75         """
76
77         def _get_ids(self, cr, uid, name, tt, lang, ids):
78                 translations, to_fetch = {}, []
79                 for id in ids:
80                         trans = self.get((lang, name, id))
81                         if trans is not None:
82                                 translations[id] = trans
83                         else:
84                                 to_fetch.append(id)
85                 if to_fetch:
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, to_fetch))+')',
92                                         (lang,tt,name))
93                         for res_id, value in cr.fetchall():
94                                 self.add((lang, tt, name, res_id), value)
95                                 translations[res_id] = value
96                 for res_id in ids:
97                         if res_id not in translations:
98                                 self.add((lang, tt, name, res_id), False)
99                                 translations[res_id] = False
100                 return translations
101
102         def _set_ids(self, cr, uid, name, tt, lang, ids, value):
103                 cr.execute('delete from ir_translation ' \
104                                 'where lang=%s ' \
105                                         'and type=%s ' \
106                                         'and name=%s ' \
107                                         'and res_id in ('+','.join(map(str,ids))+')',
108                                 (lang,tt,name))
109                 for id in ids:
110                         self.create(cr, uid, {
111                                 'lang':lang,
112                                 'type':tt,
113                                 'name':name,
114                                 'res_id':id,
115                                 'value':value,
116                                 })
117                 return len(ids)
118
119         def _get_source(self, cr, uid, name, tt, lang, source=None):
120                 trans = self.get((lang, tt, name, source))
121                 if trans is not None:
122                         return trans
123
124                 if source:
125                         #if isinstance(source, unicode):
126                         #       source = source.encode('utf8')
127                         cr.execute('select value ' \
128                                         'from ir_translation ' \
129                                         'where lang=%s ' \
130                                                 'and type=%s ' \
131                                                 'and name=%s ' \
132                                                 'and src=%s',
133                                         (lang, tt, str(name), source))
134                 else:
135                         cr.execute('select value ' \
136                                         'from ir_translation ' \
137                                         'where lang=%s ' \
138                                                 'and type=%s ' \
139                                                 'and name=%s',
140                                         (lang, tt, str(name)))
141                 res = cr.fetchone()
142                 
143                 trad = res and res[0] or ''
144                 self.add((lang, tt, name, source), trad)
145                 return trad
146
147         def unlink(self, cursor, user, ids, context=None):
148                 self.clear()
149                 return super(ir_translation, self).unlink(cusor, user, ids,
150                                 context=context)
151
152         def create(self, cursor, user, vals, context=None):
153                 self.clear()
154                 return super(ir_translation, self).create(cursor, user, vals,
155                                 context=context)
156
157         def write(self, cursor, user, ids, vals, context=None):
158                 self.clear()
159                 return super(ir_translation, self).write(cursor, user, ids, vals,
160                                 context=context)
161
162 ir_translation()