Anbarryprojects / fastapi-babel

FastAPI babel support pybable tools like translation of text, formatting of dates, times and numbers, and time zones.
MIT License
46 stars 14 forks source link

Support for lazy gettext functions variants ? #18

Closed gikpro closed 1 year ago

gikpro commented 1 year ago

Can we use this for WTForms fields ? Example

class RegistrationForm(Form):
    name = StringField(_('Name'))

https://github.com/klen/asgi-babel/issues/1

Legopapurida commented 1 year ago

Add this block of code to handle lazy gettext.

from wtforms import Form, StringField, validators as v
from wtforms import SubmitField
class lazy_gettext:
    def __init__(self, message) -> None:
        self.message = message

    def __repr__(self) -> str:
        return _(self.message)

class RegistrationForm(Form):
    username = StringField(
        lazy_gettext(_("Name")),
        [v.InputRequired(lazy_gettext(_("Please provide your name")))],
    )
    submit = SubmitField(lazy_gettext(_("Submit")))

But I will add this feature to the new release. And if you would like you can contribute to our project and send a pull request.