On Tuesday, April 5, 2016 at 10:04:47 PM UTC-7, SanDiego wrote:
>
> I wanted to use translatable fields using T() function. I couldn't find
> any information for this in the document. I could only find the info from
> forum discussions. But still there are some problems:
>
>
> COURSES = {'math': T('Math'), 'phy': T('Physics')}
> ...
> Field(course, requires=IS_IN_SET(COURSES))
>
>
>
shouldn't you be setting the type to string? That's the default type, but
it may have benefits to be explicit.
works. Only keys (e.g. 'phy') are stored in DB and COURSES dictionary can
> be used to display the value (e.g. COURSES['phy']). However, order is not
> retained.
>
>
>
> Massimo recommends using sets to retain the order here
> <https://groups.google.com/d/msg/web2py/Q51pSDExMcE/m-q85-PVggAJ>:
>
I think he's recommending a tuple of tuples. (I'm surprised it isn't a
list of tuples, as in the book)
<URL;http://web2py.com/books/default/chapter/29/07/forms-and-validators#Range--set-and-equality-validators>
then scroll down to IS_IN_SET().
>
> COURSES = (('math', T('Math')), ('phy', T('Physics')))
> ...
> Field(course, requires=IS_IN_SET(COURSES))
>
>
> This time order is retained but then display will be problematic. Probably
> we can use list comprehension to display the translated value but not very
> intuitive.
>
>
>
Are you sure the drop down menu is created for you?
> My questions are:
>
> - Is there an easy way for retaining the order and displaying
> correctly?
>
>
see above
>
> -
> - How can we set the default value? e.g. if we set default to 'phy', I
> still want to show COURSES['phy'] in the dropdown menu in the form
>
>
would that be Field("Courses", "string", default=COURSES[1],
requires=IS_IN_SET(COURSES)) ?
>
> - Would it be possible to add more info and examples for this function
> in the documentation? I think this is very basic function and used
> extensively.
>
> The material at the URL seems clear enough, although it doesn't show using
the T() function. What do you suggest is missing?
/dps
--
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 the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.