> However, I dont need to do this with PTS - i would be happy to know the 
> five solution !
I'm using sx.translations + five in order to achive this 
programatically. In order to use i18n from five, read this:


Then install sx.translations from here:

Please note that the product self do not compile po files into mo you 
have to compile it either manually or apply my patch:


I did also some functions to be able to translate messages 
programatically. Please note that I'm using zope 2.8, so, I don't know 
if it will work with 2.10.x. Please also note that I'm using a request
variable called 'lang' to set the source language. If this variable
isn't set, then the browser language will be assumed.

Best regards


from zope.component import getUtility
from zope.i18n.interfaces import ILanguageAvailability
from zope.i18n.interfaces import INegotiator
from zope.i18nmessageid.messageid import MessageID
from zope.i18n import translate as _

def getSupportedLanguages(domain=None):
   """Gets the supported languages for a Message catalog"""
   options = getUtility(ILanguageAvailability,
   langCodes = []
   for option in options:

   return langCodes

def getLanguage(context,domain=None,optionsDict = {}):
   """Gets the current language of the user browser"""
   context = getattr(context,'REQUEST',context)
   lang = optionsDict.get('lang',None)
   if lang not in [None,'']:

   supportedLanguages = getSupportedLanguages(domain)
   negotiator = getUtility(INegotiator)
   currentLang = negotiator.getLanguage(supportedLanguages,context)
   if currentLang in ['', None]:
     #We failed to get the browser's language, so, the default will be 
     currentLang = DEFAULT_LANGUAGE

   return currentLang

def useTranslationService(context, msgid, domain = None, mapping = None, 
lang = None,
                           default = None):
   """Uses the TranslationService from python"""
   context = getattr(context,'REQUEST',context)

   isMessageId = (type(msgid) == MessageID)
   if lang is None:
     if isMessageId:
       domain = msgid.domain
     lang = getLanguage(context,domain)

   if default is None:
     default = msgid

   if isMessageId:
     #A MessageId object already has everything on it: domain, mapping, 
and a
     #default value, so, we don't need to pass them here
     translation = _(msgid, target_language = lang, context = context)
     translation = _(msgid,
                     domain = domain,
                     mapping = mapping,
                     target_language = lang,
                     default = default,
                     context = context)
   return translation
Zope maillist  -  Zope@zope.org
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-dev )

Reply via email to