Access multiple translations of a field in Tryton
written on Friday, June 3, 2016
At work, we are currently migrating away from a proprietary ERP software package to Tryton. There are several reasons why we decided to switch and one them is that Tryton provides an API and sane data access. Being a free software project, there are libraries and tools available to get access to the data. This is a good foundation to build your own tools on top.
Tryton provides a client library called Proteus for programmatic data access. The first tool that I built on top of Proteus is called pedantic bot. It nags about inconsistencies and errors in the database. For example, the pedantic bot checks for the following issues:
- Leading/trailing whitespace in fields
- Control characters in fields
- Inconsistent formatting of fields (phone numbers, …)
- Incomplete data records (e.g. contact information is missing)
- Missing descriptions
- …
Tryton allows to translate some of the fields where it makes sense to have them available in multiple languages (product name, a description, payment terms, …). The pedantic bot should check all the translations alike. It took me a while to figure out how to get access to a field in a particular translation, so here is a short demo on how to accomplish it in Tryton 3.8. It should work on other versions too:
#!/usr/bin/python2 from proteus import config from proteus import Model def main(username, password, host, port, db): # Connect to Tryton. current_config = config.set_xmlrpc( 'https://{}:{}@{}:{}/{}'.format(username, password, host, port, db)) # Get the product model. Product = Model.get('product.product') # Print the product information in de_DE. with current_config.set_context({'language': 'de_DE'}): for record in Product.find(): print(u'Name: {}'.format(record.rec_name)) print(u'Description: {}'.format(record.description)) # Print the product information in en_US. with current_config.set_context({'language': 'en_US'}): for record in Product.find(): print(u'Name: {}'.format(record.rec_name)) print(u'Description: {}'.format(record.description)) # Print the product information in the language that is configured for the # connected user. for record in Product.find(): print(u'Name: {}'.format(record.rec_name)) print(u'Description: {}'.format(record.description)) if __name__ == '__main__': main('username', 'password', 'hostname', '9000', 'db')
Save this script as example.py, install Proteus and run:
$ python example.py
The solution is to set the desired language in the context: with current_config.set_context({'language': 'en_US'}). Have fun.