Пример #1
0
    def setUp(self):
        # set up i18n component
        from zope.i18n import translate
        from zope.i18n.interfaces import INegotiator
        from zope.i18n.interfaces import ITranslationDomain
        from zope.i18n.negotiator import Negotiator
        from zope.i18n.simpletranslationdomain import SimpleTranslationDomain
        from zope.i18n.tests.test_negotiator import Env
        from zope.tales.tales import Context

        self.env = Env(('klingon', 'da', 'en', 'fr', 'no'))

        class ZopeI18NContext(Context):
            def translate(self,
                          msgid,
                          domain=None,
                          context=None,
                          mapping=None,
                          default=None):
                context = self.vars['options']['env']
                return translate(msgid,
                                 domain,
                                 mapping,
                                 context=context,
                                 default=default)

        def _getContext(self, contexts=None, **kwcontexts):
            if contexts is not None:
                if kwcontexts:
                    kwcontexts.update(contexts)
                else:
                    kwcontexts = contexts
            return ZopeI18NContext(self, kwcontexts)

        def _pt_getEngineContext(namespace):
            self = namespace['template']
            engine = self.pt_getEngine()
            return _getContext(engine, namespace)

        import zope.component
        zope.component.provideUtility(Negotiator(), INegotiator)
        catalog = SimpleTranslationDomain('domain')
        zope.component.provideUtility(catalog, ITranslationDomain, 'domain')
        self.files = os.path.abspath(os.path.join(__file__, '..', 'input'))
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)
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 setUp(self):
     super(NegotiatorTest, self).setUp()
     self.negotiator = Negotiator()
Пример #5
0
        _language = provider()
        log.debug("Looking for language in %s found %s", name, _language)
        if _language and (_language in capi.zope_i18n_allowed_languages):
            default_language = _language
            log.debug("Got default language as %s from provider %s", _language,
                      name)
            break
    return default_language


class Negotiator(Negotiator):
    def getLanguage(self, langs, env):
        return get_default_language()


i18n_negotiator = Negotiator()


def get_base_direction():
    request = get_request()
    ui_lang = request.getCookies().get("I18N_LANGUAGE")
    if ui_lang is not None:
        language = ui_lang
    else:
        language = capi.default_language
    if language[:2] in capi.right_to_left_languages:
        return "rtl"
    else:
        return "ltr"

 def setUp(self):
     super(NegotiatorTest, self).setUp()
     self.negotiator = Negotiator()