requires=IS_IN_SET translate?

82 views
Skip to first unread message

Ruud Schroen

unread,
Dec 12, 2013, 7:23:51 AM12/12/13
to web...@googlegroups.com
Hi,

Is it possible to translate the strings inside requires=IS_IN_SET?

For example i have

requires=IS_IN_SET(('Allowed','Not allowed'))

Now i'd like to translate "Allowed" and "Not allowed"

Leonel Câmara

unread,
Dec 12, 2013, 7:46:36 AM12/12/13
to web...@googlegroups.com
yes, 

requires=IS_IN_SET([('Allowed', T('Allowed')), ('Not allowed', T('Not allowed'))])

黄祥

unread,
Dec 12, 2013, 8:01:01 AM12/12/13
to web...@googlegroups.com
requires=IS_IN_SET([('Allowed', T('Allowed')), ('Not allowed', T('Not allowed'))])

another way around to achieve it is
requires = IS_IN_SET({T('Allowed'), T('Not allowed') } )

p.s.
please ensure if you use translate version in the conditional logic (if), please also put the translation on it. e.g.

if db.employee.access == T('Allowed'):

if you don't put the translation (T()) in the conditonal, the result always return false, because the data in table is in translate version, yet you compare it with non translate version.

best regards,
stifan

Leonel Câmara

unread,
Dec 12, 2013, 8:29:40 AM12/12/13
to web...@googlegroups.com
Stifan, I'm sorry but that seems like a terrible way to do it as you would have different values in the database depending on the language of the user, and, even worse, you could have different values for the same thing for the same user if you start without a translation in his language and then add one. So, comparisons like the one in your example can actually stop working; you could add a field to the table so you could remember what language each row was submitted with, but that seems a bit convoluted to my primate brain.


Quinta-feira, 12 de Dezembro de 2013 12:23:51 UTC, Ruud Schroen escreveu:

黄祥

unread,
Dec 12, 2013, 8:38:50 AM12/12/13
to web...@googlegroups.com
yes, you are right, i'm understand right now, thanks for correct my mistake.

please also take a look at this discussion

ref:

thanks and best regards,
stifan

Ruud Schroen

unread,
Dec 12, 2013, 9:12:50 AM12/12/13
to web...@googlegroups.com
Would this work?

On Saturday, November 16, 2013 1:21:07 PM UTC+1, Paolo Caruccio wrote:
The IS_IN_SET validator allows to list the options as a dictionary (http://web2py.com/books/default/chapter/29/07/forms-and-validators?search=IS_IN_SET)

Therefore, for an example, you could do:

def index(): 
   options 
= {'Apple':T('Apple'),'Banana':T('Banana'),'Cherry':T('Cherry')}
   form
=SQLFORM.factory(Field('fruit',requires=IS_IN_SET(options,zero=None)))
   
if form.accepts(request, session):
       selected 
= options[form.vars.fruit]
       saved 
= form.vars.fruit
       response
.flash='selected %s in drop down list, saved %s in db' %(selected, saved)
   
return dict(form=form)



--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/Q51pSDExMcE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Massimo Di Pierro

unread,
Dec 12, 2013, 9:25:18 AM12/12/13
to web...@googlegroups.com
This would work. Yet it will not preserve the order in the list of options. To preserve the order replace

 options = {'Apple':T('Apple'),'Banana':T('Banana'),'Cherry':T('Cherry')}

with

 options = (('Apple',T('Apple')), ('Banana',T('Banana')), ('Cherry',T('Cherry')))

Ruud Schroen

unread,
Dec 12, 2013, 10:07:25 AM12/12/13
to web...@googlegroups.com
Works like a charm, thanks Massimo!
Reply all
Reply to author
Forward
0 new messages