Example #1
0
def makecatalog():
    if len(sys.argv) < 3:
        print 'Usage: %s <domain> <target directory>' % sys.argv[0]
        sys.exit(0)

    domain = sys.argv[1]
    target = sys.argv[2]

    import zope.app.locales
    zope_locales_path = os.path.dirname(zope.app.locales.__file__)
    languages = [d for d in os.listdir(zope_locales_path) \
                 if os.path.isdir(os.path.join(zope_locales_path, d))]

    if not os.path.exists(target):
        os.makedirs(target)

    pot_file = os.path.join(zope_locales_path, 'zope.pot')
    target_pot = os.path.join(target, domain + '.pot')
    shutil.copyfile(pot_file, target_pot)

    for language in languages:
        lang_path = os.path.join(target, language, 'LC_MESSAGES')
        if not os.path.exists(lang_path):
            os.makedirs(lang_path)
        src = os.path.join(zope_locales_path, language, 'LC_MESSAGES',
                           'zope.po')
        dst = os.path.join(lang_path, domain + '.po')
        shutil.copyfile(src, dst)
        compile_mo_file(domain, lang_path)
    def test_po_exists_but_invalid(self):
        import tempfile
        import shutil
        import os.path

        td = tempfile.mkdtemp(suffix=".zopei18n_test_compile")
        self.addCleanup(shutil.rmtree, td)

        with open(os.path.join(td, "foo.po"), 'w') as f:
            f.write("this should not compile")

        compile.compile_mo_file('foo', td)

        self.assertIn("Syntax error while compiling", str(self.handler))
Example #3
0
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))
Example #4
0
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))
Example #5
0
    def test_po_exists_but_invalid(self):
        import tempfile
        import shutil
        import os.path

        td = tempfile.mkdtemp(suffix=".zopei18n_test_compile")
        self.addCleanup(shutil.rmtree, td)

        with open(os.path.join(td, "foo.po"), 'w') as f:
            f.write("this should not compile")

        compile.compile_mo_file('foo', td)

        self.assertIn("Syntax error while compiling",
                      str(self.handler))
    def test_po_exists_cannot_write_mo(self):
        import tempfile
        import shutil
        import os
        import os.path

        td = tempfile.mkdtemp(suffix=".zopei18n_test_compile")
        self.addCleanup(shutil.rmtree, td)

        mofile = os.path.join(td, 'foo.mo')
        with open(mofile, 'w') as f:
            f.write("Touching")

        # Put it in the past, make it not writable
        os.utime(mofile, (1000, 1000))
        os.chmod(mofile, 0)

        with open(os.path.join(td, "foo.po"), 'w') as f:
            f.write("# A comment")

        compile.compile_mo_file('foo', td)

        self.assertIn("Error while compiling", str(self.handler))
Example #7
0
    def test_po_exists_cannot_write_mo(self):
        import tempfile
        import shutil
        import os
        import os.path

        td = tempfile.mkdtemp(suffix=".zopei18n_test_compile")
        self.addCleanup(shutil.rmtree, td)

        mofile = os.path.join(td, 'foo.mo')
        with open(mofile, 'w') as f:
            f.write("Touching")

        # Put it in the past, make it not writable
        os.utime(mofile, (1000, 1000))
        os.chmod(mofile, 0)

        with open(os.path.join(td, "foo.po"), 'w') as f:
            f.write("# A comment")

        compile.compile_mo_file('foo', td)

        self.assertIn("Error while compiling",
                      str(self.handler))
 def test_non_existant_path(self):
     self.assertIsNone(compile.compile_mo_file('no_such_domain', ''))
Example #9
0
 def test_non_existant_path(self):
     self.assertIsNone(compile.compile_mo_file('no_such_domain', ''))