summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2013-02-08 17:20:26 +0100
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2013-02-08 17:20:26 +0100
commitdf61e5c367ce0915bcedb4842bf8783cdd5b116b (patch)
treede4e959aac73711d7e61c1288caffc38d03b2a75
parentf14545082062c204dab2504d5e51e7adb9ee1fe5 (diff)
parent8565c4c13d454819550744672e566bac6714f6c3 (diff)
downloadpykolab-df61e5c367ce0915bcedb4842bf8783cdd5b116b.tar.gz
Merge branch 'master' of ssh://git.kolab.org/git/pykolab
-rw-r--r--pykolab/translit.py107
-rw-r--r--pykolab/utils.py10
-rw-r--r--tests/unit/test-010-transliterate.py39
3 files changed, 155 insertions, 1 deletions
diff --git a/pykolab/translit.py b/pykolab/translit.py
new file mode 100644
index 0000000..ac4d2d7
--- /dev/null
+++ b/pykolab/translit.py
@@ -0,0 +1,107 @@
+# -*- coding: utf-8 -*-
+# Copyright 2010-2012 Kolab Systems AG (http://www.kolabsys.com)
+#
+# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 3 or, at your option, any later version
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Library General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+
+locale_translit_map = {
+ 'ru_RU': 'cyrillic'
+ }
+
+translit_map = {
+ 'cyrillic': {
+ u'А': 'A',
+ u'а': 'a',
+ u'Б': 'B',
+ u'б': 'b',
+ u'В': 'V',
+ u'в': 'v',
+ u'Г': 'G',
+ u'г': 'g',
+ u'Д': 'D',
+ u'д': 'd',
+ u'Е': 'E',
+ u'е': 'e',
+ u'Ё': 'Yo',
+ u'ё': 'e',
+ u'Ж': 'Zh',
+ u'ж': 'zh',
+ u'З': 'Z',
+ u'з': 'z',
+ u'И': 'I',
+ u'и': 'i',
+ u'Й': 'J',
+ u'й': 'j',
+ u'К': 'K',
+ u'к': 'k',
+ u'Л': 'L',
+ u'л': 'l',
+ u'М': 'M',
+ u'м': 'm',
+ u'Н': 'N',
+ u'н': 'n',
+ u'О': 'O',
+ u'о': 'o',
+ u'П': 'P',
+ u'п': 'p',
+ u'Р': 'R',
+ u'р': 'r',
+ u'С': 'S',
+ u'с': 's',
+ u'Т': 'T',
+ u'т': 't',
+ u'У': 'U',
+ u'у': 'u',
+ u'Ф': 'F',
+ u'ф': 'f',
+ u'Х': 'Kh',
+ u'х': 'kh',
+ u'Ц': 'Tc',
+ u'ц': 'tc',
+ u'Ч': 'Ch',
+ u'ч': 'ch',
+ u'Ш': 'Sh',
+ u'ш': 'sh',
+ u'Щ': 'Shch',
+ u'щ': 'shch',
+ u'Ъ': '',
+ u'ъ': '',
+ u'Ы': 'Y',
+ u'ы': 'y',
+ u'Ь': '',
+ u'ь': '',
+ u'Э': 'E',
+ u'э': 'e',
+ u'Ю': 'Yu',
+ u'ю': 'yu',
+ u'Я': 'Ya',
+ u'я': 'ya',
+ }
+ }
+
+def transliterate(_input, lang, _output_expected=None):
+ _translit_name = locale_translit_map[lang]
+
+ _output = ''
+
+ for char in _input:
+ if translit_map[_translit_name].has_key(char):
+ _output += translit_map[_translit_name][char]
+ else:
+ _output += char
+
+ return _output
+
diff --git a/pykolab/utils.py b/pykolab/utils.py
index 7f56c29..c10bbb2 100644
--- a/pykolab/utils.py
+++ b/pykolab/utils.py
@@ -415,10 +415,18 @@ def translate(mystring, locale_name='en_US'):
log.debug(_("Executing '%s | %s'") % (r"%s" % (mystring), ' '.join(command)), level=8)
process = subprocess.Popen(command, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, env={'LANG': locale.normalize(locale_name)})
- print >> process.stdin, r"%s" % mystring
+ try:
+ print >> process.stdin, r"%s" % mystring
+ except UnicodeEncodeError, errmsg:
+ pass
result = process.communicate()[0].strip()
+ if '?' in result or (result == '' and not mystring == ''):
+ log.warning(_("Could not translate %s using locale %s") % (mystring, locale_name))
+ from pykolab import translit
+ result = translit.transliterate(mystring, locale_name)
+
return result
def true_or_false(val):
diff --git a/tests/unit/test-010-transliterate.py b/tests/unit/test-010-transliterate.py
index 7f70700..3c98a1a 100644
--- a/tests/unit/test-010-transliterate.py
+++ b/tests/unit/test-010-transliterate.py
@@ -59,6 +59,45 @@ class TestTransliteration(unittest.TestCase):
self.assertEqual('Alvaro', utils.translate(givenname, preferredlanguage))
self.assertEqual('Fuentes', utils.translate(surname, preferredlanguage))
+ def test_005_raw_ru_RU(self):
+ from pykolab import utils
+ givenname = r'Николай'
+ surname = r'Римский-Корсаков'
+
+ preferredlanguage = 'ru_RU'
+
+ self.assertEqual('Nikolaj', utils.translate(givenname, preferredlanguage))
+ self.assertEqual('Rimskij-Korsakov', utils.translate(surname, preferredlanguage))
+
+ def test_006_unicode_ru_RU(self):
+ from pykolab import utils
+ givenname = u'Николай'
+ surname = u'Римский-Корсаков'
+
+ preferredlanguage = 'ru_RU'
+
+ self.assertEqual('Nikolaj', utils.translate(givenname, preferredlanguage))
+ self.assertEqual('Rimskij-Korsakov', utils.translate(surname, preferredlanguage))
+
+ def test_007_raw_ru_RU(self):
+ from pykolab import utils
+ givenname = r'Юлия'
+ surname = r'Ёлкина'
+
+ preferredlanguage = 'ru_RU'
+
+ self.assertEqual('Yuliya', utils.translate(givenname, preferredlanguage))
+ self.assertEqual('Yolkina', utils.translate(surname, preferredlanguage))
+
+ def test_008_unicode_ru_RU(self):
+ from pykolab import utils
+ givenname = u'Юлия'
+ surname = u'Ёлкина'
+
+ preferredlanguage = 'ru_RU'
+
+ self.assertEqual('Yuliya', utils.translate(givenname, preferredlanguage))
+ self.assertEqual('Yolkina', utils.translate(surname, preferredlanguage))
if __name__ == '__main__':
unittest.main()