I want to get a field translation in a specific language if it exists.
I saw some code that is just updating the context language before getting the field value so I tried but it’s returning me the default language value:
context = {}
if self.purchase and self.purchase.party and self.purchase.party.lang:
context['language'] = self.purchase.party.lang.code
with Transaction().set_context(context):
self.product_supplier.name
Assuming that exists a translation for the product supplier name and also that I am setting the expected lang code, should it works or I am missing something else?
You should re-browse the instance with the proper context. Instances keep a copy of the context where they had been initialized and always use this context.
So your code should be updated to:
ProductSuplier = Pool().get(...)
context = {}
if self.purchase and self.purchase.party and self.purchase.party.lang:
context['language'] = self.purchase.party.lang.code
with Transaction().set_context(context):
product_supplier = ProductSupplier(self.product_supplier.di)
return product_supplier.name