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

fix translations_path #2521

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog
Fixes:

* The `Apply` button for filters will show/hide correctly again
* Fix `translations_path` attribute when Flask-Admin is used with Flask-Babel

2.0.0a0
-------
Expand Down
7 changes: 4 additions & 3 deletions flask_admin/babel.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ class CustomDomain(Domain):
def __init__(self):
super(CustomDomain, self).__init__(translations.__path__[0], domain='admin')

def get_translations_path(self, ctx):
@property
def translation_directories(self):
view = get_current_view()

if view is not None:
dirname = view.admin.translations_path
if dirname is not None:
return dirname
return [dirname] + super().translation_directories

return super(CustomDomain, self).get_translations_path(ctx)
return super().translation_directories

domain = CustomDomain()

Expand Down
33 changes: 33 additions & 0 deletions flask_admin/tests/test_babel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from unittest import mock

import pytest

from flask_admin import babel, translations

from . import flask_babel_test_decorator


@flask_babel_test_decorator
@pytest.mark.parametrize("dirname", [None, "foo/bar"])
@mock.patch("flask_admin.babel.get_current_view")
@mock.patch("flask_babel.Domain.get_translations_cache")
@mock.patch("flask_babel._get_current_context")
@mock.patch("flask_babel.get_locale", return_value="qux")
@mock.patch("babel.support.Translations")
def test_translations_path(
_Translations,
_get_locale,
_get_current_context,
_get_translations_cache,
_get_current_view,
dirname,
):
_get_current_view.return_value.admin.translations_path = dirname
_get_translations_cache.return_value = {}
_dirname = dirname or translations.__path__[0]
domain = babel.CustomDomain()
assert domain.get_translations() == _Translations.return_value
calls = [mock.call.load(translations.__path__[0], ["qux"], "admin")]
if dirname:
calls = [mock.call.load(dirname, ["qux"], "admin")] + calls
assert _Translations.method_calls == calls