Skip to content

Commit e450f3a

Browse files
committed
Support for different text domains
Both gettext and Babel make heavy use of what is called the text domain or message domain [1]. This essentially is a unique name that identifies the application, and it's also used to name the `pot`, `po` and `mo` files. The code and the documentation currently assume that the domain is always ``messages``, which is fine for a standalone application. But when integrating with other applications or migrating old code bases you might need to use a different domain. This patch adds a new optional configuration option to define the domain. It also adds documentation for the BABEL_TRANSLATION_DIRECTORIES option added on 11c7b29. [1] https://www.gnu.org/software/gawk/manual/html_node/Explaining-gettext.html
1 parent d766a89 commit e450f3a

File tree

6 files changed

+126
-13
lines changed

6 files changed

+126
-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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ class Babel(object):
5959
})
6060

6161
def __init__(self, app=None, default_locale='en', default_timezone='UTC',
62-
date_formats=None, configure_jinja=True):
62+
default_domain='messages', date_formats=None,
63+
configure_jinja=True):
6364
self._default_locale = default_locale
6465
self._default_timezone = default_timezone
66+
self._default_domain = default_domain
6567
self._date_formats = date_formats
6668
self._configure_jinja = configure_jinja
6769
self.app = app
@@ -83,6 +85,7 @@ def init_app(self, app):
8385

8486
app.config.setdefault('BABEL_DEFAULT_LOCALE', self._default_locale)
8587
app.config.setdefault('BABEL_DEFAULT_TIMEZONE', self._default_timezone)
88+
app.config.setdefault('BABEL_DOMAIN', self._default_domain)
8689
if self._date_formats is None:
8790
self._date_formats = self.default_date_formats.copy()
8891

@@ -186,6 +189,12 @@ def default_timezone(self):
186189
"""
187190
return timezone(self.app.config['BABEL_DEFAULT_TIMEZONE'])
188191

192+
@property
193+
def domain(self):
194+
"""The message domain for the translations as a string.
195+
"""
196+
return self.app.config['BABEL_DOMAIN']
197+
189198
@property
190199
def translation_directories(self):
191200
directories = self.app.config.get(
@@ -217,7 +226,8 @@ def get_translations():
217226
translations.merge(
218227
support.Translations.load(
219228
dirname,
220-
[get_locale()]
229+
[get_locale()],
230+
babel.domain
221231
)
222232
)
223233

tests/tests.py

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

56+
def test_different_domain(self):
57+
"""
58+
Ensure we can load translations from multiple directories.
59+
"""
60+
b = babel.Babel()
61+
app = flask.Flask(__name__)
62+
63+
app.config.update({
64+
'BABEL_TRANSLATION_DIRECTORIES': 'translations_different_domain',
65+
'BABEL_DEFAULT_LOCALE': 'de_DE',
66+
'BABEL_DOMAIN': 'myapp'
67+
})
68+
69+
b.init_app(app)
70+
71+
with app.test_request_context():
72+
translations = b.list_translations()
73+
74+
assert(len(translations) == 1)
75+
assert(str(translations[0]) == 'de')
76+
77+
assert gettext(u'Good bye') == 'Auf Wiedersehen'
78+
5679

5780
class DateFormattingTestCase(unittest.TestCase):
5881

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)