summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2015-08-13 10:41:42 +0200
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2015-08-13 10:41:42 +0200
commit3cb92d3f1f6f976cc5f78e60845a43dc117b8946 (patch)
tree1ba11229023aae36fdda3f93d2df1b6511933f0d
parent213cf68e7995859d149973d9e20aa5fdf0387f37 (diff)
downloadpykolab-3cb92d3f1f6f976cc5f78e60845a43dc117b8946.tar.gz
Catch errors in translation, append UTF-8 if not specified to avoid translation-related tracebacks
-rw-r--r--pykolab/translate.py14
-rw-r--r--tests/unit/test-015-translate.py9
2 files changed, 21 insertions, 2 deletions
diff --git a/pykolab/translate.py b/pykolab/translate.py
index ce9e406..7bac219 100644
--- a/pykolab/translate.py
+++ b/pykolab/translate.py
@@ -26,10 +26,18 @@ import gettext
import os
N_ = lambda x: x
-_ = lambda x: current.lgettext(x)
+
+# This function as such may, at times, cause tracebacks.
+#_ = lambda x: current.lgettext(x)
current = gettext.translation(domain, fallback=True)
+def _(x):
+ try:
+ return current.lgettext(x)
+ except Exception, errmsg:
+ return x
+
def getDefaultLangs():
languages = []
for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
@@ -46,11 +54,15 @@ def getDefaultLangs():
for nelang in gettext._expand_lang(lang):
if nelang not in nelangs:
nelangs.append(nelang)
+
return nelangs
def setUserLanguage(lang):
global current
+ if not len(lang.split('.')) > 1 and not lang.endswith('.UTF-8'):
+ lang = "%s.UTF-8" % (lang)
+
langs = []
for l in gettext._expand_lang(lang):
if l not in langs:
diff --git a/tests/unit/test-015-translate.py b/tests/unit/test-015-translate.py
index 6819b80..5eaf5d1 100644
--- a/tests/unit/test-015-translate.py
+++ b/tests/unit/test-015-translate.py
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
import unittest
import gettext
from pykolab import translate
@@ -17,7 +19,12 @@ class TestTranslate(unittest.TestCase):
translate.setUserLanguage('de_DE')
self.assertEqual(_("Folder name"), "Ordnername", "German Translation found")
translate.setUserLanguage('foo_bar')
- self.assertEqual(_("Folder name"), "Folder name", "Unkonwn language falls back to NullTranslations")
+ self.assertEqual(_("Folder name"), "Folder name", "Unknown language falls back to NullTranslations")
+
+ def test_004_unicode(self):
+ from pykolab.translate import _
+ translate.setUserLanguage('de_DE')
+ self.assertEqual(_("recipients: %r"), "Empfänger: %r")
if __name__ == '__main__':
unittest.main()