forvas
forvas

Reputation: 10189

Get product's name in the local language in OpenERP?

This is a very simple but a difficult question to answer, because I think only a "few" people work with this.

I have this simple script in Python:

import xmlrpclib

username = 'my_openerp_user'
pwd = 'my_password'
dbname = 'my_openerp_database'

sock = xmlrpclib.ServerProxy('http://localhost:8063/xmlrpc/common')
uid = sock.login(dbname, username, pwd)
sock = xmlrpclib.ServerProxy('http://localhost:8063/xmlrpc/object')

args = [('name', 'ilike', 'my_product')]
ids = sock.execute(dbname, uid, pwd, 'product.product', 'search', args)

print ids

It should find all the products in the OpenERP database with the name 'my_product', but it does not. And I know why:

I am not in a country in which English is talked, so I have one language installed in OpenERP, and the 'search' of xmlrpc is looking for products with name 'my_product' but only in english. The problem is that apparently there is not a field to save the translated name... It seems to be 'name' too! So I can't find the products if I specify their names in my language.

Anyone in the world had the same problem?

EDIT

Ok, I have a clue: if instead of doing 'search', I do 'read':

product_names = sock.execute(dbname, uid, pwd, 'product.product', 'read', ids, ['name'], {'lang': 'es_ES'})

This way I can specify the language and it works! But I can't do the same with 'search', I get errors. Anyone knows the way???

NEW EDIT

context = {'lang': 'es_ES'}

args = [('name', 'ilike', 'my_product')]  # consulta

ids = sock.execute(dbname, uid, pwd, 'product.product', 'search', args, context)

Upvotes: 3

Views: 2257

Answers (3)

nithal
nithal

Reputation: 1

In openerp/osv/expression.py the query has been reformulated to search the translation in first place an the original name in English only if where not exist a translation in ir_translation. OpenERP Server 6.0.4

query1 = '( SELECT res_id'          \
         '    FROM ir_translation'  \
         '   WHERE name = %s'       \
         '     AND lang = %s'       \
         '     AND type = %s'
instr = ' %s'
#Covering in,not in operators with operands (%s,%s) ,etc. 
if operator in ['in','not in']:
    instr = ','.join(['%s'] * len(right))
    query1 += '     AND value ' + operator +  ' ' +" (" + instr + ")" + ')'
else:
    query1 += '     AND value ' + operator + instr + ')'
query1 +=' UNION ('                \ 
         '  SELECT id'              \
         '    FROM "' + working_table._table + '"' + ' as wt '   \
         '   WHERE "'+ left + '" ' + operator + instr +  \
         '   AND NOT EXISTS (   ' \   
         '       SELECT res_id FROM ir_translation ' \
         '       WHERE wt.id = res_id ' \
         '       AND name = %s'       \
         '       AND lang = %s'       \
         '       AND type = %s)' +  ")"


query2 = [working_table._name + ',' + left,
          context.get('lang', False) or 'en_US',
          'model',
          right,
          right,
          working_table._name + ',' + left,
          context.get('lang', False) or 'en_US',
          'model'
           ]

Upvotes: 0

Martin Varela
Martin Varela

Reputation: 11

search method is like this:

search(cr, uid, args, offset=0, limit=None, order=None, context=None, count=False)

If you want to send the context you also have to send the parameters between args and context:

ids = sock.execute(dbname, uid, pwd, 'product.product', 'search', args, 0, 0, False, context)

Upvotes: 1

Lukasz Puchala
Lukasz Puchala

Reputation: 476

I'm almost sure it should work if you pass lang in context.

For example: context = {'lang': u'pl_PL'}.

If it doesn't work you should try to overwrite name_search method.

Upvotes: 5

Related Questions