[REF+IMP] l10n_multilang: code refactoring in order to split the big method into smaller pieces + improved the way the translations are handled (use of spoken_languages, can force the write if we don't want to create a translation but replace the current value...)

bzr revid: qdp-launchpad@openerp.com-20111206114411-rqcc8qvqo342u4y2
This commit is contained in:
Quentin (OpenERP) 2011-12-06 12:44:11 +01:00
parent aa68947a46
commit ae74054b5a
2 changed files with 81 additions and 98 deletions

View File

@ -22,7 +22,8 @@
from osv import fields, osv
import os
from tools.translate import _
import netsvc
logger=netsvc.Logger()
class wizard_multi_charts_accounts(osv.osv_memory):
"""
@ -32,20 +33,27 @@ class wizard_multi_charts_accounts(osv.osv_memory):
"""
_inherit = 'wizard.multi.charts.accounts'
def copy_translations(self, cr, uid, langs, in_obj, in_field, in_ids, out_obj, out_ids):
def process_translations(self, cr, uid, langs, in_obj, in_field, in_ids, out_obj, out_ids, force_write=False, context=None):
"""
This method copies translations values of templates into new Accounts/Taxes/Journals for languages selected
@param cr: A database cursor
@param uid: ID of the user currently logged in
@param langs: List of languages to load for new records
@param in_field: Name of the translatable field of source templates
@param in_obj: Name of source object of templates.
@param in_ids: List of ids of source object
@param out_obj: Destination object for which translation is to be copied
@param out_ids: List of ids of destination object
@param Return: String containing information of the translations loaded.
:param cr: A database cursor
:param uid: ID of the user currently logged in
:param langs: List of languages to load for new records
:param in_field: Name of the translatable field of source templates
:param in_obj: Name of source object of templates.
:param in_ids: List of ids of source object
:param out_obj: Destination object for which translation is to be copied
:param out_ids: List of ids of destination object
:param force_write: boolean that depicts if we need to create a translation OR simply replace the actual value
with the translation in the uid's language by doing a write (in case it's TRUE)
:param context: usual context information. May contain the key 'lang', which is the language of the user running
the wizard, that will be used if force_write is True
:return: True
"""
if context is None:
context = {}
src = {}
xlat_obj = self.pool.get('ir.translation')
#find the source from Account Template
@ -57,101 +65,90 @@ class wizard_multi_charts_accounts(osv.osv_memory):
for j in range(len(in_ids)):
in_id = in_ids[j]
if value[in_id]:
#copy Translation from Source to Destination object
xlat_obj.create(cr, uid, {
'name': out_obj._name + ',' + in_field,
'type': 'model',
'res_id': out_ids[j],
'lang': lang,
'src': src[in_id],
'value': value[in_id],
})
if not force_write:
#copy Translation from Source to Destination object
xlat_obj.create(cr, uid, {
'name': out_obj._name + ',' + in_field,
'type': 'model',
'res_id': out_ids[j],
'lang': lang,
'src': src[in_id],
'value': value[in_id],
})
else:
#replace the value in the destination object only if it's the user lang
if context.get('lang') == lang:
self.pool.get(out_obj._name).write(cr, uid, out_ids[j], {in_field: value[in_id]})
else:
logger.notifyChannel('addons.'+self._name, netsvc.LOG_ERROR,
'Language: %s. Translation from template: there is no translation available for %s!' %(lang, out_obj._description))
logger.notifyChannel('addons.'+self._name, netsvc.LOG_WARNING,
'Language: %s. Translation from template: there is no translation available for %s!' %(lang, src[in_id]))#out_obj._name))
return True
def execute(self, cr, uid, ids, context=None):
res = super(wizard_multi_charts_accounts, self).execute(cr, uid, ids, context=context)
obj_multi = self.browse(cr, uid, ids[0], context=context)
obj_mod = self.pool.get('ir.module.module')
obj_acc_template = self.pool.get('account.account.template')
obj_acc = self.pool.get('account.account')
obj_tax_code_template = self.pool.get('account.tax.code.template')
obj_tax_code = self.pool.get('account.tax.code')
obj_tax_template = self.pool.get('account.tax.template')
obj_tax = self.pool.get('account.tax')
obj_fiscal_position_template = self.pool.get('account.fiscal.position.template')
obj_fiscal_position = self.pool.get('account.fiscal.position')
obj_multi = self.browse(cr, uid, ids[0], context=context)
company_id = obj_multi.company_id.id
acc_template_root_id = obj_multi.chart_template_id.account_root_id.id
acc_root_id = obj_acc.search(cr, uid, [('company_id', '=', company_id), ('parent_id', '=', None)])[0]
tax_code_template_root_id = obj_multi.chart_template_id.tax_code_root_id.id
tax_code_root_id = obj_tax_code.search(cr, uid, [('company_id', '=', company_id), ('parent_id', '=', None)])[0]
# load languages
langs = []
installed_mids = obj_mod.search(cr, uid, [('state', '=', 'installed')])
for lang in obj_multi.lang_ids:
langs.append(lang.code)
obj_mod.update_translations(cr, uid, installed_mids, lang.code)
res_lang_obj = self.pool.get('res.lang')
installed_lang_ids = res_lang_obj.search(cr, uid, [])
installed_langs = [x.code for x in res_lang_obj.browse(cr, uid, installed_lang_ids, context=context)]
for lang in obj_multi.chart_template_id.spoken_languages.split(';'):
if lang not in installed_langs:
# the language is not installed, so we don't need to load its translations
continue
else:
# the language was already installed, so the po files have been loaded at the installation time
# and now we need to copy the translations of templates to the right objects
langs.append(lang)
if langs:
# write account.account translations in the real COA
self._process_accounts_translations(cr, uid, obj_multi, company_id, langs, 'name', context=context)
# copy account.account translations
# copy account.tax.code translations
self._process_tax_codes_translations(cr, uid, obj_multi, company_id, langs, 'name', context=context)
# copy account.tax translations
self._process_taxes_translations(cr, uid, obj_multi, company_id, langs, 'name', context=context)
# copy account.fiscal.position translations
self.process_fiscal_pos_translations(cr, uid, obj_multi, company_id, langs, 'name', context=context)
return res
def _process_accounts_translations(self, cr, uid, obj_multi, company_id, langs, field, context=None):
obj_acc_template = self.pool.get('account.account.template')
obj_acc = self.pool.get('account.account')
acc_template_root_id = obj_multi.chart_template_id.account_root_id.id
acc_root_id = obj_acc.search(cr, uid, [('company_id', '=', company_id), ('parent_id', '=', None)])[0]
in_ids = obj_acc_template.search(cr, uid, [('id', 'child_of', [acc_template_root_id])], order='id')[1:]
out_ids = obj_acc.search(cr, uid, [('id', 'child_of', [acc_root_id])], order='id')[1:]
self.copy_translations(cr, uid, langs, obj_acc_template, 'name', in_ids, obj_acc, out_ids)
return self.process_translations(cr, uid, langs, obj_acc_template, field, in_ids, obj_acc, out_ids, force_write=True, context=context)
# copy account.tax.code translations
def _process_tax_codes_translations(self, cr, uid, obj_multi, company_id, langs, field, context=None):
obj_tax_code_template = self.pool.get('account.tax.code.template')
obj_tax_code = self.pool.get('account.tax.code')
tax_code_template_root_id = obj_multi.chart_template_id.tax_code_root_id.id
tax_code_root_id = obj_tax_code.search(cr, uid, [('company_id', '=', company_id), ('parent_id', '=', None)])[0]
in_ids = obj_tax_code_template.search(cr, uid, [('id', 'child_of', [tax_code_template_root_id])], order='id')[1:]
out_ids = obj_tax_code.search(cr, uid, [('id', 'child_of', [tax_code_root_id])], order='id')[1:]
self.copy_translations(cr, uid, langs, obj_tax_code_template, 'name', in_ids, obj_tax_code, out_ids)
return self.process_translations(cr, uid, langs, obj_tax_code_template, field, in_ids, obj_tax_code, out_ids, force_write=False, context=context)
# copy account.tax translations
def _process_taxes_translations(self, cr, uid, obj_multi, company_id, langs, field, context=None):
obj_tax_template = self.pool.get('account.tax.template')
obj_tax = self.pool.get('account.tax')
in_ids = sorted([x.id for x in obj_multi.chart_template_id.tax_template_ids])
out_ids = obj_tax.search(cr, uid, [('company_id', '=', company_id)], order='id')
self.copy_translations(cr, uid, langs, obj_tax_template, 'name', in_ids, obj_tax, out_ids)
return self.process_translations(cr, uid, langs, obj_tax_template, field, in_ids, obj_tax, out_ids, force_write=False, context=context)
# copy account.fiscal.position translations
def _process_fiscal_pos_translations(self, cr, uid, obj_multi, company_id, langs, field, context=None):
obj_fiscal_position_template = self.pool.get('account.fiscal.position.template')
obj_fiscal_position = self.pool.get('account.fiscal.position')
in_ids = obj_fiscal_position_template.search(cr, uid, [('chart_template_id', '=', obj_multi.chart_template_id.id)], order='id')
out_ids = obj_fiscal_position.search(cr, uid, [('company_id', '=', company_id)], order='id')
self.copy_translations(cr, uid, langs, obj_fiscal_position_template, 'name', in_ids, obj_fiscal_position, out_ids)
return res
def onchange_chart_template_id(self, cr, uid, ids, chart_template_id=False, context=None):
res = super(wizard_multi_charts_accounts, self).onchange_chart_template_id(cr, uid, ids, chart_template_id, context=context)
installed_lang = self.get_lang(cr, uid, chart_template_id, context=context)
res['value'].update({'lang_ids': installed_lang})
return res
def get_lang(self, cr, uid, template_id=False, context=None):
installed_lang = []
if template_id:
cr.execute("SELECT module from ir_model_data where model='account.chart.template' and res_id=%s" % (template_id))
modulename = cr.fetchone()
modulename = modulename and modulename[0] or False
if modulename:
module_obj = self.pool.get('ir.module.module')
module_id = module_obj.search(cr, uid, [('name', '=', modulename)], context=context)
module = module_obj.browse(cr, uid, module_id, context=context)[0]
dirpath = module_obj._translations_subdir(module)
if dirpath:
for po in os.listdir(dirpath):
lang_id = self.pool.get('res.lang').search(cr, uid, [('code', 'ilike', '%s' % (po.split('.')[0])), ('translatable', '=', True)], context=context)
if lang_id:
installed_lang.append(lang_id[0])
return installed_lang
def default_get(self, cr, uid, fields, context=None):
res = super(wizard_multi_charts_accounts, self).default_get(cr, uid, fields, context=context)
installed_lang = self.get_lang(cr, uid, res.get('chart_template_id'), context=context)
res.update({'lang_ids': installed_lang, 'bank_accounts_id': []})
return res
_columns = {
'lang_ids': fields.many2many('res.lang', 'res_lang_type_rel', 'wizard_id', 'lang_id', 'Language'),
}
return self.process_translations(cr, uid, langs, obj_fiscal_position_template, field, in_ids, obj_fiscal_position, out_ids, force_write=False, context=context)
wizard_multi_charts_accounts()

View File

@ -6,20 +6,6 @@
<field name="sequence">25</field>
<field name="restart">onskip</field>
</record>
<!-- Wizard for Multi Charts of Accounts -->
<record id="view_wizard_multi_chart_belgian_coa" model="ir.ui.view">
<field name="name">view.wizard.multi.chart</field>
<field name="model">wizard.multi.charts.accounts</field>
<field name="inherit_id" ref="account.view_wizard_multi_chart"/>
<field name="arch" type="xml">
<data>
<field name="bank_accounts_id" position="before">
<separator string="Languages" colspan="4" groups="base.group_extended"/>
<field name="lang_ids" colspan="4" nolabel="1" groups="base.group_extended"/>
</field>
</data>
</field>
</record>
</data>
</openerp>