Imagine you want to fill a field in the sale.order
form with a default value, the same text will be used for each company that is being used in Odoo. Sometimes the usual way to proceed is to use a common field in the res.company
model. Other option is to add a field that is filled with some other content in the res.partner
form, when the customer is selected.
But the problem I found is: if I want to get the translations for that field, which are already in the original one, I would have to do it manually, inheriting the create method of ir.translation
. When the field is copied to the sale.order
form a record for the current used language is created, so I took advantage of it to create the rest of records. Is there a better way to do this?
Also, I would like to add that after saving the record I press the world icon to translate the text again, new translations are created with the current text. So I would need to translate everything again on every sale.order
record.
I did this in the ir.translation
model for a simple text field in order to create the translations of the rest of languages. The field field_to_translate
is translatable on both sides:
@api.model
def create(self, vals):
record = super(IrTranslation, self).create(vals)
name = vals.get('name', False) # name of the field to translate
lang = vals.get('lang', False) # creating record for this language
if 'context' in dir(self.env):
cur_lang = self.env.context.get('lang', False) # current used language
if name == 'sale.order,field_to_translate' and lang == cur_lang:
langs = self.env['ir.translation']._get_languages()
langs = [l[0] for l in langs if l[0] != cur_lang] # installed languages
for l in langs:
if self.env.user.company_id.field_to_translate:
t = self.env['ir.translation'].search([
('lang', '=', l),
('type', '=', 'model'),
('name', '=', 'res.company,field_to_translate')
])
if t:
self.env['ir.translation'].create({
'lang': l,
'type': 'model',
'name': 'sale.order,field_to_translate',
'res_id': record.res_id,
'src': record.src,
'value': t.value,
'state': 'translated',
})
Ah, and I want to do this because I want to print them on different reports. The language of these report will depend on the customer lang
field.
In short, how can I set a translatable field in res.company
as a default value in other field in the sale.order
model? The translations to other languages should be copied as well. My above proposal is kind of cumbersome