From dd80a2605375ed3358aead4f252b2a153d03c1fe Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Thu, 12 Aug 2004 19:56:31 +0000 Subject: Backported r26559 | srichter | 2004-07-15 17:22:32 -0400 (Thu, 15 Jul 2004) | 2 lines r26560 | srichter | 2004-07-15 17:38:42 -0400 (Thu, 15 Jul 2004) | 2 lines --- gettextmessagecatalog.py | 62 +++++++++++++++++++++++++++ locales/xmlfactory.py | 2 +- negotiator.py | 2 +- simpletranslationdomain.py | 2 +- tests/test_formats.py | 4 +- tests/test_imessagecatalog.py | 2 +- tests/test_itranslationdomain.py | 2 +- tests/test_negotiator.py | 63 ++++++++++++++++++++++++++++ tests/testi18nawareobject.py | 90 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 222 insertions(+), 7 deletions(-) create mode 100644 gettextmessagecatalog.py create mode 100644 tests/test_negotiator.py create mode 100644 tests/testi18nawareobject.py diff --git a/gettextmessagecatalog.py b/gettextmessagecatalog.py new file mode 100644 index 0000000..36e734f --- /dev/null +++ b/gettextmessagecatalog.py @@ -0,0 +1,62 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (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. +# +############################################################################## +"""A simple implementation of a Message Catalog. + +$Id$ +""" +from gettext import GNUTranslations +from zope.i18n.interfaces import IGlobalMessageCatalog +from zope.interface import implements + + +class _KeyErrorRaisingFallback(object): + def ugettext(self, message): + raise KeyError, message + + +class GettextMessageCatalog(object): + """A message catalog based on GNU gettext and Python's gettext module.""" + + implements(IGlobalMessageCatalog) + + def __init__(self, language, domain, path_to_file): + """Initialize the message catalog""" + self.language = language + self.domain = domain + self._path_to_file = path_to_file + self.reload() + self._catalog.add_fallback(_KeyErrorRaisingFallback()) + + def reload(self): + 'See IMessageCatalog' + fp = open(self._path_to_file, 'rb') + try: + self._catalog = GNUTranslations(fp) + finally: + fp.close() + + def getMessage(self, id): + 'See IMessageCatalog' + return self._catalog.ugettext(id) + + def queryMessage(self, id, default=None): + 'See IMessageCatalog' + try: + return self._catalog.ugettext(id) + except KeyError: + return default + + def getIdentifier(self): + 'See IMessageCatalog' + return self._path_to_file diff --git a/locales/xmlfactory.py b/locales/xmlfactory.py index 886a6a5..3841efc 100644 --- a/locales/xmlfactory.py +++ b/locales/xmlfactory.py @@ -23,7 +23,7 @@ from zope.i18n.locales import LocaleCalendar, LocaleCurrency, LocaleNumbers from zope.i18n.locales import LocaleFormat, LocaleFormatLength, dayMapping from zope.i18n.locales.inheritance import InheritingDictionary -class LocaleFactory: +class LocaleFactory(object): """This class creates a Locale object from an ICU XML file.""" def __init__(self, path): diff --git a/negotiator.py b/negotiator.py index e44a2c8..5aa4fd7 100644 --- a/negotiator.py +++ b/negotiator.py @@ -33,7 +33,7 @@ def normalize_langs(langs): n_langs[normalize_lang(l)] = l return n_langs -class Negotiator: +class Negotiator(object): implements(INegotiator) def getLanguage(self, langs, env): diff --git a/simpletranslationdomain.py b/simpletranslationdomain.py index acf2961..ffb6711 100644 --- a/simpletranslationdomain.py +++ b/simpletranslationdomain.py @@ -20,7 +20,7 @@ from zope.component import getUtility from zope.i18n.interfaces import ITranslationDomain, INegotiator from zope.i18n import interpolate -class SimpleTranslationDomain: +class SimpleTranslationDomain(object): """This is the simplest implementation of the ITranslationDomain I could come up with. diff --git a/tests/test_formats.py b/tests/test_formats.py index 167950f..387e3f3 100644 --- a/tests/test_formats.py +++ b/tests/test_formats.py @@ -28,10 +28,10 @@ from zope.i18n.interfaces import INumberFormat from zope.i18n.format import NumberFormat from zope.i18n.format import parseNumberPattern -class LocaleStub: +class LocaleStub(object): pass -class LocaleCalendarStub: +class LocaleCalendarStub(object): type = u'gregorian' diff --git a/tests/test_imessagecatalog.py b/tests/test_imessagecatalog.py index 37ad3c2..0646895 100644 --- a/tests/test_imessagecatalog.py +++ b/tests/test_imessagecatalog.py @@ -20,7 +20,7 @@ from zope.interface.verify import verifyObject from zope.i18n.interfaces import IMessageCatalog -class TestIMessageCatalog: +class TestIMessageCatalog(object): # This should be overwritten by every class that inherits this test diff --git a/tests/test_itranslationdomain.py b/tests/test_itranslationdomain.py index 9d2ac25..0299417 100644 --- a/tests/test_itranslationdomain.py +++ b/tests/test_itranslationdomain.py @@ -27,7 +27,7 @@ from zope.i18n.negotiator import negotiator from zope.i18n.interfaces import INegotiator, IUserPreferredLanguages from zope.i18n.interfaces import ITranslationDomain -class Environment: +class Environment(object): implements(IUserPreferredLanguages) diff --git a/tests/test_negotiator.py b/tests/test_negotiator.py new file mode 100644 index 0000000..a14938f --- /dev/null +++ b/tests/test_negotiator.py @@ -0,0 +1,63 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (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. +# +############################################################################## +"""Language Negotiator tests. + +$Id$ +""" +import unittest + +from zope.i18n.negotiator import Negotiator +from zope.i18n.interfaces import IUserPreferredLanguages +from zope.component.tests.placelesssetup import PlacelessSetup +from zope.interface import implements + +class Env(object): + implements(IUserPreferredLanguages) + + def __init__(self, langs=()): + self.langs = langs + + def getPreferredLanguages(self): + return self.langs + + +class NegotiatorTest(PlacelessSetup, unittest.TestCase): + + def setUp(self): + super(NegotiatorTest, self).setUp() + self.negotiator = Negotiator() + + def test_findLanguages(self): + + _cases = ( + (('en','de'), ('en','de','fr'), 'en'), + (('en'), ('it','de','fr'), None), + (('pt-br','de'), ('pt_BR','de','fr'), 'pt_BR'), + (('pt-br','en'), ('pt', 'en', 'fr'), 'pt'), + (('pt-br','en-us', 'de'), ('de', 'en', 'fr'), 'en'), + ) + + for user_pref_langs, obj_langs, expected in _cases: + env = Env(user_pref_langs) + self.assertEqual(self.negotiator.getLanguage(obj_langs, env), + expected) + + +def test_suite(): + return unittest.TestSuite(( + unittest.makeSuite(NegotiatorTest), + )) + +if __name__ == '__main__': + unittest.main(defaultTest='test_suite') diff --git a/tests/testi18nawareobject.py b/tests/testi18nawareobject.py new file mode 100644 index 0000000..8efeeea --- /dev/null +++ b/tests/testi18nawareobject.py @@ -0,0 +1,90 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (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 is a test for the II18nAware interface. + +$Id$ +""" +import unittest + +from zope.i18n.interfaces import II18nAware +from zope.i18n.tests.testii18naware import TestII18nAware +from zope.interface import implements + + +class I18nAwareContentObject(object): + + implements(II18nAware) + + def __init__(self): + self.content = {} + self.defaultLanguage = 'en' + + def getContent(self, language): + return self.content[language] + + def queryContent(self, language, default=None): + return self.content.get(language, default) + + def setContent(self, content, language): + self.content[language] = content + + ############################################################ + # Implementation methods for interface + # II18nAware.py + + def getDefaultLanguage(self): + 'See II18nAware' + return self.defaultLanguage + + def setDefaultLanguage(self, language): + 'See II18nAware' + self.defaultLanguage = language + + def getAvailableLanguages(self): + 'See II18nAware' + return self.content.keys() + + # + ############################################################ + + +class TestI18nAwareObject(TestII18nAware): + + def _createObject(self): + object = I18nAwareContentObject() + object.setContent('English', 'en') + object.setContent('Lithuanian', 'lt') + object.setContent('French', 'fr') + return object + + def testSetContent(self): + self.object.setContent('German', 'de') + self.assertEqual(self.object.content['de'], 'German') + + def testGetContent(self): + self.assertEqual(self.object.getContent('en'), 'English') + self.assertRaises(KeyError, self.object.getContent, 'es') + + def testQueryContent(self): + self.assertEqual(self.object.queryContent('en'), 'English') + self.assertEqual(self.object.queryContent('es', 'N/A'), 'N/A') + + +def test_suite(): + loader = unittest.TestLoader() + return loader.loadTestsFromTestCase(TestI18nAwareObject) + + +if __name__ == '__main__': + unittest.TextTestRunner().run(test_suite()) -- cgit v1.2.1