Merge pull request #1852 from ilvalle/fix_languages_py3

fix py3 languages decode
This commit is contained in:
mdipierro
2018-02-24 10:57:17 -06:00
committed by GitHub
2 changed files with 53 additions and 3 deletions
+8 -2
View File
@@ -431,10 +431,16 @@ class lazyT(object):
return str(self) if self.M else local_html_escape(str(self), quote=False)
def encode(self, *a, **b):
return str(self).encode(*a, **b)
if PY2 and a[0] != 'utf8':
return to_unicode(str(self)).encode(*a, **b)
else:
return str(self)
def decode(self, *a, **b):
return str(self).decode(*a, **b)
if PY2:
return str(self).decode(*a, **b)
else:
return str(self)
def read(self):
return str(self)
+45 -1
View File
@@ -12,7 +12,9 @@ import tempfile
import unittest
from gluon import languages
from gluon._compat import PY2
from gluon._compat import PY2, to_unicode, to_bytes
from gluon.storage import Messages
from gluon.html import SPAN
MP_WORKING = 0
try:
@@ -108,6 +110,8 @@ class TestTranslations(unittest.TestCase):
T.force('it')
self.assertEqual(str(T('Hello World')),
'Salve Mondo')
self.assertEqual(to_unicode(T('Hello World')),
'Salve Mondo')
class TestDummyApp(unittest.TestCase):
@@ -179,3 +183,43 @@ def index():
for key in ['hello', 'world', '%s %%{shop}', 'ahoy']:
self.assertTrue(key in en_dict)
self.assertTrue(key in pt_dict)
class TestMessages(unittest.TestCase):
def setUp(self):
if os.path.isdir('gluon'):
self.langpath = 'applications/welcome/languages'
else:
self.langpath = os.path.realpath(
'../../applications/welcome/languages')
self.http_accept_language = 'en'
def tearDown(self):
pass
def test_decode(self):
T = languages.translator(self.langpath, self.http_accept_language)
messages = Messages(T)
messages.update({'email_sent':'Email sent', 'test': "ä"})
self.assertEqual(to_unicode(messages.email_sent, 'utf-8'), 'Email sent')
class TestHTMLTag(unittest.TestCase):
def setUp(self):
if os.path.isdir('gluon'):
self.langpath = 'applications/welcome/languages'
else:
self.langpath = os.path.realpath(
'../../applications/welcome/languages')
self.http_accept_language = 'en'
def tearDown(self):
pass
def test_decode(self):
T = languages.translator(self.langpath, self.http_accept_language)
elem = SPAN(T("Complete"))
self.assertEqual(elem.flatten(), "Complete")
elem = SPAN(T("Cannot be empty", language="ru"))
self.assertEqual(elem.xml(), to_bytes('<span>Пустое значение недопустимо</span>'))
self.assertEqual(elem.flatten(), 'Пустое значение недопустимо')