diff --git a/docs/index.rst b/docs/index.rst index 0af7f28..57d87fc 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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 diff --git a/flask_babel/__init__.py b/flask_babel/__init__.py index 2664676..0e9964c 100644 --- a/flask_babel/__init__.py +++ b/flask_babel/__init__.py @@ -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() @@ -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 @@ -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(): @@ -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 diff --git a/tests/tests.py b/tests/tests.py index aefba35..a4b5750 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -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'