2008-07-23 15:01:27 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
2006-12-07 13:41:40 +00:00
|
|
|
##############################################################################
|
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
|
|
|
|
# $Id$
|
2008-06-16 11:00:21 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from osv import fields, osv
|
|
|
|
from osv.osv import Cacheable
|
2007-10-24 06:13:53 +00:00
|
|
|
import tools
|
|
|
|
|
|
|
|
TRANSLATION_TYPE = [
|
2008-07-22 14:24:36 +00:00
|
|
|
('field', 'Field'),
|
2008-08-21 13:49:55 +00:00
|
|
|
('model', 'Object'),
|
2008-07-22 14:24:36 +00:00
|
|
|
('rml', 'RML'),
|
|
|
|
('selection', 'Selection'),
|
|
|
|
('view', 'View'),
|
|
|
|
('wizard_button', 'Wizard Button'),
|
|
|
|
('wizard_field', 'Wizard Field'),
|
|
|
|
('wizard_view', 'Wizard View'),
|
|
|
|
('xsl', 'XSL'),
|
|
|
|
('help', 'Help'),
|
|
|
|
('code', 'Code'),
|
|
|
|
('constraint', 'Constraint'),
|
2007-10-24 06:13:53 +00:00
|
|
|
]
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
class ir_translation(osv.osv, Cacheable):
|
2008-07-22 14:24:36 +00:00
|
|
|
_name = "ir.translation"
|
|
|
|
_log_access = False
|
2007-10-24 06:13:53 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def _get_language(self, cr, uid, context):
|
|
|
|
lang_obj = self.pool.get('res.lang')
|
|
|
|
lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)],
|
|
|
|
context=context)
|
|
|
|
langs = lang_obj.browse(cr, uid, lang_ids, context=context)
|
2008-12-11 10:17:53 +00:00
|
|
|
res = [(lang.code, lang.name) for lang in langs]
|
2008-07-22 14:24:36 +00:00
|
|
|
for lang_dict in tools.scan_languages():
|
|
|
|
if lang_dict not in res:
|
|
|
|
res.append(lang_dict)
|
|
|
|
return res
|
2007-10-24 06:13:53 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
_columns = {
|
|
|
|
'name': fields.char('Field Name', size=128, required=True),
|
2008-07-31 12:13:54 +00:00
|
|
|
'res_id': fields.integer('Resource ID', select=True),
|
2008-07-22 14:24:36 +00:00
|
|
|
'lang': fields.selection(_get_language, string='Language', size=5),
|
2008-07-31 12:13:54 +00:00
|
|
|
'type': fields.selection(TRANSLATION_TYPE, string='Type', size=16, select=True),
|
2008-07-22 14:24:36 +00:00
|
|
|
'src': fields.text('Source'),
|
|
|
|
'value': fields.text('Translation Value'),
|
|
|
|
}
|
2008-07-31 12:13:54 +00:00
|
|
|
|
|
|
|
def _auto_init(self, cr, context={}):
|
|
|
|
super(ir_translation, self)._auto_init(cr, context)
|
|
|
|
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltns',))
|
|
|
|
if not cr.fetchone():
|
|
|
|
cr.execute('CREATE INDEX ir_translation_ltns ON ir_translation (lang, type, name, src)')
|
|
|
|
cr.commit()
|
|
|
|
|
|
|
|
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltn',))
|
|
|
|
if not cr.fetchone():
|
|
|
|
cr.execute('CREATE INDEX ir_translation_ltn ON ir_translation (lang, type, name)')
|
|
|
|
cr.commit()
|
|
|
|
|
|
|
|
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_lts',))
|
|
|
|
if not cr.fetchone():
|
|
|
|
cr.execute('CREATE INDEX ir_translation_lts ON ir_translation (lang, type, src)')
|
|
|
|
cr.commit()
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def _get_ids(self, cr, uid, name, tt, lang, ids):
|
|
|
|
translations, to_fetch = {}, []
|
|
|
|
for id in ids:
|
|
|
|
trans = self.get((lang, name, id))
|
|
|
|
if trans is not None:
|
|
|
|
translations[id] = trans
|
|
|
|
else:
|
|
|
|
to_fetch.append(id)
|
|
|
|
if to_fetch:
|
|
|
|
cr.execute('select res_id,value ' \
|
|
|
|
'from ir_translation ' \
|
|
|
|
'where lang=%s ' \
|
|
|
|
'and type=%s ' \
|
|
|
|
'and name=%s ' \
|
|
|
|
'and res_id in ('+','.join(map(str, to_fetch))+')',
|
|
|
|
(lang,tt,name))
|
|
|
|
for res_id, value in cr.fetchall():
|
|
|
|
self.add((lang, tt, name, res_id), value)
|
|
|
|
translations[res_id] = value
|
|
|
|
for res_id in ids:
|
|
|
|
if res_id not in translations:
|
|
|
|
self.add((lang, tt, name, res_id), False)
|
|
|
|
translations[res_id] = False
|
|
|
|
return translations
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def _set_ids(self, cr, uid, name, tt, lang, ids, value):
|
|
|
|
cr.execute('delete from ir_translation ' \
|
|
|
|
'where lang=%s ' \
|
|
|
|
'and type=%s ' \
|
|
|
|
'and name=%s ' \
|
|
|
|
'and res_id in ('+','.join(map(str,ids))+')',
|
|
|
|
(lang,tt,name))
|
|
|
|
for id in ids:
|
|
|
|
self.create(cr, uid, {
|
|
|
|
'lang':lang,
|
|
|
|
'type':tt,
|
|
|
|
'name':name,
|
|
|
|
'res_id':id,
|
|
|
|
'value':value,
|
|
|
|
})
|
|
|
|
return len(ids)
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def _get_source(self, cr, uid, name, tt, lang, source=None):
|
|
|
|
trans = self.get((lang, tt, name, source))
|
|
|
|
if trans is not None:
|
|
|
|
return trans
|
2007-11-26 15:46:03 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
if source:
|
|
|
|
#if isinstance(source, unicode):
|
|
|
|
# source = source.encode('utf8')
|
|
|
|
cr.execute('select value ' \
|
|
|
|
'from ir_translation ' \
|
|
|
|
'where lang=%s ' \
|
|
|
|
'and type=%s ' \
|
|
|
|
'and name=%s ' \
|
|
|
|
'and src=%s',
|
|
|
|
(lang, tt, str(name), source))
|
|
|
|
else:
|
|
|
|
cr.execute('select value ' \
|
|
|
|
'from ir_translation ' \
|
|
|
|
'where lang=%s ' \
|
|
|
|
'and type=%s ' \
|
|
|
|
'and name=%s',
|
|
|
|
(lang, tt, str(name)))
|
|
|
|
res = cr.fetchone()
|
|
|
|
|
|
|
|
trad = res and res[0] or ''
|
|
|
|
self.add((lang, tt, name, source), trad)
|
|
|
|
return trad
|
2007-11-26 15:46:03 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def unlink(self, cursor, user, ids, context=None):
|
|
|
|
self.clear()
|
2008-10-28 14:25:23 +00:00
|
|
|
return super(ir_translation, self).unlink(cursor, user, ids,
|
2008-07-22 14:24:36 +00:00
|
|
|
context=context)
|
2007-11-26 15:46:03 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def create(self, cursor, user, vals, context=None):
|
|
|
|
self.clear()
|
|
|
|
return super(ir_translation, self).create(cursor, user, vals,
|
|
|
|
context=context)
|
2007-11-26 15:46:03 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def write(self, cursor, user, ids, vals, context=None):
|
|
|
|
self.clear()
|
|
|
|
return super(ir_translation, self).write(cursor, user, ids, vals,
|
|
|
|
context=context)
|
2007-11-26 15:46:03 +00:00
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
ir_translation()
|
2008-07-23 15:01:27 +00:00
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
|
|
|