Skip to content

Commit

Permalink
Merge pull request #1509 from UlrichB22/flask-babel-3
Browse files Browse the repository at this point in the history
Upgrade Flask-Babel to 3.x
  • Loading branch information
RogerHaase authored Sep 20, 2023
2 parents 811c359 + 9561f24 commit 299ad39
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
9 changes: 5 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# Copyright: 2001 by Juergen Hermann <[email protected]>
# Copyright: 2001-2018 MoinMoin:ThomasWaldmann
# Copyright: 2023 MoinMoin project
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

import os
Expand Down Expand Up @@ -72,17 +73,17 @@
],
install_requires=[
'Babel>=2.10.0', # internationalization support
'blinker>=1.1', # event signalling (e.g. for change notification trigger)
'blinker>=1.5', # event signalling (e.g. for change notification trigger)
'docutils>=0.18.1', # reST markup processing
'Markdown>=3.4.1', # Markdown markup processing
'Flask<2.3.0', # micro framework
'Flask-Babel<3.0.0', # i18n support
'Flask-Babel>=3.0.0', # i18n support
'Flask-Caching>=1.2.0', # caching support
'Flask-Theme>=0.3.5', # theme support
'Flask-Theme>=0.3.6', # theme support
'emeraldtree>=0.10.0', # xml processing
'feedgen==0.9.*', # Atom feed
'flatland>=0.8', # form handling
'Jinja2<3.1.0', # template engine
'Jinja2>=3.1.0', # template engine
'markupsafe<=2.2.0', # safe html and xml
'pygments>=1.4', # src code / text file highlighting
'Werkzeug<2.4.0', # wsgi toolkit
Expand Down
3 changes: 2 additions & 1 deletion src/moin/apps/frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Copyright: 2010 MoinMoin:DiogenesAugusto
# Copyright: 2001 Richard Jones <[email protected]>
# Copyright: 2001 Juergen Hermann <[email protected]>
# Copyright: 2023 MoinMoin project
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Expand Down Expand Up @@ -2296,7 +2297,7 @@ class UserSettingsPersonalForm(Form):
# _timezones_keys = sorted(Locale('en').time_zones.keys())
_timezones_keys = [str(tz) for tz in pytz.common_timezones]
timezone = Select.using(label=L_('Timezone')).out_of((e, e) for e in _timezones_keys)
_supported_locales = [Locale('en')] + app.babel_instance.list_translations()
_supported_locales = [Locale('en')] + app.extensions['babel'].instance.list_translations()
locale = Select.using(label=L_('Locale')).out_of(
((str(locale), locale.display_name) for locale in _supported_locales), sort_by=1)
submit_label = L_('Save')
Expand Down
13 changes: 6 additions & 7 deletions src/moin/i18n/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright: 2011 MoinMoin:ThomasWaldmann
# Copyright: 2023 MoinMoin project
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Expand Down Expand Up @@ -33,9 +34,7 @@

def i18n_init(app):
""" initialize Flask-Babel """
babel = Babel(app)
babel.localeselector(get_locale)
babel.timezoneselector(get_timezone)
Babel(app, locale_selector=get_locale, timezone_selector=get_timezone)


def get_locale():
Expand All @@ -56,7 +55,7 @@ def get_locale():
except RuntimeError: # CLI call has no valid request context
cli_no_request_ctx = True

supported_locales = [Locale('en')] + current_app.babel_instance.list_translations()
supported_locales = [Locale('en')] + current_app.extensions['babel'].instance.list_translations()
logging.debug("supported_locales = {0!r}".format(supported_locales))
supported_languages = [str(locale) for locale in supported_locales]
logging.debug("supported_languages = {0!r}".format(supported_languages))
Expand Down Expand Up @@ -96,16 +95,16 @@ def force_locale(locale):
yield
return
babel = ctx.app.extensions['babel']
orig_locale_selector_func = babel.locale_selector_func
orig_locale_selector = babel.locale_selector
orig_attrs = {}
for key in ('babel_translations', 'babel_locale'):
orig_attrs[key] = getattr(ctx, key, None)
try:
babel.locale_selector_func = lambda: locale
babel.locale_selector = lambda: locale
for key in orig_attrs:
setattr(ctx, key, None)
yield
finally:
babel.locale_selector_func = orig_locale_selector_func
babel.locale_selector = orig_locale_selector
for key, value in orig_attrs.items():
setattr(ctx, key, value)
5 changes: 3 additions & 2 deletions src/moin/i18n/_tests/test_i18n.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright: 2011 Prashant Kumar <contactprashantat AT gmail DOT com>
# Copyright: 2023 MoinMoin project
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Expand Down Expand Up @@ -41,12 +42,12 @@ def test_text():
def test_force_locale():
pytest.skip("This test needs to be run with --assert=reinterp or --assert=plain flag")
app = Flask(__name__)
b = babel.Babel(app)

@b.localeselector
def select_locale():
return 'de_DE'

babel.Babel(app, locale_selector=select_locale)

with app.test_request_context():
assert str(babel.get_locale()) == 'de_DE'
with force_locale('en_US'):
Expand Down

0 comments on commit 299ad39

Please sign in to comment.