def setUp(self): setup.placefulSetUp() self.rootFolder = setup.buildSampleFolderTree() gsm = getGlobalSiteManager() de_catalog = MessageCatalog("de", "default") de_catalog.setMessage("short_greeting", "Hallo!", 10) de_catalog.setMessage("long_greeting", "Guten Tag!", 10) # register global translation domain and add the catalog. domain = GlobalTranslationDomain("default") domain.addCatalog(de_catalog) gsm.registerUtility(domain, ITranslationDomain, "default") # create a local site manager and add a local translation domain td = TranslationDomain() td.domain = "default" de_catalog = MessageCatalog("de", "default") de_catalog.setMessage("short_greeting", "Hallo Welt!", 10) td["de-default-1"] = de_catalog mgr = setup.createSiteManager(traverse(self.rootFolder, "folder1")) setup.addUtility(mgr, "default", interfaces.ILocalTranslationDomain, td) self.trans1 = td self.trans = domain
def setUp(self): psetup = PlacefulSetup() self.sm = psetup.setUp(True, True) self.rootFolder = psetup.rootFolder setUpSiteManagerLookup() import zope.i18n.interfaces import zope.i18n.negotiator provideUtility(zope.i18n.negotiator.negotiator, zope.i18n.interfaces.INegotiator) gsm = getGlobalSiteManager() de_catalog = MessageCatalog('de', 'default') de_catalog.setMessage('short_greeting', 'Hallo!', 10) de_catalog.setMessage('long_greeting', 'Guten Tag!', 10) # register global translation domain and add the catalog. domain = GlobalTranslationDomain('default') domain.addCatalog(de_catalog) gsm.registerUtility(domain, ITranslationDomain, 'default') # create a local site manager and add a local translation domain td = TranslationDomain() td.domain = 'default' de_catalog = MessageCatalog('de', 'default') de_catalog.setMessage('short_greeting', 'Hallo Welt!', 10) td['de-default-1'] = de_catalog mgr = createSiteManager(traverse(self.rootFolder, 'folder1')) mgr['default']['default'] = td mgr.registerUtility(mgr['default']['default'], ITranslationDomain, 'default') self.trans1 = td self.trans = domain
def _getTranslationDomain(self): domain = TranslationDomain('default') en_catalog = GettextMessageCatalog('en', 'default', en_file) de_catalog = GettextMessageCatalog('de', 'default', de_file) domain.addCatalog(en_catalog) domain.addCatalog(de_catalog) return domain
def testMessageIDRecursiveTranslate(self): factory = MessageFactory('default') translate = self._domain.translate msgid_sub1 = factory(u'44-not-there', '${blue}', mapping={'blue': 'BLUE'}) msgid_sub2 = factory(u'45-not-there', '${yellow}', mapping={'yellow': 'YELLOW'}) mapping = {'color1': msgid_sub1, 'color2': msgid_sub2} msgid = factory(u'46-not-there', 'Color: ${color1}/${color2}', mapping=mapping) self.assertEqual( translate(msgid, target_language='en', default="default"), "Color: BLUE/YELLOW") # The recursive translation must not change the mappings self.assertEqual(msgid.mapping, { 'color1': msgid_sub1, 'color2': msgid_sub2 }) # A circular reference should not lead to crashes msgid1 = factory(u'47-not-there', 'Message 1 and $msg2', mapping={}) msgid2 = factory(u'48-not-there', 'Message 2 and $msg1', mapping={}) msgid1.mapping['msg2'] = msgid2 msgid2.mapping['msg1'] = msgid1 self.assertRaises(ValueError, translate, msgid1, None, None, 'en', "default") # Recusrive translations also work if the original message id wasn't a # message id but a unicode with a directly passed mapping self.assertEqual( "Color: BLUE/YELLOW", translate(u'Color: ${color1}/${color2}', mapping=mapping, target_language='en')) # If we have mapping with a message id from a different domain, make sure # we use that domain, not ours. If the message domain is not registered yet, # we should return a defualt translation. alt_factory = MessageFactory('alt') msgid_sub = alt_factory(u'special', default=u'oohhh') mapping = {'message': msgid_sub} msgid = factory(u'46-not-there', 'Message: ${message}', mapping=mapping) # test we get a default with no domain registerd self.assertEqual( translate(msgid, target_language='en', default="default"), "Message: oohhh") # provide the domain domain = TranslationDomain('alt') path = testdir() en_catalog = GettextMessageCatalog('en', 'alt', os.path.join(path, 'en-alt.mo')) domain.addCatalog(en_catalog) # test that we get the right translation zope.component.provideUtility(domain, ITranslationDomain, 'alt') self.assertEqual( translate(msgid, target_language='en', default="default"), "Message: Wow")
def createTranslations(portal,canon,domain='plumi'): """Creates translations of 'canon' into all available languages in 'domain'""" parent = canon.getParentNode() wftool = getToolByName(portal,'portal_workflow') transDomain = TranslationDomain(domain) # transDomain = getUtility(ITranslationDomain, domain) availLanguages = transDomain.getCatalogsInfo() langs = [] for lang in availLanguages.keys(): if str(lang) != 'test': langs.append(str(lang)) for lang in langs: transId = '%s-%s' % (canon.id, lang) transTitle = transDomain.translate(canon.title, target_language=lang) transDesc = transDomain.translate(canon.description, target_language=lang) if not hasattr(parent, transId): if parent != portal and parent.hasTranslation(lang): #if parent folder has a translation, put the clone in that translation = parent.getTranslation(lang).manage_clone(canon, transId) else: translation = parent.manage_clone(canon, transId) translation.setTitle(transTitle) translation.setDescription(transDesc) translation.setLanguage(lang) translation.addTranslationReference(canon) wftool.doActionFor(translation,action='publish')
def _getTranslationDomain(self, locale, variant="default"): path = os.path.dirname(tests.__file__) self._path = os.path.join(path, '%s-%s.mo' % (locale, variant)) catalog = GettextMessageCatalog(locale, variant, self._path) domain = TranslationDomain('default') domain.addCatalog(catalog) return domain
def registerTranslations(_context, directory): path = os.path.normpath(directory) domains = {} # Gettext has the domain-specific catalogs inside the language directory, # which is exactly the opposite as we need it. So create a dictionary that # reverses the nesting. for language in os.listdir(path): lc_messages_path = os.path.join(path, language, 'LC_MESSAGES') if os.path.isdir(lc_messages_path): for domain_file in os.listdir(lc_messages_path): if domain_file.endswith('.mo'): domain_path = os.path.join(lc_messages_path, domain_file) domain = domain_file[:-3] if not domain in domains: domains[domain] = {} domains[domain][language] = domain_path # Now create TranslationDomain objects and add them as utilities for name, langs in domains.items(): domain = TranslationDomain(name) for lang, file in langs.items(): domain.addCatalog(GettextMessageCatalog(lang, name, file)) # make sure we have a TEST catalog for each domain: domain.addCatalog(TestMessageCatalog(name)) utility(_context, ITranslationDomain, domain, name=name)
def setUp(self): setup.placefulSetUp() self.rootFolder = setup.buildSampleFolderTree() gsm = getGlobalSiteManager() de_catalog = MessageCatalog('de', 'default') de_catalog.setMessage('short_greeting', 'Hallo!', 10) de_catalog.setMessage('long_greeting', 'Guten Tag!', 10) # register global translation domain and add the catalog. domain = GlobalTranslationDomain('default') domain.addCatalog(de_catalog) gsm.registerUtility(domain, ITranslationDomain, 'default') # create a local site manager and add a local translation domain td = TranslationDomain() td.domain = 'default' de_catalog = MessageCatalog('de', 'default') de_catalog.setMessage('short_greeting', 'Hallo Welt!', 10) td['de-default-1'] = de_catalog mgr = setup.createSiteManager(traverse(self.rootFolder, 'folder1')) setup.addUtility( mgr, 'default', interfaces.ILocalTranslationDomain, td) self.trans1 = td self.trans = domain
def _getTranslationDomain(self): domain = TranslationDomain("default") path = testdir() en_catalog = GettextMessageCatalog("en", "default", os.path.join(path, "en-default.mo")) de_catalog = GettextMessageCatalog("de", "default", os.path.join(path, "de-default.mo")) domain.addCatalog(en_catalog) domain.addCatalog(de_catalog) return domain
def testMessageIDRecursiveTranslate(self): factory = MessageFactory('default') translate = self._domain.translate msgid_sub1 = factory(_u("44-not-there"), '${blue}', mapping = {'blue': 'BLUE'}) msgid_sub2 = factory(_u("45-not-there"), '${yellow}', mapping = {'yellow': 'YELLOW'}) mapping = {'color1': msgid_sub1, 'color2': msgid_sub2} msgid = factory(_u("46-not-there"), 'Color: ${color1}/${color2}', mapping=mapping) self.assertEqual( translate(msgid, target_language='en', default="default"), "Color: BLUE/YELLOW") # The recursive translation must not change the mappings self.assertEqual(msgid.mapping, {'color1': msgid_sub1, 'color2': msgid_sub2}) # A circular reference should not lead to crashes msgid1 = factory(_u("47-not-there"), 'Message 1 and $msg2', mapping = {}) msgid2 = factory(_u("48-not-there"), 'Message 2 and $msg1', mapping = {}) msgid1.mapping['msg2'] = msgid2 msgid2.mapping['msg1'] = msgid1 self.assertRaises(ValueError, translate, msgid1, None, None, 'en',"default") # Recusrive translations also work if the original message id wasn't a # message id but a unicode with a directly passed mapping self.assertEqual("Color: BLUE/YELLOW", translate(_u("Color: ${color1}/${color2}"), mapping=mapping, target_language='en')) # If we have mapping with a message id from a different domain, make sure # we use that domain, not ours. If the message domain is not registered yet, # we should return a defualt translation. alt_factory = MessageFactory('alt') msgid_sub = alt_factory(_u("special"), default=_u("oohhh")) mapping = {'message': msgid_sub} msgid = factory(_u("46-not-there"), 'Message: ${message}', mapping=mapping) # test we get a default with no domain registerd self.assertEqual( translate(msgid, target_language='en', default="default"), "Message: oohhh") # provide the domain domain = TranslationDomain('alt') path = testdir() en_catalog = GettextMessageCatalog('en', 'alt', os.path.join(path, 'en-alt.mo')) domain.addCatalog(en_catalog) # test that we get the right translation zope.component.provideUtility(domain, ITranslationDomain, 'alt') self.assertEqual( translate(msgid, target_language='en', default="default"), "Message: Wow")
def _getTranslationDomain(self): domain = TranslationDomain('default') path = testdir() en_catalog = GettextMessageCatalog('en', 'default', os.path.join(path, 'en-default.mo')) de_catalog = GettextMessageCatalog('de', 'default', os.path.join(path, 'de-default.mo')) domain.addCatalog(en_catalog) domain.addCatalog(de_catalog) return domain
def testMessageIDTranslateForDifferentDomain(self): domain = TranslationDomain("alt") path = testdir() en_catalog = GettextMessageCatalog("en", "alt", os.path.join(path, "en-alt.mo")) domain.addCatalog(en_catalog) zope.component.provideUtility(domain, ITranslationDomain, "alt") factory = MessageFactory("alt") msgid = factory(u"special", "default") self.assertEqual(self._domain.translate(msgid, target_language="en"), u"Wow")
def handler(catalogs, name): """ special handler handling the merging of two message catalogs """ gsm = getSiteManager() # Try to get an existing domain and add the given catalogs to it domain = queryUtility(ITranslationDomain, name) if domain is None: domain = TranslationDomain(name) gsm.registerUtility(domain, ITranslationDomain, name=name) for catalog in catalogs: domain.addCatalog(catalog) # make sure we have a TEST catalog for each domain: domain.addCatalog(TestMessageCatalog(name))
def testMessageIDTranslateForDifferentDomain(self): domain = TranslationDomain('other') path = testdir() en_catalog = GettextMessageCatalog('en', 'other', os.path.join(path, 'en-default.mo')) domain.addCatalog(en_catalog) zope.component.provideUtility(domain, ITranslationDomain, 'other') factory = MessageIDFactory('other') msgid = factory(u'short_greeting', 'default') self.assertEqual( self._domain.translate(msgid, target_language='en'), u'Hello!')
def testMessageIDTranslateForDifferentDomain(self): domain = TranslationDomain('alt') path = testdir() en_catalog = GettextMessageCatalog('en', 'alt', os.path.join(path, 'en-alt.mo')) domain.addCatalog(en_catalog) zope.component.provideUtility(domain, ITranslationDomain, 'alt') factory = MessageFactory('alt') msgid = factory(u'special', 'default') self.assertEqual(self._domain.translate(msgid, target_language='en'), u'Wow')
def testMessageIDTranslateForDifferentDomain(self): domain = TranslationDomain('alt') path = testdir() en_catalog = GettextMessageCatalog('en', 'alt', os.path.join(path, 'en-alt.mo')) domain.addCatalog(en_catalog) zope.component.provideUtility(domain, ITranslationDomain, 'alt') factory = MessageFactory('alt') msgid = factory(_u("special"), 'default') self.assertEqual( self._domain.translate(msgid, target_language='en'), _u("Wow"))
def _register_catalog_file(name, msgpath, lang, domain, update=False): """Registers a catalog file as an ITranslationDomain.""" if not _checkLanguage(lang): return mofile = join(msgpath, name[:-2] + 'mo') result = _updateMoFile(name, msgpath, lang, domain, mofile) if result or update: # Newly created file or one from a i18n folder, # the Z3 domain utility does not exist if queryUtility(ITranslationDomain, name=domain) is None: ts_domain = TranslationDomain(domain) sm = getGlobalSiteManager() sm.registerUtility(ts_domain, ITranslationDomain, name=domain) util = queryUtility(ITranslationDomain, name=domain) if util is not None and os.path.exists(mofile): if PTS_LANGUAGES is not None: # If we have restricted the available languages, # use the speed and not memory optimized version cat = GettextMessageCatalog(lang, domain, mofile) else: # Otherwise optimize for memory footprint cat = LazyGettextMessageCatalog(lang, domain, mofile) # Add message catalog util.addCatalog(cat)
def _setup_i18n(): global DOMAIN_SETUP if DOMAIN_SETUP: return domain = TranslationDomain(I18N_DOMAIN) for catalog in os.listdir(MO_PATH): catalog_path = os.path.join(MO_PATH, catalog) mo_path = os.path.join(catalog_path, 'LC_MESSAGES', I18N_DOMAIN + '.mo') if not os.path.exists(mo_path): continue domain.addCatalog(UTF8GettextMessageCatalog( catalog, I18N_DOMAIN, mo_path)) component.provideUtility(domain, ITranslationDomain, name='cc_org') DOMAIN_SETUP = True
def translate(self, msgid, mapping=None, context=None, target_language=None, default=None): if msgid == "project_noun": noun = TranslationDomain.translate(self, msgid, mapping, context, target_language, default) return noun mapping = mapping or {} # defer import to circumvent loops from opencore.project.utils import project_noun mapping["project_noun"] = project_noun() if isinstance(msgid, zope.i18nmessageid.Message) and msgid.mapping: combined_mapping = dict(mapping) combined_mapping.update(msgid.mapping) msgid = zope.i18nmessageid.Message(msgid, mapping=combined_mapping) return TranslationDomain.translate(self, msgid, mapping, context, target_language, default)
def _setup_i18n(): global DOMAIN_SETUP if DOMAIN_SETUP: return domain = TranslationDomain(I18N_DOMAIN) for catalog in os.listdir(MO_PATH): catalog_path = os.path.join(MO_PATH, catalog) mo_path = os.path.join(catalog_path, 'LC_MESSAGES', I18N_DOMAIN + '.mo') if not os.path.exists(mo_path): continue domain.addCatalog( UTF8GettextMessageCatalog(catalog, I18N_DOMAIN, mo_path)) component.provideUtility(domain, ITranslationDomain, name='cc_org') DOMAIN_SETUP = True
def testMessageIDRecursiveTranslate(self): factory = MessageFactory("default") translate = self._domain.translate msgid_sub1 = factory(u"44-not-there", "${blue}", mapping={"blue": "BLUE"}) msgid_sub2 = factory(u"45-not-there", "${yellow}", mapping={"yellow": "YELLOW"}) mapping = {"color1": msgid_sub1, "color2": msgid_sub2} msgid = factory(u"46-not-there", "Color: ${color1}/${color2}", mapping=mapping) self.assertEqual(translate(msgid, target_language="en", default="default"), "Color: BLUE/YELLOW") # The recursive translation must not change the mappings self.assertEqual(msgid.mapping, {"color1": msgid_sub1, "color2": msgid_sub2}) # A circular reference should not lead to crashes msgid1 = factory(u"47-not-there", "Message 1 and $msg2", mapping={}) msgid2 = factory(u"48-not-there", "Message 2 and $msg1", mapping={}) msgid1.mapping["msg2"] = msgid2 msgid2.mapping["msg1"] = msgid1 self.assertRaises(ValueError, translate, msgid1, None, None, "en", "default") # Recusrive translations also work if the original message id wasn't a # message id but a unicode with a directly passed mapping self.assertEqual( "Color: BLUE/YELLOW", translate(u"Color: ${color1}/${color2}", mapping=mapping, target_language="en") ) # If we have mapping with a message id from a different domain, make sure # we use that domain, not ours. If the message domain is not registered yet, # we should return a defualt translation. alt_factory = MessageFactory("alt") msgid_sub = alt_factory(u"special", default=u"oohhh") mapping = {"message": msgid_sub} msgid = factory(u"46-not-there", "Message: ${message}", mapping=mapping) # test we get a default with no domain registerd self.assertEqual(translate(msgid, target_language="en", default="default"), "Message: oohhh") # provide the domain domain = TranslationDomain("alt") path = testdir() en_catalog = GettextMessageCatalog("en", "alt", os.path.join(path, "en-alt.mo")) domain.addCatalog(en_catalog) # test that we get the right translation zope.component.provideUtility(domain, ITranslationDomain, "alt") self.assertEqual(translate(msgid, target_language="en", default="default"), "Message: Wow")
def setUp(self): setup.placefulSetUp() self.rootFolder = setup.buildSampleFolderTree() sm = zapi.getGlobalSiteManager() de_catalog = MessageCatalog("de", "default") de_catalog.setMessage("short_greeting", "Hallo!", 10) # Create global translation domain and add the catalog. domain = GlobalTranslationDomain("default") domain.addCatalog(de_catalog) sm.provideUtility(ITranslationDomain, domain, "default") # Create Domain in root folder mgr = setup.createSiteManager(self.rootFolder) self.trans = setup.addDomain(mgr, Translation, TranslationDomain()) # Create Domain in folder1 mgr = setup.createSiteManager(zapi.traverse(self.rootFolder, "folder1")) td = TranslationDomain() td.domain = "default" de_catalog = MessageCatalog("de", "default") de_catalog.setMessage("short_greeting", "Hallo Welt!", 10) td["de-default-1"] = de_catalog self.trans1 = setup.addDomain(mgr, Translation, ts)
available_languages = [] for lang in os.listdir(basepath): if lang.endswith('.pot'): continue langpath = os.path.join(basepath, lang, 'LC_MESSAGES') available_languages.append(lang) for file in os.listdir(langpath): domainpath = os.path.abspath(os.path.join(langpath, file)) if not file.endswith('.mo'): continue domainname = file[:-3] domain = gsm.queryUtility(ITranslationDomain, domainname) if domain is None: domain = TranslationDomain(domainname) gsm.registerUtility(domain, ITranslationDomain, name=domainname) domain.addCatalog(GettextMessageCatalog(lang, domainname, domainpath)) # negotiation sucks :( because Chameleon never passes a context into the # translate method of the template. but the target_language is passed. # so we need to set the target language to what comes with the request as # 'Accept-Language' header. The RequestNegotiator then get this header value class RequestNegotiator(object): implements(INegotiator) def getLanguage(self, available_languages, accept_languages_header): if isinstance(accept_languages_header, Request): accept_languages_header = accept_languages_header.headers.get('Accept-Language', '')
def TestMessageFallbackDomain(domain_id=u""): domain = TranslationDomain(domain_id) domain.addCatalog(TestMessageCatalog(domain_id)) return domain