Repository navigation
[rejected AI] Fix i18n silently disabled when Flask-Babel isn't installed - #703
Closed
cristianchiriac wants to merge 1 commit into
Closed
cristianchiriac wants to merge 1 commit into
cristianchiriac wants to merge 1 commit into
Conversation
Fixes pallets-eco#582. FlaskForm.Meta.get_translations() unconditionally returned "translations" unless WTF_I18N_ENABLED was explicitly set to False. "translations" is set to None when Flask-Babel isn't installed (caught ImportError in flask_wtf/form.py), so any form using meta={"locales": [...]} without Flask-Babel silently got no translations at all, even though plain wtforms.Form with the exact same meta.locales configuration works out of the box. Fall back to WTForms own translations (super().get_translations) whenever "translations" is None, the same way the WTF_I18N_ENABLED=False path already does, instead of returning None.
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #582.
Bug
Plain
wtforms.Formworks out of the box withmeta={"locales": [...]}, no Flask-Babel needed:But the same configuration on a
FlaskFormsilently produces English error messages unless Flask-Babel is installed and initialized:FlaskForm.Meta.get_translations()is the reason:translations(imported fromflask_wtf.i18n) isNonewhenever Flask-Babel isn't installed — caught as anImportErrorinflask_wtf/form.py. WithWTF_I18N_ENABLEDat its default (True), this method returns thatNoneunconditionally, discarding the form's ownmeta.locales-based translations entirely, instead of falling back to WTForms' own translation machinery the way theWTF_I18N_ENABLED=Falsebranch already does.Fix
Also fall back to
super().get_translations(form)whentranslations is None.Test plan
test_i18n_falls_back_without_babel, monkeypatchingflask_wtf.form.translationstoNone(deterministic regardless of whether Flask-Babel happens to be installed in the CI matrix) and asserting ameta.locales-configured form still gets localized errors.pytest tests/— 84 passed (up from 80), 2 skipped (unchanged) with Flask-Babel installed; ran the full suite both with and without Flask-Babel present.ruff check/ruff format --check— clean.CONTRIBUTING.rst.