Index: /branches/1.0-stable/trac/util/tests/__init__.py =================================================================== --- /branches/1.0-stable/trac/util/tests/__init__.py (revision 12274) +++ /branches/1.0-stable/trac/util/tests/__init__.py (revision 12275) @@ -22,5 +22,6 @@ from trac import util -from trac.util.tests import concurrency, datefmt, presentation, text, html +from trac.util.tests import concurrency, datefmt, presentation, text, \ + translation, html @@ -182,4 +183,5 @@ suite.addTest(doctest.DocTestSuite(util)) suite.addTest(text.suite()) + suite.addTest(translation.suite()) suite.addTest(html.suite()) return suite Index: /branches/1.0-stable/trac/util/tests/translation.py =================================================================== --- /branches/1.0-stable/trac/util/tests/translation.py (revision 12275) +++ /branches/1.0-stable/trac/util/tests/translation.py (revision 12275) @@ -0,0 +1,83 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2013 Edgewall Software +# All rights reserved. +# +# This software is licensed as described in the file COPYING, which +# you should have received as part of this distribution. The terms +# are also available at http://trac.edgewall.org/wiki/TracLicense. +# +# This software consists of voluntary contributions made by many +# individuals. For the exact contribution history, see the revision +# history and logs, available at http://trac.edgewall.org/log/. + +import shutil +import tempfile +import unittest +from pkg_resources import resource_exists, resource_filename +try: + import babel +except ImportError: + babel = None + locale_identifiers = lambda: () +else: + try: + from babel.localedata import locale_identifiers + except ImportError: + from babel.localedata import list as locale_identifiers + +from trac.test import EnvironmentStub +from trac.util import translation + + +class TranslationsProxyTestCase(unittest.TestCase): + + def setUp(self): + self.env = EnvironmentStub() + self.env.path = tempfile.mkdtemp(prefix='trac-tempenv-') + + def tearDown(self): + translation.deactivate() + self.env.reset_db() + shutil.rmtree(self.env.path) + + def _get_locale_dir(self): + return resource_filename('trac', 'locale') + + def _get_available_locales(self): + return sorted(locale + for locale in translation.get_available_locales() + if resource_exists('trac', + 'locale/%s/LC_MESSAGES/messages.mo' + % locale)) + + def test_activate(self): + locales = self._get_available_locales() + if locales: + translation.activate(locales[0], self.env.path) + + def test_activate_unavailable_locale(self): + unavailables = sorted(set(locale_identifiers()) - + set(translation.get_available_locales())) or \ + ('en_US',) + locale_dir = self._get_locale_dir() + translation.add_domain('catalog1', self.env.path, locale_dir) + translation.add_domain('catalog2', self.env.path, locale_dir) + translation.activate(unavailables[0], self.env.path) + + def test_activate_with_non_existent_catalogs(self): + locales = self._get_available_locales() + if locales: + locale_dir = self._get_locale_dir() + translation.add_domain('catalog1', self.env.path, locale_dir) + translation.add_domain('catalog2', self.env.path, locale_dir) + translation.activate(locales[0], self.env.path) + + +def suite(): + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(TranslationsProxyTestCase, 'test')) + return suite + +if __name__ == '__main__': + unittest.main(defaultTest='suite') Index: /branches/1.0-stable/trac/util/translation.py =================================================================== --- /branches/1.0-stable/trac/util/translation.py (revision 12274) +++ /branches/1.0-stable/trac/util/translation.py (revision 12275) @@ -150,6 +150,6 @@ t = self._null_translations else: - t.add(Translations.load(locale_dir, locale or 'en_US', - 'tracini')) + self._add(t, Translations.load(locale_dir, locale or 'en_US', + 'tracini')) if env_path: with self._plugin_domains_lock: @@ -157,5 +157,6 @@ domains = domains.items() for domain, dirname in domains: - t.add(Translations.load(dirname, locale, domain)) + self._add(t, Translations.load(dirname, locale, + domain)) self._current.translations = t self._activate_failed = False @@ -184,4 +185,10 @@ return self._current.translations is not None \ or self._activate_failed + + # Internal methods + + def _add(self, t, translations): + if isinstance(translations, Translations): + t.add(translations) # Delegated methods