vendor/Five/1.2b-r20590

view i18n.py @ 0:3673ed425f80

Vendor import of Five 1.2b+ (r20590)
author fguillaume
date Fri, 02 Dec 2005 20:25:42 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2005 Zope Corporation and Contributors.
4 # All Rights Reserved.
5 #
6 # This software is subject to the provisions of the Zope Public License,
7 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11 # FOR A PARTICULAR PURPOSE.
12 #
13 ##############################################################################
14 """Mimick Zope3 i18n machinery for Zope 2
16 $Id: i18n.py 19274 2005-10-31 17:15:42Z philikon $
17 """
18 from Acquisition import aq_acquire
19 from zope.interface import implements
20 from zope.i18n import interpolate
21 from zope.i18n.interfaces import ITranslationDomain, IUserPreferredLanguages
22 from zope.i18nmessageid import MessageID
23 from zope.app import zapi
24 from zope.publisher.browser import BrowserLanguages
26 from Products.PageTemplates import GlobalTranslationService as GTS
28 class FiveTranslationService:
29 """Translation service that delegates to ``zope.i18n`` machinery.
30 """
31 # this is mostly a copy of zope.i18n.translate, with modifications
32 # regarding fallback and Zope 2 compatability
33 def translate(self, domain, msgid, mapping=None,
34 context=None, target_language=None, default=None):
35 if isinstance(msgid, MessageID):
36 domain = msgid.domain
37 default = msgid.default
38 mapping = msgid.mapping
40 util = zapi.queryUtility(ITranslationDomain, domain)
42 if util is None:
43 # fallback to translation service that was registered,
44 # DummyTranslationService the worst
45 ts = _fallback_translation_service
46 return ts.translate(domain, msgid, mapping=mapping, context=context,
47 target_language=target_language, default=default)
49 # in Zope3, context is adapted to IUserPreferredLanguages,
50 # which means context should be the request in this case.
51 if context is not None:
52 context = aq_acquire(context, 'REQUEST', None)
53 return util.translate(msgid, mapping=mapping, context=context,
54 target_language=target_language, default=default)
56 class LocalizerLanguages(object):
57 """Languages adapter that chooses languages according to Localizer
58 settings."""
59 implements(IUserPreferredLanguages)
61 def __init__(self, context):
62 self.context = context
64 def getPreferredLanguages(self):
65 accept_language = self.context.AcceptLanguage
66 langs = []
67 for lang, node in accept_language.children.items():
68 langs.append((node.get_quality(), lang))
69 langs.extend([(n.get_quality(), l) for l, n
70 in node.children.items()])
71 langs.sort()
72 langs.reverse()
73 langs = [l for q, l in langs]
74 if '' in langs:
75 langs.remove('')
76 return langs
78 class PTSLanguages(object):
79 """Languages adapter that chooses languages like
80 PlacelessTranslationService."""
81 implements(IUserPreferredLanguages)
83 def __init__(self, context):
84 self.context = context
86 def getPreferredLanguages(self):
87 from Products.PlacelessTranslationService.Negotiator import getLangPrefs
88 return getLangPrefs(self.context)
90 # these are needed for the monkey
91 _fallback_translation_service = GTS.DummyTranslationService()
92 fiveTranslationService = FiveTranslationService()
94 def getGlobalTranslationService():
95 return fiveTranslationService
97 def setGlobalTranslationService(newservice):
98 global _fallback_translation_service
99 oldservice, _fallback_translation_service = \
100 _fallback_translation_service, newservice
101 return oldservice
103 def monkey():
104 # get the services that has been registered so far and plug in our
105 # new one
106 global _fallback_translation_service
107 _fallback_translation_service = \
108 GTS.setGlobalTranslationService(fiveTranslationService)
110 # now override the getter/setter so that noone else can mangle
111 # with it anymore
112 GTS.getGlobalTranslationService = getGlobalTranslationService
113 GTS.setGlobalTranslationService = setGlobalTranslationService