Skip to content
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

How to use the built-in translations provided by WTForms #303

Closed
greyli opened this issue Aug 13, 2017 · 10 comments
Closed

How to use the built-in translations provided by WTForms #303

greyli opened this issue Aug 13, 2017 · 10 comments

Comments

@greyli
Copy link
Member

greyli commented Aug 13, 2017

As describe in WTForms's documentation here, we can define a base class to set the default locale app-widely:

from wtforms import Form

class MyBaseForm(Form):
    class Meta:
        locales = ['es_ES', 'es']

How to achieve this with Flask-WTF?

@greyli
Copy link
Member Author

greyli commented Aug 13, 2017

Actually, I don't know too much about i18n.

Install Flask-Babel and set BABEL_DEFAULT_LOCALE will do the same thing. However, I want to know is it possible to make it work without Flask-Babel (Babel)?

@lepture
Copy link
Contributor

lepture commented Aug 14, 2017

@greyli No. You must use Flask-Babel to enable i18n.

@greyli
Copy link
Member Author

greyli commented Aug 14, 2017

Thanks!

@greyli greyli closed this as completed Aug 14, 2017
@greyli
Copy link
Member Author

greyli commented Dec 21, 2017

@lepture Sorry to disturb you, I thought maybe there is a misunderstanding. Actually, I just want to set the default error message's language to Chinese. How to do it without Flask-Babel or Babel just like use pure WTForms?

Similar to this SO question.

@greyli greyli reopened this Dec 21, 2017
@greyli
Copy link
Member Author

greyli commented Dec 21, 2017

The working code with pure WTForms:

class MyBaseForm(Form):
    class Meta:
        locales = ['zh']

class HelloForm(MyBaseForm):
    name = StringField('Name', validators=[DataRequired()])
    submit = SubmitField()

Then the error message will be displayed in Chinese.

@lepture
Copy link
Contributor

lepture commented Dec 21, 2017

@greyli The current implementation is using Babel to determine the language automatically. In this case, you will have to use Babel.

@greyli
Copy link
Member Author

greyli commented Dec 21, 2017

Is it possible to add support for this usage?

I thought it's a common need. Sometimes people don't want to add i18n/l10n support, the only thing they want is using the specific built-in translation.

@greyli
Copy link
Member Author

greyli commented Dec 21, 2017

Just something like #300, but with a better implementation.

@greyli
Copy link
Member Author

greyli commented Dec 21, 2017

If you think it's ok, I'll submit a PR.

@lepture
Copy link
Contributor

lepture commented Dec 21, 2017

Sure, if it won’t take too much work.

@greyli greyli closed this as completed Jan 14, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants