-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Form translation #719
Comments
This works form.structure.find("[type=submit]")[0]["_value"] = "Grabar" |
Also need to Translate other Form stuff like "check to delete": { After submit having checked to delete, record is deleted but does not execute the code (browser stays static): |
The field's labels are translated as usual with the T() fixture.
|
This ticket searches an app wide solution to translate native Form stuff, In the examples app, just add deletable=True to this action in controllers.py and test
/translations/es.json
This way, app's will work as far as they find the proper translation language for the user of the app (es, it, fr, ...) |
I agree this is a workaround and a form translation procedure is needed.
In this case I looked into the form structure and then changed it in the controller. I we want to use T() then:
|
I ran into this issue as well, specifically also related to This is my solution for general Form translation - though I haven't tested whether it leads to issues with multiple requests: class TranslatedFormStyleFactory(FormStyleFactory):
def __init__(self, translator):
self.translator = translator
super().__init__()
def __call__(self, table, *args, **kwargs):
labels_before = {} # store what labels were before translation
# this "all_fields" code is copied from the original FormStyleFactor.__call__
all_fields = [x for x in table] # noqa: C416
if "_virtual_fields" in dir(table):
all_fields += table._virtual_fields
# translate the fields labels after storing their original value
for field in all_fields:
labels_before[field] = field.label
field.label = self.translator(field.label)
# call the actual form style code which creates the forms html
# this uses the translated field labels
result = super().__call__(table, *args, **kwargs)
# reset labels back to what they were before translation
for field, before in labels_before.items():
field.label = before
return result This requires passing the from py4web.utils.form import FormStyleBulma as OriginalFormStyleBulma
T = Translator(...)
FormStyleBulma = TranslatedFormStyleFactory(T)
FormStyleBulma.classes.update(OriginalFormStyleBulma.classes) I haven't extensively tested this, for example, I can imagine it may cause race condition issues when multiple clients request the same form with different translation. Potentially, the fact that I reset the label could reset them while another thread is using them. I just wanted to throw it out there as a possible solution. |
Improve Form so we can translate the value of the submit button
form = Form(...., submit_button="Grabar") <-- does not work
and neither works this in the translations file
"Submit": {
"0": "Grabar"
},
The text was updated successfully, but these errors were encountered: