[Zope-Checkins] CVS: Zope3/lib/python/Zope/I18n - Domain.py:1.1.2.2 IDomain.py:1.1.2.2 TranslationService.py:1.1.2.2
Stephan Richter
srichter@cbu.edu
Wed, 5 Jun 2002 17:07:31 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/I18n
In directory cvs.zope.org:/tmp/cvs-serv23992
Modified Files:
Tag: Zope-3x-branch
Domain.py IDomain.py TranslationService.py
Log Message:
Well folks, here it is. The first working cut of the TranslationService.
I dunno whether the ZCML is correct at this point in time, but I want to
wait until Wonderland lands. I still have to finish the Domain and
BrowserLanguage getter. I also plan some sort of internationalized object
converter that will do some monatary, date-time and other local
conversions.
But what does work? The Langauge Negotiater works just fine. The simple
MessageCatalog too. Of course the TranslationService does its thing. I need
to write some more unit tests though.
=== Zope3/lib/python/Zope/I18n/Domain.py 1.1.2.1 => 1.1.2.2 ===
"""
+from Zope.I18n.IDomain import IDomain
+class Domain:
+
+ __implements__ = IDomain
+
+ def __init__(self, place, domain):
+ self._place = place
+ self.domain = domain
+
+
+ def getPlace(self):
+ """Return the place this domain was created for."""
+ return self._place
+
+
+ def getDomainName(self):
+ """Return the domain name"""
+ return self._domain
+
+
+ ############################################################
+ # Implementation methods for interface
+ # IDomain.py
+
+ def translate(self, source, mapping=None, context=None,
+ target_language=None):
+ 'See Zope.I18n.IDomain.IDomain'
+
+ # lookup the correct translation service
+ service_manager = getServiceManager(self.place)
+ service = service_manager.getBoundService('Translation Service')
+
+ return service.translate(self.domain, source, mapping, context,
+ target_language)
+
+ #
+ ############################################################
=== Zope3/lib/python/Zope/I18n/IDomain.py 1.1.2.1 => 1.1.2.2 ===
domain -- Secifies the domain to look up for the translation. See
- ITranslationService for more details on domains."""
+ ITranslationService for more details on domains.
+ """
def translate(source, mapping=None, context=None, target_language=None):
=== Zope3/lib/python/Zope/I18n/TranslationService.py 1.1.2.1 => 1.1.2.2 ===
from Zope.App.OFS.Container.BTreeContainer import BTreeContainer
+from Zope.App.OFS.Container.IContainer import IContainer
+from Negotiator import negotiator
from IMessageCatalog import IMessageCatalog
from ITranslationService import ITranslationService
-from Zope.App.OFS.Container.IContainer import IContainer
+# Setting up some regular expressions for finding interpolation variables in
+# the text.
NAME_RE = r"[a-zA-Z][a-zA-Z0-9_]*"
_interp_regex = re.compile(r'(?<!\$)(\$(?:%(n)s|{%(n)s}))' %({'n': NAME_RE}))
_get_var_regex = re.compile(r'%(n)s' %({'n': NAME_RE}))
@@ -111,11 +114,20 @@
if context is None:
raise TypeError, 'No destination language'
else:
- target_language = context['language']
+ avail_langs = self.getAvailableLanguages(domain)
+ target_language = negotiator.getLanguage(avail_langs, context)
# Get the translation. Default is the source text itself.
- text = self.__data.get((target_language, domain), {}
- ).get(source, source)
+ catalog_names = self._catalogs.get((target_language, domain), {})
+
+ text = source
+ for name in catalog_names:
+ catalog = self.__data[name]
+ try:
+ text = catalog.getMessage(source)
+ break
+ except:
+ pass
# Now we need to do the interpolation
return self.interpolate(text, mapping)
@@ -134,3 +146,11 @@
text = text.replace(string, mapping.get(var))
return text
+
+
+ def getAvailableLanguages(self, domain):
+ """Find all the languages that are available for this domain"""
+ identifiers = self._catalogs.keys()
+ identifiers = filter(lambda x, d=domain: x[1] == domain, identifiers)
+ languages = map(lambda x: x[0], identifiers)
+ return languages