This question has been flagged
1 Reply
4800 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?

Avatar
Discard
Author Best Answer

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
Avatar
Discard