[Zope3-checkins] CVS: Zope3/src/zope/i18n/tests - test_translator.py:1.1 test_globaltranslationservice.py:1.4

Barry Warsaw barry@wooz.org
Tue, 25 Mar 2003 15:21:59 -0500


Update of /cvs-repository/Zope3/src/zope/i18n/tests
In directory cvs.zope.org:/tmp/cvs-serv25905/src/zope/i18n/tests

Modified Files:
	test_globaltranslationservice.py 
Added Files:
	test_translator.py 
Log Message:
Add an ITranslator interface and a Translator implementation of that
interface.  This is basically a glue class for specifying the domain,
context, and locale in one packet and then providing a translate()
method which just takes the message id and mapping.

Glue this into the Zope app's page template engine.

Also removed some unused code and fix some typos.


=== Added File Zope3/src/zope/i18n/tests/test_translator.py ===
##############################################################################
#
# Copyright (c) 2003 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.
#
##############################################################################
"""This module tests the ITranslator / Translator

$Id: test_translator.py,v 1.1 2003/03/25 20:21:28 bwarsaw Exp $
"""

import os
import unittest

from zope.i18n.globaltranslationservice import GlobalTranslationService
from zope.i18n.interfaces import IReadTranslationService
from zope.i18n.interfaces import ILocaleIdentity, ILocale
from zope.i18n.translate import Translator
from zope.i18n.gettextmessagecatalog import GettextMessageCatalog
from zope.i18n.tests.test_globaltranslationservice import testdir
from zope.app.tests.placelesssetup import PlacelessSetup
from zope.component import getServiceManager


class TranslatorTests(unittest.TestCase, PlacelessSetup):
    def setUp(self):
        # Create all the goo for placeless services
        PlacelessSetup.setUp(self)
        # Create a global translation service, initialized with a bunch of
        # catalogs (stolen from test_globaltranslationservice.py).
        path = testdir()
        service = GlobalTranslationService('default')
        de_catalog = GettextMessageCatalog('de', 'default',
                                           os.path.join(path, 'de-default.mo'))
        service.addCatalog(de_catalog)
        # Now register the translation service
        getServiceManager(None).defineService('Translation',
                                              IReadTranslationService)
        getServiceManager(None).provideService('Translation',
                                               service)
        # Create a stub ILocaleIdentity
        class LocaleIdentityStub:
            # Lie -- we're only going to implement part of the interface
            __implements__ = ILocaleIdentity
            language = 'de'

        # Create a stub ILocale
        class LocaleStub:
            # Lie -- we're only going to implement part of the interface
            __implements__ = ILocale
            id = LocaleIdentityStub()

        self._locale = LocaleStub()

    def test_translator(self):
        translator = Translator(self._locale, 'default', None)
        self.assertEqual(translator.translate('short_greeting'), 'Hallo!')



def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TranslatorTests))
    return suite


=== Zope3/src/zope/i18n/tests/test_globaltranslationservice.py 1.3 => 1.4 ===
--- Zope3/src/zope/i18n/tests/test_globaltranslationservice.py:1.3	Tue Mar 25 12:01:53 2003
+++ Zope3/src/zope/i18n/tests/test_globaltranslationservice.py	Tue Mar 25 15:21:28 2003
@@ -37,8 +37,7 @@
         de_catalog = GettextMessageCatalog('de', 'default',
                                            os.path.join(path, 'de-default.mo'))
         alt_en_catalog = GettextMessageCatalog('en', 'alt',
-                                           os.path.join(path,
-                                                        'en-alt.mo'))
+                                               os.path.join(path, 'en-alt.mo'))
         service.addCatalog(alt_en_catalog)
         service.addCatalog(en_catalog)
         service.addCatalog(de_catalog)
@@ -63,23 +62,23 @@
         # Test that at least one of context or target_language is given
         raises(TypeError, translate, 'short_greeting', context=None)
 
-       
+
     def testStringTranslate(self):
         translate = self._service.translate
         self.assertEqual(translate('default', u'short_greeting',
                                    target_language='en'),
                          u'Hello!')
-        
+
     def testMessageIDTranslate(self):
         translate = self._service.translate
         self.assertEqual(translate('default', u'short_greeting',
                                    target_language='en'),
                          u'Hello!')
         msgid = MessageIDFactory('alt')('short_greeting')
-        self.assertEqual(translate('default', msgid, 
+        self.assertEqual(translate('default', msgid,
                                    target_language='en'),
                          u'Hey!')
-       
+
     def testMessageIDTranslate_w_Domain(self):
         translate = self._service.getDomain('default').translate
         self.assertEqual(translate(u'short_greeting', target_language='en'),
@@ -87,8 +86,8 @@
         msgid = MessageIDFactory('alt')('short_greeting')
         self.assertEqual(translate(msgid, target_language='en'),
                          u'Hey!')
-       
-           
+
+
     def testSimpleFallbackTranslation(self):
         translate = self._service.translate
         raises = self.assertRaises