[Zope-Checkins] CVS: Zope3/lib/python/Zope/I18n/tests - testITranslationService.py:1.1.2.1
Jim Fulton
jim@zope.com
Thu, 17 Jan 2002 19:04:54 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/I18n/tests
In directory cvs.zope.org:/tmp/cvs-serv4109
Added Files:
Tag: Zope-3x-branch
testITranslationService.py
Log Message:
Stephan Richter and Jim:
Initial cut at translation service interface and tests.
=== Added File Zope3/lib/python/Zope/I18n/tests/testITranslationService.py ===
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
import unittest, sys
from Zope.I18n.ITranslationService import ITranslationService
from Zope.I18n.ITranslationService import IVariable
class TestITranslationService(unittest.TestCase):
def setUp(self):
''' '''
self._service = StubService('en', 'default')
self._service.add('en', ('Hello',), 'de', ('Hallo',))
self._service.add('en', ('Hello ', Var('name'), ', how are you?'),
'de', ('Hallo ', Var('name'), ', wie geht es Dir?'))
def testSimpleNoTranslate(self):
service = self._service
self.assertRaises(TypeError, service.translate, 'Hello')
self.assertEqual(service.translate('Hello',
destination_language_tag='es'),
'Hello')
self.assertEqual(service.translate('Hello', xxx=noLang), 'Hello')
self.assertRaises(TypeError, service.translate, 'Hello', xxx=None)
def testSimpleTranslate(self):
''' '''
service = self._service
self.assertEqual(service.translate('Hello',
destination_language_tag='de'),
'Hallo')
def testSequenceTranslate(self):
''' '''
service = self._service
self.assertEqual(list(service.translateSequence(('Hello',),
destination_language_tag='de')),
['Hallo'])
def testDynamicSequenceTranslate(self):
''' '''
service = self._service
self.assertEqual(list(service.translateSequence(
('Hello ', Var('name'), ', how are you?'),
destination_language_tag='de', data={'name': 'Stephan'})),
['Hallo Stephan, wie geht es Dir?'])
noLang = ''
class StubService:
''' '''
__implements__ = ITranslationService
def __init__(self, default_src_lang, default_domain=''):
'''Our _data is a
{(src_lang, dst_lang, domain) -> {src_text -> dst_txt}}'''
self.default_src_lang = default_src_lang
self.default_domain = default_domain
self._data = {}
def add(self, src_lang, src_text, dst_lang, dst_text, domain=None):
''''''
if domain is None:
domain = self.default_domain
m = self._data.setdefault((src_lang, dst_lang, domain), {})
m[src_text] = dst_text
############################################################
# Implementation methods for interface
# Zope.I18n.ITranslationService.
def translate(self, source_text, xxx=None, source_language_tag=None,
destination_language_tag=None, domain=None):
'''See interface ITranslationService'''
result = self.translateSequence((source_text,), xxx,
source_language_tag,
destination_language_tag, domain)
return result[0]
def getTranslation(self, message_id, xxx=None,
destination_language_tag=None, domain=None,
data=None):
'''See interface ITranslationService'''
def translateSequence(self, source_sequence, xxx=None,
source_language_tag=None,
destination_language_tag=None,
domain=None, data=None):
'''See interface ITranslationService'''
if source_language_tag is None:
source_language_tag = self.default_src_lang
if domain is None:
domain = self.default_domain
if destination_language_tag is None:
if xxx is None:
raise TypeError, 'No destination language'
else:
destination_language_tag = xxx
t = self._data.get((source_language_tag,
destination_language_tag,
domain), {}
).get(source_sequence, source_sequence)
result = []
text = []
for elem in t:
if type(elem) is type(''):
text.append(elem)
else:
value = data.get(elem.getInterpolationName(), None)
if value is None:
if text:
result.append(''.join(text))
text = []
result.append(elem)
else:
text.append(str(value))
if text:
result.append(''.join(text))
return result
#
############################################################
class Var:
''' '''
__implements__ = IVariable
def __init__(self, name):
''' '''
self._name = name
def __hash__(self):
''' '''
return hash(self._name)
def __cmp__(self, other):
''' '''
return cmp(self._name, other._name)
############################################################
# Implementation methods for interface
# Zope.I18n.ITranslationService.IVariable
def getInterpolationName(self):
'''See interface IVariable'''
return self._name
#
############################################################
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(TestITranslationService)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())