Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to load translated fields through the web services ?

By
Chloé Desoutter
on 6/28/13, 8:32 AM 1,747 views

Hello,

We're working on integrating OpenERP as backoffice for e-commerce solutions. One important point is to be able to load product descriptions from OpenERP to the multilingual e-commerce website.

For the moment my code looks like

products = o.join(o.search_and_load('product.product', None, None, product_structure()), 'product_tmpl_id', 'product.template', product_template_structure())

which, basically, performs a join between product.product and product.template to load the full product informations and returns a dictionary.

Is there any facility in the webservices to load the translated fields directly or should we implement it ourselves?

If that'd be so, we'd load a translation cache in our middleware and perform string replacements when we get the dictionary.

Any hint?

0
Chloé Desoutter
On 7/1/13, 3:14 PM

Since no one seems to have a hint at hand, I have produced some (quick & dirty) code to perform this operation.

It's not universal, but it is quite straightforward and can be adapted easily to your needs. Its weak point is that it does not translate recursively "join"ed structures.

This code is not at all production-ready! as it does not cache locally the translations. A cache should be added to be ready for production.

  def translate(self, dataset, language_code, model):
    if self.__transcache == None: self._load_tcache()
    langset = self.__transcache[language_code][model]
    for i in dataset:
      id_ = i['id']
      for field in i:
#        print "Seeking field %s with ID %s" % (field, id_)
        if field in langset:
          sid_ = str(id_)
          if sid_ in langset[field]:
#            print "Found translation for %s[%s]:%s" % (model, field, id_)
            i[field] = langset[field][sid_]
    return dataset

  def _load_tcache(self):
    r = self.search_and_load('ir.translation', fields = ['lang', 'module', 'name', 'res_id', 'value'])
    l = {}
    for i in r:
      lng = i['lang']
      nam = i['name'].split(',')
      if (len(nam)<2): continue
      module = nam[0]
      field = nam[1]

      if not lng in l: l[lng] = {}
      if not module in l[lng]: l[lng][module] = {}
      if not field in l[lng][module]: l[lng][module][field] = {}
#      print "Added cache for %s[%s][%s][%s]: %s" % (lng, module, field, i['res_id'], unicode(i['value']).encode('utf-8'))
      l[lng][module][field][str(i['res_id'])] = i['value']
    self.__transcache = l

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 6/28/13, 8:32 AM
Seen: 1747 times
Last updated: 3/16/15, 8:10 AM