Skip to content

Commit 03e94fe

Browse files
authored
Merge pull request #91 from amercader/master
Support for different text domains
2 parents 726af40 + 9bbe285 commit 03e94fe

File tree

6 files changed

+129
-13
lines changed

6 files changed

+129
-13
lines changed

docs/index.rst

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,23 @@ object after configuring the application::
3838
babel = Babel(app)
3939

4040
The babel object itself can be used to configure the babel support
41-
further. Babel has two configuration values that can be used to change
42-
some internal defaults:
43-
44-
=========================== =============================================
45-
`BABEL_DEFAULT_LOCALE` The default locale to use if no locale
46-
selector is registered. This defaults
47-
to ``'en'``.
48-
`BABEL_DEFAULT_TIMEZONE` The timezone to use for user facing dates.
49-
This defaults to ``'UTC'`` which also is the
50-
timezone your application must use internally.
51-
=========================== =============================================
41+
further. Babel has the following configuration values that can be used to
42+
change some internal defaults:
43+
44+
=============================== =============================================
45+
`BABEL_DEFAULT_LOCALE` The default locale to use if no locale
46+
selector is registered. This defaults
47+
to ``'en'``.
48+
`BABEL_DEFAULT_TIMEZONE` The timezone to use for user facing dates.
49+
This defaults to ``'UTC'`` which also is the
50+
timezone your application must use internally.
51+
`BABEL_TRANSLATION_DIRECTORIES` A semi-colon (``;``) separated string of
52+
absolute and relative (to the app root) paths
53+
to translation folders. Defaults to
54+
``translations``.
55+
`BABEL_DOMAIN` The message domain used by the application.
56+
Defaults to ``messages``.
57+
=============================== =============================================
5258

5359
For more complex applications you might want to have multiple applications
5460
for different users which is where selector functions come in handy. The

flask_babel/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ class Babel(object):
5555
})
5656

5757
def __init__(self, app=None, default_locale='en', default_timezone='UTC',
58-
date_formats=None, configure_jinja=True):
58+
default_domain='messages', date_formats=None,
59+
configure_jinja=True):
5960
self._default_locale = default_locale
6061
self._default_timezone = default_timezone
62+
self._default_domain = default_domain
6163
self._date_formats = date_formats
6264
self._configure_jinja = configure_jinja
6365
self.app = app
@@ -79,6 +81,7 @@ def init_app(self, app):
7981

8082
app.config.setdefault('BABEL_DEFAULT_LOCALE', self._default_locale)
8183
app.config.setdefault('BABEL_DEFAULT_TIMEZONE', self._default_timezone)
84+
app.config.setdefault('BABEL_DOMAIN', self._default_domain)
8285
if self._date_formats is None:
8386
self._date_formats = self.default_date_formats.copy()
8487

@@ -182,6 +185,12 @@ def default_timezone(self):
182185
"""
183186
return timezone(self.app.config['BABEL_DEFAULT_TIMEZONE'])
184187

188+
@property
189+
def domain(self):
190+
"""The message domain for the translations as a string.
191+
"""
192+
return self.app.config['BABEL_DOMAIN']
193+
185194
@property
186195
def translation_directories(self):
187196
directories = self.app.config.get(
@@ -213,7 +222,11 @@ def get_translations():
213222

214223
babel = current_app.extensions['babel']
215224
for dirname in babel.translation_directories:
216-
catalog = support.Translations.load(dirname, [get_locale()])
225+
catalog = support.Translations.load(
226+
dirname,
227+
[get_locale()],
228+
babel.domain
229+
)
217230
translations.merge(catalog)
218231
# FIXME: Workaround for merge() being really, really stupid. It
219232
# does not copy _info, plural(), or any other instance variables

tests/tests.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ def test_multiple_directories(self):
5555
name='Peter'
5656
) == 'Hallo Peter!'
5757

58+
def test_different_domain(self):
59+
"""
60+
Ensure we can load translations from multiple directories.
61+
"""
62+
b = babel.Babel()
63+
app = flask.Flask(__name__)
64+
65+
app.config.update({
66+
'BABEL_TRANSLATION_DIRECTORIES': 'translations_different_domain',
67+
'BABEL_DEFAULT_LOCALE': 'de_DE',
68+
'BABEL_DOMAIN': 'myapp'
69+
})
70+
71+
b.init_app(app)
72+
73+
with app.test_request_context():
74+
translations = b.list_translations()
75+
76+
assert(len(translations) == 1)
77+
assert(str(translations[0]) == 'de')
78+
79+
assert gettext(u'Good bye') == 'Auf Wiedersehen'
80+
5881
def test_lazy_old_style_formatting(self):
5982
lazy_string = lazy_gettext(u'Hello %(name)s')
6083
assert lazy_string % {u'name': u'test'} == u'Hello test'
Binary file not shown.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# German translations for PROJECT.
2+
# Copyright (C) 2010 ORGANIZATION
3+
# This file is distributed under the same license as the PROJECT project.
4+
# FIRST AUTHOR <EMAIL@ADDRESS>, 2010.
5+
#
6+
msgid ""
7+
msgstr ""
8+
"Project-Id-Version: PROJECT VERSION\n"
9+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
10+
"POT-Creation-Date: 2010-05-29 17:00+0200\n"
11+
"PO-Revision-Date: 2010-05-30 12:56+0200\n"
12+
"Last-Translator: Armin Ronacher <[email protected]>\n"
13+
"Language-Team: de <[email protected]>\n"
14+
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
15+
"MIME-Version: 1.0\n"
16+
"Content-Type: text/plain; charset=utf-8\n"
17+
"Content-Transfer-Encoding: 8bit\n"
18+
"Generated-By: Babel 0.9.5\n"
19+
20+
#: tests.py:94
21+
#, python-format
22+
msgid "Hello %(name)s!"
23+
msgstr "Hallo %(name)s!"
24+
25+
#: tests.py:95 tests.py:96
26+
#, python-format
27+
msgid "%(num)s Apple"
28+
msgid_plural "%(num)s Apples"
29+
msgstr[0] "%(num)s Apfel"
30+
msgstr[1] "%(num)s Äpfel"
31+
32+
#: tests.py:119
33+
msgid "Yes"
34+
msgstr "Ja"
35+
36+
msgid "Good bye"
37+
msgstr "Auf Wiedersehen"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Translations template for PROJECT.
2+
# Copyright (C) 2010 ORGANIZATION
3+
# This file is distributed under the same license as the PROJECT project.
4+
# FIRST AUTHOR <EMAIL@ADDRESS>, 2010.
5+
#
6+
#, fuzzy
7+
msgid ""
8+
msgstr ""
9+
"Project-Id-Version: PROJECT VERSION\n"
10+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
11+
"POT-Creation-Date: 2010-05-30 12:56+0200\n"
12+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14+
"Language-Team: LANGUAGE <[email protected]>\n"
15+
"MIME-Version: 1.0\n"
16+
"Content-Type: text/plain; charset=utf-8\n"
17+
"Content-Transfer-Encoding: 8bit\n"
18+
"Generated-By: Babel 0.9.5\n"
19+
20+
#: tests.py:94
21+
#, python-format
22+
msgid "Hello %(name)s!"
23+
msgstr ""
24+
25+
#: tests.py:95 tests.py:96
26+
#, python-format
27+
msgid "%(num)s Apple"
28+
msgid_plural "%(num)s Apples"
29+
msgstr[0] ""
30+
msgstr[1] ""
31+
32+
#: tests.py:119
33+
msgid "Yes"
34+
msgstr ""
35+
36+
msgid "Good bye"
37+
msgstr ""

0 commit comments

Comments
 (0)