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 _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 _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 _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 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 registerTranslations(_context, directory, domain='*'): path = os.path.normpath(directory) domains = {} loaded = False # 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): if not allow_language(language): continue lc_messages_path = os.path.join(path, language, 'LC_MESSAGES') if os.path.isdir(lc_messages_path): # Preprocess files and update or compile the mo files if config.COMPILE_MO_FILES: for domain_path in glob( os.path.join(lc_messages_path, '%s.po' % domain)): domain_file = os.path.basename(domain_path) name = domain_file[:-3] compile_mo_file(name, lc_messages_path) for domain_path in glob( os.path.join(lc_messages_path, '%s.mo' % domain)): loaded = True domain_file = os.path.basename(domain_path) name = domain_file[:-3] if name not in domains: domains[name] = {} domains[name][language] = domain_path if loaded: logger.debug('register directory %s', directory) # Now create TranslationDomain objects and add them as utilities for name, langs in domains.items(): catalogs = [] for lang, file in langs.items(): catalogs.append(GettextMessageCatalog(lang, name, file)) # register the necessary actions directly (as opposed to using # `zope.component.zcml.utility`) since we need the actual utilities # in place before the merging can be done... _context.action(discriminator=None, callable=handler, args=(catalogs, name)) # also register the interface for the translation utilities provides = ITranslationDomain _context.action(discriminator=None, callable=provideInterface, args=(provides.__module__ + '.' + provides.getName(), provides))
def test_is_unicode(self): from zope.i18n.gettextmessagecatalog import GettextMessageCatalog path = os.path.dirname(__file__) langs = os.listdir(path) for lang in langs: lc_path = os.path.join(path, lang, 'LC_MESSAGES') if os.path.isdir(lc_path): files = os.listdir(lc_path) for f in files: if f.endswith('.mo'): mcatalog = GettextMessageCatalog(lang, 'zope', os.path.join(lc_path, f)) catalog = mcatalog._catalog self.failUnless(catalog._charset, u"""Charset value for the Message catalog is missing. The language is %s (zope.po). Value of the message catalog should be in unicode""" % (lang,) )
def _getMessageCatalog(self): from zope.i18n import tests path = os.path.dirname(tests.__file__) self._path = os.path.join(path, 'en-default.mo') catalog = GettextMessageCatalog('en', 'default', self._path) return catalog
def _getMessageCatalog(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) return catalog