Line 0
Link Here
|
|
|
1 |
# -*- coding: utf-8 -*- |
2 |
# |
3 |
# Copyright (C) 2013 Edgewall Software |
4 |
# All rights reserved. |
5 |
# |
6 |
# This software is licensed as described in the file COPYING, which |
7 |
# you should have received as part of this distribution. The terms |
8 |
# are also available at http://trac.edgewall.org/wiki/TracLicense. |
9 |
# |
10 |
# This software consists of voluntary contributions made by many |
11 |
# individuals. For the exact contribution history, see the revision |
12 |
# history and logs, available at http://trac.edgewall.org/log/. |
13 |
|
14 |
import shutil |
15 |
import tempfile |
16 |
import unittest |
17 |
from pkg_resources import resource_exists, resource_filename |
18 |
try: |
19 |
import babel |
20 |
except ImportError: |
21 |
babel = None |
22 |
locale_identifiers = lambda: () |
23 |
else: |
24 |
try: |
25 |
from babel.localedata import locale_identifiers |
26 |
except ImportError: |
27 |
from babel.localedata import list as locale_identifiers |
28 |
|
29 |
from trac.test import EnvironmentStub |
30 |
from trac.util import translation |
31 |
|
32 |
|
33 |
class TranslationsProxyTestCase(unittest.TestCase): |
34 |
|
35 |
def setUp(self): |
36 |
self.env = EnvironmentStub() |
37 |
self.env.path = tempfile.mkdtemp(prefix='trac-tempenv-') |
38 |
|
39 |
def tearDown(self): |
40 |
translation.deactivate() |
41 |
self.env.reset_db() |
42 |
shutil.rmtree(self.env.path) |
43 |
|
44 |
def _get_locale_dir(self): |
45 |
return resource_filename('trac', 'locale') |
46 |
|
47 |
def _get_available_locales(self): |
48 |
return sorted(locale |
49 |
for locale in translation.get_available_locales() |
50 |
if resource_exists('trac', |
51 |
'locale/%s/LC_MESSAGES/messages.mo' |
52 |
% locale)) |
53 |
|
54 |
def test_activate(self): |
55 |
locales = self._get_available_locales() |
56 |
if locales: |
57 |
translation.activate(locales[0], self.env.path) |
58 |
|
59 |
def test_activate_unavailable_locale(self): |
60 |
unavailables = sorted(set(locale_identifiers()) - |
61 |
set(translation.get_available_locales())) or \ |
62 |
('en_US',) |
63 |
locale_dir = self._get_locale_dir() |
64 |
translation.add_domain('catalog1', self.env.path, locale_dir) |
65 |
translation.add_domain('catalog2', self.env.path, locale_dir) |
66 |
translation.activate(unavailables[0], self.env.path) |
67 |
|
68 |
def test_activate_with_non_existent_catalogs(self): |
69 |
locales = self._get_available_locales() |
70 |
if locales: |
71 |
locale_dir = self._get_locale_dir() |
72 |
translation.add_domain('catalog1', self.env.path, locale_dir) |
73 |
translation.add_domain('catalog2', self.env.path, locale_dir) |
74 |
translation.activate(locales[0], self.env.path) |
75 |
|
76 |
|
77 |
def suite(): |
78 |
suite = unittest.TestSuite() |
79 |
suite.addTest(unittest.makeSuite(TranslationsProxyTestCase, 'test')) |
80 |
return suite |
81 |
|
82 |
if __name__ == '__main__': |
83 |
unittest.main(defaultTest='suite') |