Internationalization for forms and future grid

196 views
Skip to first unread message

Kevin Keller

unread,
Sep 3, 2020, 7:59:18 PM9/3/20
to py4web
Will we be able to add translation for the labels and submit button?
Possibly even be able to add custom buttons and translations? 

Just getting into forms here and its really cool, but 
there seems to be a lot of the html hard coded for now so just thought could be a useful discussion to have. 


Massimo

unread,
Sep 3, 2020, 10:33:05 PM9/3/20
to py4web
You can already do Field(.... label=T("label"))
They are not translated by default. We can think how to make it better.
Maybe form and grid should translate them by default as well as T(...) all the build-in stings.

CarlosDB

unread,
Nov 7, 2022, 6:41:49 PM11/7/22
to py4web

I have been looking at how forms can be internationalized, submit button and the different messages and texts of the widgets.

After a few tests, it starts to work, but I don’t know if it is a correct and valid approach.

I have added a new parameter (T=lambda text: text) in init of all widgets
Something like this:

class Widget:

    """Prototype widget object for all form widgets"""

    type_map = {
        "string": "text",
        "date": "date",
        "time": "time",
    }

    def __init__(self, T=lambda text: text):
        self.T = T

    def make(self, field, value, error, title, placeholder="", readonly=False):
        """converts the widget to an HTML helper"""
        return INPUT(
            _value=value,
            _type=self.type_map.get(field.type, "text"),
            _id=to_id(field),
            _name=field.name,
            _placeholder=self.T(placeholder),
            _title=self.T(title),
            _readonly=readonly,
        )
Also to FormStyleFactory and Form
class FormStyleFactory:
    ...
    ...
    def __init__(self, T=lambda text: text):
       ...
      ...
class Form(object):
    ...
    ...
    define __init__(
        self,
        table,
        record=None,
        readonly=False,
        deleteable=True,
        noncreate=True,
        formstyle=FormStyleDefault,
        dbio=True,
        keep_values=False,
        form_name=None,
        hidden=None,
        validation=None,
        csrf_session=None,
        csrf_protection=True,
        lifespan=None,
        signing_info=None,
        submit_value="Submit",
        show_id=True,
        T=lambda text: text,
        **kwargs
    )
...
...

In this way all texts can be translated.
It remains to complete the code, do many tests and also see how to translate the login forms...

What is your opinion?

Carlos.

Jorge Salvat

unread,
Nov 7, 2022, 10:50:30 PM11/7/22
to py4web
This is great, thanks Carlos.
Long time waiting for translations to work as a py4web native feature every where (Forma, Auth, _scafold, ...
Necesary to go into production app's for non english cultures.
Will do some testing ASAP.

Jorge 

CarlosDB

unread,
Nov 14, 2022, 11:45:29 AM11/14/22
to py4web
Hi Massimo,

What do you think about modifying all the widgets of the form in a similar way to the one I propose in this thread and then making other adjustments in FormStyleFactory and Form for internationalization with T

Carlos.

CarlosDB

unread,
Nov 18, 2022, 7:00:25 AM11/18/22
to py4web
Hello,

I have advanced a bit making some modifications in the form for the internationalization of the messages.

Translates inside the form:
field.label
field.comment
field._placeholder
error messages
The submit button
FileUploadWidget texts

Should label, comment and placeholder be translated inside the form?
Or better in the definition of the field?


What is your opinion?

Carlos.

CarlosDB

unread,
Nov 25, 2022, 5:32:14 AM11/25/22
to py4web
Any comments or opinions on this topic?

Carlos.

Massimo

unread,
Nov 27, 2022, 6:49:54 PM11/27/22
to py4web
Sorry for my late reply.

We definitively have inconsistent behavior. Form, intentionally, did not depend on T and I do not think we want to add that dependency. The intention was to be explicit and do things like db.table.field.label = T("field name").

Grid instead knows about T and calls self.T in some places. I would have preferred to be consistent and not pass T to grid but pass strings to configure it. Those strings could optionally be wrapped by T.

One possible approach is to have all strings in one place in grid and, if T is passed to the constructor, loop over all the strings and wrap them into T, that way we do not need to add self.T(..) all over. Notice that str(T(T(value))) == str(T(value)) so this is safe.

Massimo

CarlosDB

unread,
Nov 28, 2022, 12:29:50 PM11/28/22
to py4web
The problem that I have found with the solution that you propose is the translation of the default error messages of the widgets in general and in particular all the texts of FileUploadWidget

For this reason, the only solution I have found is that Form, FileUploadWidget and FormStyleFactory have a reference of T. I really believe that there are few modifications.

field.label, field.comment, field._title field.
_placeholder can be translated from outside the Form, but I don't see any way to translate FileUploadWidget texts and Widgets' default error messages without the FormStyleFactory, Form, and FileUploadWidget having a reference to T

I'm sorry to insist a little. I'll keep thinking about it a bit.

Carlos.

Massimo DiPierro

unread,
Nov 29, 2022, 1:12:35 AM11/29/22
to CarlosDB, py4web
thanks for the explanation. let me think about this a little. priority should be to have a uniform method. 

--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/py4web/12f93427-3318-4615-8433-a16a2844d11en%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages