Example #1
0
    def test_sorting(self):
        # Ensure the header is sorted to the first entry so that its charset
        # can be applied to all subsequent messages by GNUTranslations
        # (ensuring all messages are safely converted to unicode)
        catalog = Catalog(locale='en_US')
        catalog.add(u'', '''\
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n''')
        catalog.add(u'foo', 'Voh')
        catalog.add((u'There is', u'There are'), (u'Es gibt', u'Es gibt'))
        catalog.add(u'Fizz', '')
        catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
        buf = BytesIO()
        mofile.write_mo(buf, catalog)
        buf.seek(0)
        translations = Translations(fp=buf)
        self.assertEqual(u'Voh', translations.ugettext('foo'))
        assert isinstance(translations.ugettext('foo'), text_type)
        self.assertEqual(u'Es gibt', translations.ungettext('There is', 'There are', 1))
        assert isinstance(translations.ungettext('There is', 'There are', 1), text_type)
        self.assertEqual(u'Fizz', translations.ugettext('Fizz'))
        assert isinstance(translations.ugettext('Fizz'), text_type)
        self.assertEqual(u'Fuzz', translations.ugettext('Fuzz'))
        assert isinstance(translations.ugettext('Fuzz'), text_type)
        self.assertEqual(u'Fuzzes', translations.ugettext('Fuzzes'))
        assert isinstance(translations.ugettext('Fuzzes'), text_type)
Example #2
0
 def setUp(self):
     # Use a locale which won't fail to run the tests
     os.environ['LANG'] = 'en_US.UTF-8'
     messages1 = [
         ('foo', {'string': 'Voh'}),
         ('foo', {'string': 'VohCTX', 'context': 'foo'}),
         (('foo1', 'foos1'), {'string': ('Voh1', 'Vohs1')}),
         (('foo1', 'foos1'), {'string': ('VohCTX1', 'VohsCTX1'), 'context': 'foo'}),
     ]
     messages2 = [
         ('foo', {'string': 'VohD'}),
         ('foo', {'string': 'VohCTXD', 'context': 'foo'}),
         (('foo1', 'foos1'), {'string': ('VohD1', 'VohsD1')}),
         (('foo1', 'foos1'), {'string': ('VohCTXD1', 'VohsCTXD1'), 'context': 'foo'}),
     ]
     catalog1 = Catalog(locale='en_GB', domain='messages')
     catalog2 = Catalog(locale='en_GB', domain='messages1')
     for ids, kwargs in messages1:
         catalog1.add(ids, **kwargs)
     for ids, kwargs in messages2:
         catalog2.add(ids, **kwargs)
     catalog1_fp = BytesIO()
     catalog2_fp = BytesIO()
     write_mo(catalog1_fp, catalog1)
     catalog1_fp.seek(0)
     write_mo(catalog2_fp, catalog2)
     catalog2_fp.seek(0)
     translations1 = support.Translations(catalog1_fp)
     translations2 = support.Translations(catalog2_fp, domain='messages1')
     self.translations = translations1.add(translations2, merge=False)
Example #3
0
    def run(self):
        po_files = []
        mo_files = []

        if not self.input_file:
            if self.locale:
                po_files.append(
                    (self.locale, os.path.join(self.directory, self.locale, "LC_MESSAGES", self.domain + ".po"))
                )
                mo_files.append(os.path.join(self.directory, self.locale, "LC_MESSAGES", self.domain + ".mo"))
            else:
                for locale in os.listdir(self.directory):
                    po_file = os.path.join(self.directory, locale, "LC_MESSAGES", self.domain + ".po")
                    if os.path.exists(po_file):
                        po_files.append((locale, po_file))
                        mo_files.append(os.path.join(self.directory, locale, "LC_MESSAGES", self.domain + ".mo"))
        else:
            po_files.append((self.locale, self.input_file))
            if self.output_file:
                mo_files.append(self.output_file)
            else:
                mo_files.append(os.path.join(self.directory, self.locale, "LC_MESSAGES", self.domain + ".mo"))

        if not po_files:
            raise DistutilsOptionError("no message catalogs found")

        for idx, (locale, po_file) in enumerate(po_files):
            mo_file = mo_files[idx]
            infile = open(po_file, "rb")
            try:
                catalog = read_po(infile, locale)
            finally:
                infile.close()

            if self.statistics:
                translated = 0
                for message in list(catalog)[1:]:
                    if message.string:
                        translated += 1
                percentage = 0
                if len(catalog):
                    percentage = translated * 100 // len(catalog)
                log.info("%d of %d messages (%d%%) translated in %r", translated, len(catalog), percentage, po_file)

            if catalog.fuzzy and not self.use_fuzzy:
                log.warn("catalog %r is marked as fuzzy, skipping", po_file)
                continue

            for message, errors in catalog.check():
                for error in errors:
                    log.error("error: %s:%d: %s", po_file, message.lineno, error)

            log.info("compiling catalog %r to %r", po_file, mo_file)

            outfile = open(mo_file, "wb")
            try:
                write_mo(outfile, catalog, use_fuzzy=self.use_fuzzy)
            finally:
                outfile.close()
Example #4
0
    def test_load(self):
        tempdir = tempfile.mkdtemp()
        try:
            messages_dir = os.path.join(tempdir, 'fr', 'LC_MESSAGES')
            os.makedirs(messages_dir)
            catalog = Catalog(locale='fr', domain='messages')
            catalog.add('foo', 'bar')
            with open(os.path.join(messages_dir, 'messages.mo'), 'wb') as f:
                write_mo(f, catalog)

            translations = support.Translations.load(tempdir, locales=('fr',), domain='messages')
            self.assertEqual('bar', translations.gettext('foo'))
        finally:
            shutil.rmtree(tempdir)
Example #5
0
 def setUp(self):
     fp = BytesIO()
     write_mo(fp, Catalog(locale='de'))
     fp.seek(0)
     self.translations = support.Translations(fp=fp)
     self.null_translations = support.NullTranslations(fp=fp)
Example #6
0
 def test_more_plural_forms(self):
     catalog2 = Catalog(locale='ru_RU')
     catalog2.add(('Fuzz', 'Fuzzes'), ('', '', ''))
     buf = BytesIO()
     mofile.write_mo(buf, catalog2)
Example #7
0
    def compile(self, argv):
        """Subcommand for compiling a message catalog to a MO file.

        :param argv: the command arguments
        :since: version 0.9
        """
        parser = OptionParser(usage=self.usage % ("compile", ""), description=self.commands["compile"])
        parser.add_option("--domain", "-D", dest="domain", help="domain of MO and PO files (default '%default')")
        parser.add_option("--directory", "-d", dest="directory", metavar="DIR", help="base directory of catalog files")
        parser.add_option("--locale", "-l", dest="locale", metavar="LOCALE", help="locale of the catalog")
        parser.add_option("--input-file", "-i", dest="input_file", metavar="FILE", help="name of the input file")
        parser.add_option(
            "--output-file",
            "-o",
            dest="output_file",
            metavar="FILE",
            help="name of the output file (default " "'<output_dir>/<locale>/LC_MESSAGES/" "<domain>.mo')",
        )
        parser.add_option(
            "--use-fuzzy",
            "-f",
            dest="use_fuzzy",
            action="store_true",
            help="also include fuzzy translations (default " "%default)",
        )
        parser.add_option(
            "--statistics", dest="statistics", action="store_true", help="print statistics about translations"
        )

        parser.set_defaults(domain="messages", use_fuzzy=False, compile_all=False, statistics=False)
        options, args = parser.parse_args(argv)

        po_files = []
        mo_files = []
        if not options.input_file:
            if not options.directory:
                parser.error("you must specify either the input file or the " "base directory")
            if options.locale:
                po_files.append(
                    (
                        options.locale,
                        os.path.join(options.directory, options.locale, "LC_MESSAGES", options.domain + ".po"),
                    )
                )
                mo_files.append(os.path.join(options.directory, options.locale, "LC_MESSAGES", options.domain + ".mo"))
            else:
                for locale in os.listdir(options.directory):
                    po_file = os.path.join(options.directory, locale, "LC_MESSAGES", options.domain + ".po")
                    if os.path.exists(po_file):
                        po_files.append((locale, po_file))
                        mo_files.append(os.path.join(options.directory, locale, "LC_MESSAGES", options.domain + ".mo"))
        else:
            po_files.append((options.locale, options.input_file))
            if options.output_file:
                mo_files.append(options.output_file)
            else:
                if not options.directory:
                    parser.error("you must specify either the output file or " "the base directory")
                mo_files.append(os.path.join(options.directory, options.locale, "LC_MESSAGES", options.domain + ".mo"))
        if not po_files:
            parser.error("no message catalogs found")

        for idx, (locale, po_file) in enumerate(po_files):
            mo_file = mo_files[idx]
            infile = open(po_file, "rb")
            try:
                catalog = read_po(infile, locale)
            finally:
                infile.close()

            if options.statistics:
                translated = 0
                for message in list(catalog)[1:]:
                    if message.string:
                        translated += 1
                percentage = 0
                if len(catalog):
                    percentage = translated * 100 // len(catalog)
                self.log.info(
                    "%d of %d messages (%d%%) translated in %r", translated, len(catalog), percentage, po_file
                )

            if catalog.fuzzy and not options.use_fuzzy:
                self.log.warning("catalog %r is marked as fuzzy, skipping", po_file)
                continue

            for message, errors in catalog.check():
                for error in errors:
                    self.log.error("error: %s:%d: %s", po_file, message.lineno, error)

            self.log.info("compiling catalog %r to %r", po_file, mo_file)

            outfile = open(mo_file, "wb")
            try:
                write_mo(outfile, catalog, use_fuzzy=options.use_fuzzy)
            finally:
                outfile.close()