[IMP] translations: parse views iteratively instead of recursively
[odoo/odoo.git] / addons / base_setup / base_setup.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 import simplejson
23 import cgi
24 from openerp import tools
25 from openerp.osv import fields, osv
26 from openerp.tools.translate import _
27 from lxml import etree
28
29 # Specify Your Terminology will move to 'partner' module
30 class specify_partner_terminology(osv.osv_memory):
31     _name = 'base.setup.terminology'
32     _inherit = 'res.config'
33     _columns = {
34         'partner': fields.selection([
35             ('Customer','Customer'),
36             ('Client','Client'),
37             ('Member','Member'),
38             ('Patient','Patient'),
39             ('Partner','Partner'),
40             ('Donor','Donor'),
41             ('Guest','Guest'),
42             ('Tenant','Tenant')
43         ], 'How do you call a Customer', required=True ),
44     }
45     _defaults={
46         'partner' :'Customer',
47     }
48
49     def make_translations(self, cr, uid, ids, name, type, src, value, res_id=0, context=None):
50         trans_obj = self.pool.get('ir.translation')
51         user_obj = self.pool.get('res.users')
52         context_lang = user_obj.browse(cr, uid, uid, context=context).lang
53         existing_trans_ids = trans_obj.search(cr, uid, [('name','=',name), ('lang','=',context_lang), ('type','=',type), ('src','=',src), ('res_id','=',res_id)])
54         if existing_trans_ids:
55             trans_obj.write(cr, uid, existing_trans_ids, {'value': value}, context=context)
56         else:
57             create_id = trans_obj.create(cr, uid, {'name': name,'lang': context_lang, 'type': type, 'src': src, 'value': value , 'res_id': res_id}, context=context)
58         return {}
59
60     def execute(self, cr, uid, ids, context=None):
61         def _case_insensitive_replace(ref_string, src, value):
62             import re
63             pattern = re.compile(src, re.IGNORECASE)
64             return pattern.sub(_(value), _(ref_string))
65         trans_obj = self.pool.get('ir.translation')
66         fields_obj = self.pool.get('ir.model.fields')
67         menu_obj = self.pool.get('ir.ui.menu')
68         act_window_obj = self.pool.get('ir.actions.act_window')
69         for o in self.browse(cr, uid, ids, context=context):
70             #translate label of field
71             field_ids = fields_obj.search(cr, uid, [('field_description','ilike','Customer')])
72             for f_id in fields_obj.browse(cr ,uid, field_ids, context=context):
73                 field_ref = f_id.model_id.model + ',' + f_id.name
74                 self.make_translations(cr, uid, ids, field_ref, 'field', f_id.field_description, _case_insensitive_replace(f_id.field_description,'Customer',o.partner), context=context)
75             #translate help tooltip of field
76             for obj in self.pool.models.values():
77                 for field_name, field_rec in obj._columns.items():
78                     if field_rec.help.lower().count('customer'):
79                         field_ref = obj._name + ',' + field_name
80                         self.make_translations(cr, uid, ids, field_ref, 'help', field_rec.help, _case_insensitive_replace(field_rec.help,'Customer',o.partner), context=context)
81             #translate menuitems
82             menu_ids = menu_obj.search(cr,uid, [('name','ilike','Customer')])
83             for m_id in menu_obj.browse(cr, uid, menu_ids, context=context):
84                 menu_name = m_id.name
85                 menu_ref = 'ir.ui.menu' + ',' + 'name'
86                 self.make_translations(cr, uid, ids, menu_ref, 'model', menu_name, _case_insensitive_replace(menu_name,'Customer',o.partner), res_id=m_id.id, context=context)
87             #translate act window name
88             act_window_ids = act_window_obj.search(cr, uid, [('name','ilike','Customer')])
89             for act_id in act_window_obj.browse(cr ,uid, act_window_ids, context=context):
90                 act_ref = 'ir.actions.act_window' + ',' + 'name'
91                 self.make_translations(cr, uid, ids, act_ref, 'model', act_id.name, _case_insensitive_replace(act_id.name,'Customer',o.partner), res_id=act_id.id, context=context)
92             #translate act window tooltips
93             act_window_ids = act_window_obj.search(cr, uid, [('help','ilike','Customer')])
94             for act_id in act_window_obj.browse(cr ,uid, act_window_ids, context=context):
95                 act_ref = 'ir.actions.act_window' + ',' + 'help'
96                 self.make_translations(cr, uid, ids, act_ref, 'model', act_id.help, _case_insensitive_replace(act_id.help,'Customer',o.partner), res_id=act_id.id, context=context)
97         return {}
98
99 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: