Thank you Leonel.
2016-10-04 11:19 GMT+01:00 Leonel Câmara <[email protected]>:
> I have solved this problem in 2 different ways. Depending on your need,
> you pick one.
>
> Option 1 "have an extra translation table" can look something like this
>
> LANG = T.accepted_language if T.accepted_language in SUPPORTED_LANGUAGES
> else 'en'
>
>
> db.define_table('tag',
> format=lambda r: db(
> (db.tag_translation.tag == r.id) &
> (db.tag_translation.lang == LANG)
> ).select().first().name
> )
>
>
> db.define_table('tag_translation',
> Field('tag', 'reference tag'),
> Field('lang', requires=IS_IN_SET(SUPPORTED_LANGUAGES)),
> Field('name', label=T('Name')),
> )
>
>
>
>
> Option 2 which I kind of favor right now if I'm using postgresql, is to
> "use a JSON field" and store all the translations there:
>
>
> def pick_lang():
> twoletter = session.lang or T.accepted_language[:2]
> if twoletter in SUPPORTED_LANGUAGES:
> T.force(twoletter)
> return twoletter
> else:
> T.force('en')
> return 'en'
> LANG = pick_lang()
>
> def represent_lang(v):
> if v is None:
> return v
> if LANG in v:
> return v[LANG]
> else:
> return v['en']
>
>
> db.define_table('tag',
> Field('name', 'json', widget=TranslateWidget().widget,
> requires=IS_JSON(), represent=represent_lang)
> )
>
> You may notice this second option has a TranslateWidget, I have included a
> file with it in this post, just put it in modules and import it in your
> models.
>
> If you go with this option another useful function if you wanted to e.g.
> search tags by name, is:
>
> from gluon.dal import Expression
>
> def JSON_KEY_EXPRESSION(field, key, value_type='string'):
> db = field._db
> def op(first, second):
> return "%s->>'%s'" % (db._adapter.expand(first), db._adapter.
> expand(second))
> return Expression(db, op, field, key, value_type)
>
> And then in a controller you could do something like
>
> tags = db(JSON_KEY_EXPRESSION(db.tag.name, LANG).like('%' +
> request.vars.search
> + '%', case_sensitive=False)).select()
>
>
>
> --
> 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.
>
--
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.