How can I add entries to languages\pt.py file when a user adds a record?

26 views
Skip to first unread message

João Matos

unread,
Mar 19, 2019, 5:13:01 AM3/19/19
to web2py-users
Hello,

I would like to add entries to languages\pt.py file when a user adds a record.
How can I do that?

Thanks,

JM

Leonel Câmara

unread,
Mar 19, 2019, 5:56:33 AM3/19/19
to web2py-users
Ideally you don't. Frankly web2py's translation system is not ideal for dynamic content. That said, you can simply put a function in the record's table _after_insert that is something like this:

def make_portuguese_entry(fields, recordid):
    T.force('pt')
    T(fields[name_of_the_field], lazy=False)

See:

João Matos

unread,
Mar 19, 2019, 6:17:53 AM3/19/19
to web...@googlegroups.com
Thanks.

Doesn't that only add the left entry part?
I would like to add the left in English and the right in Portuguese.

Leonel Câmara

unread,
Mar 19, 2019, 7:27:06 AM3/19/19
to web2py-users
How would web2py know how to translate it when the record is inserted to fill that part? Is it one of the fields? 

João Matos

unread,
Mar 19, 2019, 8:12:08 AM3/19/19
to web2py-users
Yes, the user will enter both languages (EN and PT).
I would like to add the pair to languages\pt.py.

Leonel Câmara

unread,
Mar 19, 2019, 8:41:50 AM3/19/19
to web2py-users
Something like this:

from gluon.languages import write_dict

def make_portuguese_entry(fields, recordid):
    T.force('pt')
    T.t[fields[name_of_the_field]] = fields[name_of_the_field_pt]
    if T.is_writable:
        write_dict(T.language_file, T.t)

your_table._after_insert.append(make_portuguese_entry)

João Matos

unread,
Mar 19, 2019, 8:45:22 AM3/19/19
to web2py-users
That's it. Thanks.
Reply all
Reply to author
Forward
0 new messages