Skip to Content
Menu
This question has been flagged
2 Replies
2402 Zobrazenia

I have made a code that gets the translation value of the name field from the database and allows you to edit it and change it in the database, this code is not very well written (because this is the first time I work directly with the database), but it works for all languages and for all cases when the form is already created, but I can't figure out how to add this code so that it works when creating a new form. I was thinking of making the nameUA, nameRU... fields not count during creation, but only during editing, but I haven't figured out how to do it.

Here are my fields:

name = fields.Char('Name', index=True, required=True, translate=True)
nameUA = fields.Char('Name Ua', default=name, compute='_compute_name_ukr', required=True, readonly=False)
nameRU = fields.Char('Name Ru', default=name, compute='_compute_name_rus', required=True, readonly=False)
nameDE = fields.Char('Name De', default=name, compute='_compute_name_deu', required=True, readonly=False)
nameEN = fields.Char('Name En', default=name, compute='_compute_name_eng', required=True, readonly=False)

Here is my implementation code:

def _compute_name_ukr(self):
for template in self:
template.env.cr.execute(
f"""SELECT src FROM ir_translation WHERE value = '{template.name}';""")
src = template.env.cr.fetchall()[0][0]
template.env.cr.execute(f"""SELECT value FROM ir_translation WHERE src = '{src}' and lang ='uk_UA';""")
name = template.env.cr.fetchall()[0][0]
if name == '':
template.env.cr.execute(
f"""SELECT value FROM ir_translation WHERE src = '{src}' and lang ='{template.env.user.lang}';""")
name = template.env.cr.fetchall()[0][0]
template.env.cr.execute(
f"""UPDATE ir_translation SET value ='{name}' WHERE src = '{template.nameEN}' and lang ='uk_UA';""")
template.nameUA = name

@api.onchange('nameUA')
def onchange_name_ukr(self):
for template in self:
template.env.cr.execute(f"""UPDATE ir_translation SET value ='{template.nameUA}' WHERE src = '{template.nameEN}' and lang ='uk_UA';""")

def _compute_name_deu(self):
for template in self:
template.env.cr.execute(
f"""SELECT src FROM ir_translation WHERE value = '{template.name}';""")
src = template.env.cr.fetchall()[0][0]
template.env.cr.execute(f"""SELECT value FROM ir_translation WHERE src = '{src}' and lang ='de_DE';""")
name = template.env.cr.fetchall()[0][0]
if name == '':
template.env.cr.execute(
f"""SELECT value FROM ir_translation WHERE src = '{src}' and lang ='{template.env.user.lang}';""")
name = template.env.cr.fetchall()[0][0]
template.env.cr.execute(
f"""UPDATE ir_translation SET value ='{name}' WHERE src = '{src}' and lang ='de_DE';""")
template.nameDE = name

@api.onchange('nameDE')
def onchange_name_deu(self):
for template in self:
template.env.cr.execute(f"""UPDATE ir_translation SET value ='{template.nameDE}' WHERE src = '{template.nameEN}' and lang ='de_DE';""")

def _compute_name_rus(self):
for template in self:
template.env.cr.execute(
f"""SELECT src FROM ir_translation WHERE value = '{template.name}';""")
src = template.env.cr.fetchall()[0][0]
template.env.cr.execute(f"""SELECT value FROM ir_translation WHERE src = '{src}' and lang ='ru_RU';""")
name = template.env.cr.fetchall()[0][0]
if name == '':
template.env.cr.execute(
f"""SELECT value FROM ir_translation WHERE src = '{src}' and lang ='{template.env.user.lang}';""")
name = template.env.cr.fetchall()[0][0]
template.env.cr.execute(
f"""UPDATE ir_translation SET value ='{name}' WHERE src = '{src}' and lang ='ru_RU';""")
template.nameRU = name

@api.onchange('nameRU')
def onchange_name_rus(self):
for template in self:
template.env.cr.execute(f"""UPDATE ir_translation SET value ='{template.nameRU}' WHERE src = '{template.nameEN}' and lang ='ru_RU';""")
def _compute_name_eng(self):
for template in self:
template.env.cr.execute(
f"""SELECT src FROM ir_translation WHERE value = '{template.nameRU}';""")
src = template.env.cr.fetchall()[0][0]
template.nameEN = src

@api.onchange('nameEN')
def onchange_name_eng(self):
for template in self:
template.env.cr.execute(
f"""SELECT src FROM ir_translation WHERE value = '{template.nameDE}';""")
src = template.env.cr.fetchall()[0][0]
template.env.cr.execute(
f"""UPDATE ir_translation
SET value ='
{template.nameEN}'
WHERE src = '
{src}' and lang ='en_US';
""")
template.env.cr.execute(
f"""UPDATE ir_translation
SET src ='
{template.nameEN}'
WHERE value ='
{template.nameEN}' and lang ='en_US';
""")
template.env.cr.execute(
f"""UPDATE ir_translation
SET src ='
{template.nameEN}'
WHERE value ='
{template.nameUA}' and lang ='uk_UA';
""")
template.env.cr.execute(
f"""UPDATE ir_translation
SET src ='
{template.nameEN}'
WHERE value ='
{template.nameRU}' and lang ='ru_RU';
""")
template.env.cr.execute(
f"""UPDATE ir_translation
SET src ='
{template.nameEN}'
WHERE value ='
{template.nameDE}' and lang ='de_DE';
""")

template.env.cr.execute(
f"""SELECT src FROM ir_translation WHERE value = '{template.nameRU}';""")
src = template.env.cr.fetchall()[0][0]
template.nameEN = src

@api.onchange('name')
def name_onchange(self):
for template in self:
if template.env.user.lang == 'de_DE':
template.nameDE = template.name
if template.env.user.lang == 'uk_UA':
template.nameUA = template.name
if template.env.user.lang == 'ru_RU':
template.nameRU = template.name
if template.env.user.lang == 'en_US':
template.nameEN = template.name

If anyone can give me a hint to solve my problem I will be most grateful!

Avatar
Zrušiť
Best Answer

Odoo provides built-in support for adding a translation term for translated fields, what's your use case?





I think it's not a good idea to create compute fields for translation,  it would be better to create a new form/widget in js and handle the translation based on context like the translation dialog box did 
ref: https://github.com/odoo/odoo/blob/14.0/addons/web/static/src/js/widgets/translation_dialog.js#L10
https://github.com/odoo/odoo/blob/14.0/addons/web/static/src/js/widgets/translation_dialog.js#L166

Avatar
Zrušiť
Autor

I know that it can be done with translate=True, but I need to do it in the form, not in a separate window.

Best Answer

Hi,

Instead didn’t you try the builtin feature of translation provider by odoo, you can add the field parameter translate=True,

Just redifine your field like this,

name = fields.Char('Name', index=True, required=True, translate=True)
 For the time being, remove the unnecessary onchange and custom fields.

Now, activate the developer mode, settings -> translations -> in the application terms view filter the terms by your module and search for the name field. You will see all the translations for the name field in different languages and can edit here so that odoo will handle the translation for this field based on the users language preference.

Thanks

Avatar
Zrušiť
Autor

I know that it can be done with translate=True, but I need to do it in the form, not in a separate window.

Related Posts Replies Zobrazenia Aktivita
0
aug 21
111
3
dec 21
15429
1
mar 15
7282
2
jún 25
1608
0
máj 25
14