def test_domain(self): # Note, set_as_global is True, so second overrides first. translation.add_domain('foo', self._localedir('locale1')) translation.add_domain('bar', self._localedir('locale1')) translation.set_language(('en_US',)) self.assertEqual(translation.gettext('bar'), 'blah') self.assertEqual(translation.gettext('test'), 'test') self.assertEqual(translation.gettext('test2'), 'foo') # dgettext and ldgettext should be the same. for method in ('dgettext', 'ldgettext', 'dgettext_lazy', 'ldgettext_lazy'): m = getattr(translation, method) self.assertEqual(m('foo', 'bar'), 'bar') self.assertEqual(m('foo', 'test'), 'test') self.assertEqual(m('foo', 'test2'), 'test2') self.assertEqual(m('bar', 'bar'), 'blah') self.assertEqual(m('bar', 'test'), 'test') self.assertEqual(m('bar', 'test2'), 'foo') translation.set_language(('es',)) for method in ('dgettext', 'ldgettext', 'dgettext_lazy', 'ldgettext_lazy'): m = getattr(translation, method) self.assertEqual(m('foo', 'bar'), 'bar') # Locale conversion won't work with US-ASCII if not method.startswith('ldgettext'): self.assertEqual(m('foo', 'test'), 'Español') self.assertEqual(m('foo', 'test2'), 'quesadilla') self.assertEqual(m('bar', 'bar'), 'bar') if not method.startswith('ldgettext'): self.assertEqual(m('bar', 'test'), 'Español-bar') self.assertEqual(m('bar', 'test2'), 'test2')
def test_domain(self): # Note, set_as_global is True, so second overrides first. translation.add_domain('foo', self._localedir('locale1')) translation.add_domain('bar', self._localedir('locale1')) translation.set_language(('en_US', )) self.assertEqual(translation.gettext('bar'), 'blah') self.assertEqual(translation.gettext('test'), 'test') self.assertEqual(translation.gettext('test2'), 'foo') # dgettext and ldgettext should be the same. for method in ('dgettext', 'ldgettext', 'dgettext_lazy', 'ldgettext_lazy'): m = getattr(translation, method) self.assertEqual(m('foo', 'bar'), 'bar') self.assertEqual(m('foo', 'test'), 'test') self.assertEqual(m('foo', 'test2'), 'test2') self.assertEqual(m('bar', 'bar'), 'blah') self.assertEqual(m('bar', 'test'), 'test') self.assertEqual(m('bar', 'test2'), 'foo') translation.set_language(('es', )) for method in ('dgettext', 'ldgettext', 'dgettext_lazy', 'ldgettext_lazy'): m = getattr(translation, method) self.assertEqual(m('foo', 'bar'), 'bar') # Locale conversion won't work with US-ASCII if not method.startswith('ldgettext'): self.assertEqual(m('foo', 'test'), 'Español') self.assertEqual(m('foo', 'test2'), 'quesadilla') self.assertEqual(m('bar', 'bar'), 'bar') if not method.startswith('ldgettext'): self.assertEqual(m('bar', 'test'), 'Español-bar') self.assertEqual(m('bar', 'test2'), 'test2')
def test_language(self): translation.add_domain('foo', self._localedir('locale1')) # Test environment variable as default. os.environ['LANG'] = 'de' try: self.assertEqual(translation.gettext('test4'), 'freundleben') finally: del os.environ['LANG'] # Test 'C'. translation.set_language(('C',)) self.assertEqual(translation.gettext('test4'), 'test4') # Test lang method. def lang(): return ('fr', 'es') translation.set_language(lang) self.assertEqual(translation.gettext('test4'), 'paella')
def test_language(self): translation.add_domain('foo', self._localedir('locale1')) # Test environment variable as default. os.environ['LANG'] = 'de' try: self.assertEqual(translation.gettext('test4'), 'freundleben') finally: del os.environ['LANG'] # Test 'C'. translation.set_language(('C', )) self.assertEqual(translation.gettext('test4'), 'test4') # Test lang method. def lang(): return ('fr', 'es') translation.set_language(lang) self.assertEqual(translation.gettext('test4'), 'paella')
def test_refresh(self): for check_timestamps in (True, False): translation.reset() lazy = translation.gettext_lazy('test') self.assertEqual(str(lazy), 'test') self.assertEqual(translation.gettext('test'), 'test') translation.set_language(('en', )) path = os.path.join(os.getcwd(), 'translation', 'locale-dynamic', 'en_US', 'LC_MESSAGES') target = os.path.join(path, 'foo.mo') if os.path.exists(target): os.unlink(target) shutil.copyfile(os.path.join(path, 'foo1.mo'), target) # Make sure timestamps are different. t = time.time() os.utime(target, (t - 120, t - 120)) translation.add_domain('foo', self._localedir('locale-dynamic')) self.assertEqual(str(lazy), 'test-dyn1') self.assertEqual(translation.gettext('test'), 'test-dyn1') os.unlink(target) # Make sure timestamps are different. shutil.copyfile(os.path.join(path, 'foo2.mo'), target) os.utime(target, (t - 60, t - 60)) translation.get_domain('foo').refresh(check_timestamps) self.assertEqual(str(lazy), 'test-dyn2') self.assertEqual(translation.gettext('test'), 'test-dyn2') os.unlink(target) # Make sure timestamps are different. shutil.copyfile(os.path.join(path, 'foo1.mo'), target) os.utime(target, (t - 30, t - 30)) translation.get_domain('foo').refresh(check_timestamps) self.assertEqual(str(lazy), 'test-dyn1') self.assertEqual(translation.gettext('test'), 'test-dyn1')
def test_funky_encoding(self): translation.add_domain('foo', self._localedir('locale1')) translation.set_language(('ja_JP.SJIS',)) self.assertEqual(translation.gettext('test'), '\x82\x9f\x83@\xea\x9f\xea\xa2') # Default locale is normally us-ascii, can't convert shift-jis to ASCII. self.assertRaises(UnicodeEncodeError, translation.lgettext, 'test') os.environ['LANG'] = 'ja_JP.SJIS' try: self.assertEqual(translation.lgettext('test'), '\x82\x9f\x83@\xea\x9f\xea\xa2') os.environ['LANG'] = 'ja_JP.UTF-8' self.assertEqual(translation.lgettext('test'), '\xe3\x81\x81\xe3\x82\xa1\xe5\xa0\xaf\xe7\x91\xa4') finally: del os.environ['LANG'] self.assertEqual(translation.ugettext('test'), u'\u3041\u30a1\u582f\u7464')
def test_refresh(self): for check_timestamps in (True, False): translation.reset() lazy = translation.gettext_lazy('test') self.assertEqual(str(lazy), 'test') self.assertEqual(translation.gettext('test'), 'test') translation.set_language(('en',)) path = os.path.join(os.getcwd(), 'translation', 'locale-dynamic', 'en_US', 'LC_MESSAGES') target = os.path.join(path, 'foo.mo') if os.path.exists(target): os.unlink(target) shutil.copyfile(os.path.join(path, 'foo1.mo'), target) # Make sure timestamps are different. t = time.time() os.utime(target, (t-120, t-120)) translation.add_domain('foo', self._localedir('locale-dynamic')) self.assertEqual(str(lazy), 'test-dyn1') self.assertEqual(translation.gettext('test'), 'test-dyn1') os.unlink(target) # Make sure timestamps are different. shutil.copyfile(os.path.join(path, 'foo2.mo'), target) os.utime(target, (t-60, t-60)) translation.get_domain('foo').refresh(check_timestamps) self.assertEqual(str(lazy), 'test-dyn2') self.assertEqual(translation.gettext('test'), 'test-dyn2') os.unlink(target) # Make sure timestamps are different. shutil.copyfile(os.path.join(path, 'foo1.mo'), target) os.utime(target, (t-30, t-30)) translation.get_domain('foo').refresh(check_timestamps) self.assertEqual(str(lazy), 'test-dyn1') self.assertEqual(translation.gettext('test'), 'test-dyn1')
def test_funky_encoding(self): translation.add_domain('foo', self._localedir('locale1')) translation.set_language(('ja_JP.SJIS', )) self.assertEqual(translation.gettext('test'), '\x82\x9f\x83@\xea\x9f\xea\xa2') # Default locale is normally us-ascii, can't convert shift-jis to ASCII. self.assertRaises(UnicodeEncodeError, translation.lgettext, 'test') os.environ['LANG'] = 'ja_JP.SJIS' try: self.assertEqual(translation.lgettext('test'), '\x82\x9f\x83@\xea\x9f\xea\xa2') os.environ['LANG'] = 'ja_JP.UTF-8' self.assertEqual( translation.lgettext('test'), '\xe3\x81\x81\xe3\x82\xa1\xe5\xa0\xaf\xe7\x91\xa4') finally: del os.environ['LANG'] self.assertEqual(translation.ugettext('test'), u'\u3041\u30a1\u582f\u7464')
def test_normalization(self): translation.add_domain('foo', self._localedir('locale1')) translation.set_language(('en', )) self.assertEqual(translation.gettext('test4'), 'oddball') translation.set_language(('es_ES', )) self.assertEqual(translation.gettext('test4'), 'paella') translation.set_language(('en_GB', )) self.assertEqual(translation.gettext('test4'), 'test4') translation.set_language(('en_GB', 'en')) self.assertEqual(translation.gettext('test4'), 'oddball') translation.set_language(('es_ES.UTF-8', )) self.assertEqual(translation.gettext('test4'), 'paella') translation.set_language(('de_DE@euro', )) self.assertEqual(translation.gettext('test4'), 'freundleben')
def test_normalization(self): translation.add_domain('foo', self._localedir('locale1')) translation.set_language(('en',)) self.assertEqual(translation.gettext('test4'), 'oddball') translation.set_language(('es_ES',)) self.assertEqual(translation.gettext('test4'), 'paella') translation.set_language(('en_GB',)) self.assertEqual(translation.gettext('test4'), 'test4') translation.set_language(('en_GB', 'en')) self.assertEqual(translation.gettext('test4'), 'oddball') translation.set_language(('es_ES.UTF-8',)) self.assertEqual(translation.gettext('test4'), 'paella') translation.set_language(('de_DE@euro',)) self.assertEqual(translation.gettext('test4'), 'freundleben')
def test_thread_local(self): translation.add_domain('foo', self._localedir('locale1')) self.assertEqual(translation.gettext('test'), 'test') self.assertEqual(translation.gettext('test4'), 'test4') translation.set_language(('es', )) self.assertEqual(translation.gettext('test'), 'Español') self.assertEqual(translation.gettext('test4'), 'paella') translation.set_language_current_thread(('de', )) self.assertEqual(translation.gettext('test'), 'groß') self.assertEqual(translation.gettext('test4'), 'freundleben') translation.reset() translation.add_domain('foo', self._localedir('locale1')) self.assertEqual(translation.gettext('test'), 'test') self.assertEqual(translation.gettext('test4'), 'test4') t = translation.gettext_lazy('test') self.assertEqual(t, 'test') current = [('es', )] def lang(): return current[0] translation.set_language_current_thread(lang) self.assertEqual(t, 'Español') self.assertEqual(translation.gettext('test'), 'Español') self.assertEqual(translation.gettext('test4'), 'paella') current[0] = ('de', ) self.assertEqual(t, 'groß') self.assertEqual(translation.gettext('test'), 'groß') self.assertEqual(translation.gettext('test4'), 'freundleben')
def test_catalog_update(self): translation.add_domain('foo', self._localedir('locale1')) translation.set_language(('en', )) self.assertEqual(translation.gettext('test'), 'test') self.assertEqual(translation.gettext('test4'), 'oddball') translation.add_domain('foo', self._localedir('locale2')) self.assertEqual(translation.gettext('test'), 'test') self.assertEqual(translation.gettext('test1'), 'test1') self.assertEqual(translation.gettext('test2'), '2-test2') self.assertEqual(translation.gettext('test3'), 'test3') self.assertEqual(translation.gettext('asdf1'), '2-asdf1') self.assertEqual(translation.gettext('asdf2'), '2-asdf2') self.assertEqual(translation.gettext('test4'), 'oddball') self.assertEqual(translation.gettext('test5'), 'test5') translation.reset() translation.add_domain('foo', self._localedir('locale2')) translation.set_language(('en', )) self.assertEqual(translation.gettext('test'), '2-test') self.assertEqual(translation.gettext('test1'), '2-test1') self.assertEqual(translation.gettext('test2'), '2-test2') self.assertEqual(translation.gettext('test3'), '2-test3') self.assertEqual(translation.gettext('asdf1'), '2-asdf1') self.assertEqual(translation.gettext('asdf2'), '2-asdf2') self.assertEqual(translation.gettext('test4'), 'test4') self.assertEqual(translation.gettext('test5'), 'test5') translation.add_domain('foo', self._localedir('locale1')) self.assertEqual(translation.gettext('test'), '2-test') self.assertEqual(translation.gettext('test1'), '2-test1') self.assertEqual(translation.gettext('test2'), '2-test2') self.assertEqual(translation.gettext('test3'), '2-test3') self.assertEqual(translation.gettext('asdf1'), '2-asdf1') self.assertEqual(translation.gettext('asdf2'), '2-asdf2') self.assertEqual(translation.gettext('test4'), 'oddball') self.assertEqual(translation.gettext('test5'), 'test5') # Make something that updates. translation.reset() translation.set_language(('en', )) path = os.path.join(os.getcwd(), 'translation', 'locale-dynamic', 'en_US', 'LC_MESSAGES') target = os.path.join(path, 'foo.mo') if os.path.exists(target): os.unlink(target) os.symlink(os.path.join(path, 'foo1.mo'), target) translation.add_domain('foo', self._localedir('locale-dynamic')) self.assertEqual(translation.gettext('test'), 'test-dyn1') os.unlink(target) os.symlink(os.path.join(path, 'foo2.mo'), target) translation.add_domain('foo', self._localedir('locale-dynamic')) self.assertEqual(translation.gettext('test'), 'test-dyn2')
def test_thread_local(self): translation.add_domain('foo', self._localedir('locale1')) self.assertEqual(translation.gettext('test'), 'test') self.assertEqual(translation.gettext('test4'), 'test4') translation.set_language(('es',)) self.assertEqual(translation.gettext('test'), 'Español') self.assertEqual(translation.gettext('test4'), 'paella') translation.set_language_current_thread(('de',)) self.assertEqual(translation.gettext('test'), 'groß') self.assertEqual(translation.gettext('test4'), 'freundleben') translation.reset() translation.add_domain('foo', self._localedir('locale1')) self.assertEqual(translation.gettext('test'), 'test') self.assertEqual(translation.gettext('test4'), 'test4') t = translation.gettext_lazy('test') self.assertEqual(t, 'test') current = [('es',)] def lang(): return current[0] translation.set_language_current_thread(lang) self.assertEqual(t, 'Español') self.assertEqual(translation.gettext('test'), 'Español') self.assertEqual(translation.gettext('test4'), 'paella') current[0] = ('de',) self.assertEqual(t, 'groß') self.assertEqual(translation.gettext('test'), 'groß') self.assertEqual(translation.gettext('test4'), 'freundleben')
def test_catalog_update(self): translation.add_domain('foo', self._localedir('locale1')) translation.set_language(('en',)) self.assertEqual(translation.gettext('test'), 'test') self.assertEqual(translation.gettext('test4'), 'oddball') translation.add_domain('foo', self._localedir('locale2')) self.assertEqual(translation.gettext('test'), 'test') self.assertEqual(translation.gettext('test1'), 'test1') self.assertEqual(translation.gettext('test2'), '2-test2') self.assertEqual(translation.gettext('test3'), 'test3') self.assertEqual(translation.gettext('asdf1'), '2-asdf1') self.assertEqual(translation.gettext('asdf2'), '2-asdf2') self.assertEqual(translation.gettext('test4'), 'oddball') self.assertEqual(translation.gettext('test5'), 'test5') translation.reset() translation.add_domain('foo', self._localedir('locale2')) translation.set_language(('en',)) self.assertEqual(translation.gettext('test'), '2-test') self.assertEqual(translation.gettext('test1'), '2-test1') self.assertEqual(translation.gettext('test2'), '2-test2') self.assertEqual(translation.gettext('test3'), '2-test3') self.assertEqual(translation.gettext('asdf1'), '2-asdf1') self.assertEqual(translation.gettext('asdf2'), '2-asdf2') self.assertEqual(translation.gettext('test4'), 'test4') self.assertEqual(translation.gettext('test5'), 'test5') translation.add_domain('foo', self._localedir('locale1')) self.assertEqual(translation.gettext('test'), '2-test') self.assertEqual(translation.gettext('test1'), '2-test1') self.assertEqual(translation.gettext('test2'), '2-test2') self.assertEqual(translation.gettext('test3'), '2-test3') self.assertEqual(translation.gettext('asdf1'), '2-asdf1') self.assertEqual(translation.gettext('asdf2'), '2-asdf2') self.assertEqual(translation.gettext('test4'), 'oddball') self.assertEqual(translation.gettext('test5'), 'test5') # Make something that updates. translation.reset() translation.set_language(('en',)) path = os.path.join(os.getcwd(), 'translation', 'locale-dynamic', 'en_US', 'LC_MESSAGES') target = os.path.join(path, 'foo.mo') if os.path.exists(target): os.unlink(target) os.symlink(os.path.join(path, 'foo1.mo'), target) translation.add_domain('foo', self._localedir('locale-dynamic')) self.assertEqual(translation.gettext('test'), 'test-dyn1') os.unlink(target) os.symlink(os.path.join(path, 'foo2.mo'), target) translation.add_domain('foo', self._localedir('locale-dynamic')) self.assertEqual(translation.gettext('test'), 'test-dyn2')