Skip to content

Commit

Permalink
Merge branch 'amercader-fix-single-plural-multiple-domains'
Browse files Browse the repository at this point in the history
  • Loading branch information
TkTech committed Jun 19, 2024
2 parents 645b489 + e63da29 commit 869715c
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 220 deletions.
21 changes: 9 additions & 12 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,38 @@

# -- Project information -----------------------------------------------------

project = 'Flask-Babel'
copyright = '2020, Armin Ronacher'
author = 'Armin Ronacher'
project = "Flask-Babel"
copyright = "2020, Armin Ronacher"
author = "Armin Ronacher"

# The full version, including alpha/beta/rc tags
release = '4.0.0'
release = "4.0.0"


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.viewcode'
]
extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode"]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'furo'
html_theme = "furo"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_static_path = ["_static"]
2 changes: 1 addition & 1 deletion flask_babel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ def get_translations(self):
# does not copy _info, plural(), or any other instance variables
# populated by GNUTranslations. We probably want to stop using
# `support.Translations.merge` entirely.
if hasattr(catalog, "plural"):
if catalog.info() and hasattr(catalog, "plural"):
translations.plural = catalog.plural

cache[str(locale), self.domain[0]] = translations
Expand Down
13 changes: 4 additions & 9 deletions tests/test_app_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ def test_app_factory():
b = babel.Babel()

def locale_selector():
return 'de_DE'
return "de_DE"

def create_app():
app_ = flask.Flask(__name__)
b.init_app(
app_,
default_locale='en_US',
locale_selector=locale_selector
)
b.init_app(app_, default_locale="en_US", locale_selector=locale_selector)
return app_

app = create_app()
with app.test_request_context():
assert str(babel.get_locale()) == 'de_DE'
assert babel.gettext(u'Hello %(name)s!', name='Peter') == \
'Hallo Peter!'
assert str(babel.get_locale()) == "de_DE"
assert babel.gettext("Hello %(name)s!", name="Peter") == "Hallo Peter!"
50 changes: 24 additions & 26 deletions tests/test_date_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,44 @@ def test_basics():
delta = timedelta(days=6)

with app.test_request_context():
assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00\u202fPM'
assert babel.format_date(d) == 'Apr 12, 2010'
assert babel.format_time(d) == '1:46:00\u202fPM'
assert babel.format_timedelta(delta) == '1 week'
assert babel.format_timedelta(delta, threshold=1) == '6 days'
assert babel.format_datetime(d) == "Apr 12, 2010, 1:46:00\u202fPM"
assert babel.format_date(d) == "Apr 12, 2010"
assert babel.format_time(d) == "1:46:00\u202fPM"
assert babel.format_timedelta(delta) == "1 week"
assert babel.format_timedelta(delta, threshold=1) == "6 days"

with app.test_request_context():
get_babel(app).default_timezone = 'Europe/Vienna'
assert babel.format_datetime(d) == 'Apr 12, 2010, 3:46:00\u202fPM'
assert babel.format_date(d) == 'Apr 12, 2010'
assert babel.format_time(d) == '3:46:00\u202fPM'
get_babel(app).default_timezone = "Europe/Vienna"
assert babel.format_datetime(d) == "Apr 12, 2010, 3:46:00\u202fPM"
assert babel.format_date(d) == "Apr 12, 2010"
assert babel.format_time(d) == "3:46:00\u202fPM"

with app.test_request_context():
get_babel(app).default_locale = 'de_DE'
assert babel.format_datetime(d, 'long') == \
'12. April 2010, 15:46:00 MESZ'
get_babel(app).default_locale = "de_DE"
assert babel.format_datetime(d, "long") == "12. April 2010, 15:46:00 MESZ"


def test_custom_formats():
app = flask.Flask(__name__)
app.config.update(
BABEL_DEFAULT_LOCALE='en_US',
BABEL_DEFAULT_TIMEZONE='Pacific/Johnston'
BABEL_DEFAULT_LOCALE="en_US", BABEL_DEFAULT_TIMEZONE="Pacific/Johnston"
)
b = babel.Babel(app)
b.date_formats['datetime'] = 'long'
b.date_formats['datetime.long'] = 'MMMM d, yyyy h:mm:ss a'
b.date_formats["datetime"] = "long"
b.date_formats["datetime.long"] = "MMMM d, yyyy h:mm:ss a"
d = datetime(2010, 4, 12, 13, 46)

with app.test_request_context():
assert babel.format_datetime(d) == 'April 12, 2010 3:46:00 AM'
assert babel.format_datetime(d) == "April 12, 2010 3:46:00 AM"


def test_custom_locale_selector():
app = flask.Flask(__name__)
b = babel.Babel(app)
d = datetime(2010, 4, 12, 13, 46)

the_timezone = 'UTC'
the_locale = 'en_US'
the_timezone = "UTC"
the_locale = "en_US"

def select_locale():
return the_locale
Expand All @@ -64,21 +62,21 @@ def select_timezone():
get_babel(app).timezone_selector = select_timezone

with app.test_request_context():
assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00\u202fPM'
assert babel.format_datetime(d) == "Apr 12, 2010, 1:46:00\u202fPM"

the_locale = 'de_DE'
the_timezone = 'Europe/Vienna'
the_locale = "de_DE"
the_timezone = "Europe/Vienna"

with app.test_request_context():
assert babel.format_datetime(d) == '12.04.2010, 15:46:00'
assert babel.format_datetime(d) == "12.04.2010, 15:46:00"


def test_refreshing():
app = flask.Flask(__name__)
babel.Babel(app)
d = datetime(2010, 4, 12, 13, 46)
with app.test_request_context():
assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00\u202fPM'
get_babel(app).default_timezone = 'Europe/Vienna'
assert babel.format_datetime(d) == "Apr 12, 2010, 1:46:00\u202fPM"
get_babel(app).default_timezone = "Europe/Vienna"
babel.refresh()
assert babel.format_datetime(d) == 'Apr 12, 2010, 3:46:00\u202fPM'
assert babel.format_datetime(d) == "Apr 12, 2010, 3:46:00\u202fPM"
34 changes: 17 additions & 17 deletions tests/test_force_locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,67 @@

def test_force_locale():
app = flask.Flask(__name__)
babel.Babel(app, locale_selector=lambda: 'de_DE')
babel.Babel(app, locale_selector=lambda: "de_DE")

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


def test_force_locale_with_threading():
app = flask.Flask(__name__)
babel.Babel(app, locale_selector=lambda: 'de_DE')
babel.Babel(app, locale_selector=lambda: "de_DE")

semaphore = Semaphore(value=0)

def first_request():
with app.test_request_context():
with babel.force_locale('en_US'):
assert str(babel.get_locale()) == 'en_US'
with babel.force_locale("en_US"):
assert str(babel.get_locale()) == "en_US"
semaphore.acquire()

thread = Thread(target=first_request)
thread.start()

try:
with app.test_request_context():
assert str(babel.get_locale()) == 'de_DE'
assert str(babel.get_locale()) == "de_DE"
finally:
semaphore.release()
thread.join()


def test_force_locale_with_threading_and_app_context():
app = flask.Flask(__name__)
babel.Babel(app, locale_selector=lambda: 'de_DE')
babel.Babel(app, locale_selector=lambda: "de_DE")

semaphore = Semaphore(value=0)

def first_app_context():
with app.app_context():
with babel.force_locale('en_US'):
assert str(babel.get_locale()) == 'en_US'
with babel.force_locale("en_US"):
assert str(babel.get_locale()) == "en_US"
semaphore.acquire()

thread = Thread(target=first_app_context)
thread.start()

try:
with app.app_context():
assert str(babel.get_locale()) == 'de_DE'
assert str(babel.get_locale()) == "de_DE"
finally:
semaphore.release()
thread.join()


def test_refresh_during_force_locale():
app = flask.Flask(__name__)
babel.Babel(app, locale_selector=lambda: 'de_DE')
babel.Babel(app, locale_selector=lambda: "de_DE")

with app.test_request_context():
with babel.force_locale('en_US'):
assert str(babel.get_locale()) == 'en_US'
with babel.force_locale("en_US"):
assert str(babel.get_locale()) == "en_US"
babel.refresh()
assert str(babel.get_locale()) == 'en_US'
assert str(babel.get_locale()) == "en_US"
Loading

0 comments on commit 869715c

Please sign in to comment.