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

Support multiple translations with different message domains #148

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ change some internal defaults:
``translations``.
`BABEL_DOMAIN` The message domain used by the application.
Defaults to ``messages``.
`BABEL_TRANSLATIONS` A list of available translations with specified
message domains. The list is a list of
two-tuples in the format ``(translation folder,
message domain)`` - for example,
``('translations', 'messages')``.
=============================== =============================================

For more complex applications you might want to have multiple applications
Expand Down
29 changes: 19 additions & 10 deletions flask_babel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def init_app(self, app):
app.config.setdefault('BABEL_DEFAULT_LOCALE', self._default_locale)
app.config.setdefault('BABEL_DEFAULT_TIMEZONE', self._default_timezone)
app.config.setdefault('BABEL_DOMAIN', self._default_domain)

if self._date_formats is None:
self._date_formats = self.default_date_formats.copy()

Expand Down Expand Up @@ -147,7 +148,7 @@ def list_translations(self):
"""
result = []

for dirname in self.translation_directories:
for dirname, _ in self.translations:
if not os.path.isdir(dirname):
continue

Expand Down Expand Up @@ -187,16 +188,24 @@ def domain(self):

@property
def translation_directories(self):
for dirname, _ in self.translations:
yield dirname

@property
def translations(self):
directories = self.app.config.get(
'BABEL_TRANSLATION_DIRECTORIES',
'translations'
).split(';')

for path in directories:
if os.path.isabs(path):
yield path
_translations = [(dirname, self.domain) for dirname in directories]
_translations += self.app.config.get('BABEL_TRANSLATIONS') or []

for dirname, domain in _translations:
if os.path.isabs(dirname):
yield (dirname, domain)
else:
yield os.path.join(self.app.root_path, path)
yield (os.path.join(self.app.root_path, dirname), domain)


def get_translations():
Expand All @@ -215,12 +224,12 @@ def get_translations():
translations = support.Translations()

babel = current_app.extensions['babel']
for dirname in babel.translation_directories:
for dirname, domain in babel.translations:
catalog = support.Translations.load(
dirname,
[get_locale()],
babel.domain
)
dirname,
[get_locale()],
domain
)
translations.merge(catalog)
# FIXME: Workaround for merge() being really, really stupid. It
# does not copy _info, plural(), or any other instance variables
Expand Down
34 changes: 34 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,40 @@ def test_different_domain(self):

assert gettext(u'Good bye') == 'Auf Wiedersehen'

def test_multiple_translations(self):
"""
Ensure we can load multiple translations with different text domains.
"""
b = babel.Babel()
app = flask.Flask(__name__)

app.config.update({
'BABEL_TRANSLATION_DIRECTORIES': ';'.join((
'translations',
'renamed_translations'
)),
'BABEL_TRANSLATIONS': [
('translations_different_domain', 'myapp')
],
'BABEL_DEFAULT_LOCALE': 'de_DE'
})

b.init_app(app)

with app.test_request_context():
translations = b.list_translations()

assert(len(translations) == 3)
assert(str(translations[0]) == 'de')
assert(str(translations[1]) == 'de')
assert(str(translations[2]) == 'de')

assert gettext(
u'Hello %(name)s!',
name='Peter'
) == 'Hallo Peter!'
assert gettext(u'Good bye') == 'Auf Wiedersehen'

def test_lazy_old_style_formatting(self):
lazy_string = lazy_gettext(u'Hello %(name)s')
assert lazy_string % {u'name': u'test'} == u'Hello test'
Expand Down