当前位置: 首页>>代码示例>>Python>>正文


Python pofile.read_po函数代码示例

本文整理汇总了Python中babel.messages.pofile.read_po函数的典型用法代码示例。如果您正苦于以下问题:Python read_po函数的具体用法?Python read_po怎么用?Python read_po使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了read_po函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run

    def run(self, root):

        i18n_dir = self.extension.getConfig('i18n_dir')
        pot_path = os.path.join(i18n_dir, 'messages.pot')

        if os.path.exists(pot_path):
            with open(pot_path, 'r') as f:
                catalog = pofile.read_po(f)
        else:
            catalog = Catalog()

        lang = self.extension.getConfig('i18n_lang')
        mo_path = os.path.join(i18n_dir, lang, 'LC_MESSAGES', 'messages.mo')
        po_path = os.path.join(i18n_dir, lang, 'LC_MESSAGES', 'messages.po')

        if os.path.exists(po_path):
            with open(po_path, 'r') as f:
                lang_catalog = pofile.read_po(f)
            with open(mo_path, 'w') as mo:
                mofile.write_mo(mo, lang_catalog)

        translations = Translations.load(i18n_dir, locales=[lang])
        self.translate(catalog, translations, root)

        with open(pot_path, 'w') as pot_file:
            pofile.write_po(pot_file, catalog)
开发者ID:gisce,项目名称:markdown-i18n,代码行数:26,代码来源:parser.py

示例2: test_update

    def test_update(self):
        template = Catalog()
        template.add("1")
        template.add("2")
        template.add("3")
        tmpl_file = os.path.join(self._i18n_dir(), "temp-template.pot")
        with open(tmpl_file, "wb") as outfp:
            write_po(outfp, template)
        po_file = os.path.join(self._i18n_dir(), "temp1.po")
        self.cli.run(sys.argv + ["init", "-l", "fi", "-o", po_file, "-i", tmpl_file])
        with open(po_file, "r") as infp:
            catalog = read_po(infp)
            assert len(catalog) == 3

        # Add another entry to the template

        template.add("4")

        with open(tmpl_file, "wb") as outfp:
            write_po(outfp, template)

        self.cli.run(sys.argv + ["update", "-l", "fi_FI", "-o", po_file, "-i", tmpl_file])

        with open(po_file, "r") as infp:
            catalog = read_po(infp)
            assert len(catalog) == 4  # Catalog was updated
开发者ID:sachinpali146,项目名称:babel,代码行数:26,代码来源:test_frontend.py

示例3: import_file

 def import_file(self, locale, po_path):
   if locale is None:
     raise Error('Must specify locale.')
   if not os.path.exists(po_path):
     raise Error('Couldn\'t find PO file: {}'.format(po_path))
   babel_locale = external_to_babel_locales.get(locale, locale)
   pod_translations_dir = os.path.join(
       'translations', babel_locale, 'LC_MESSAGES')
   pod_po_path = os.path.join(pod_translations_dir, 'messages.po')
   if self.pod.file_exists(pod_po_path):
     existing_po_file = self.pod.open_file(pod_po_path)
     existing_catalog = pofile.read_po(existing_po_file, babel_locale)
     po_file_to_merge = open(po_path)
     catalog_to_merge = pofile.read_po(po_file_to_merge, babel_locale)
     for message in catalog_to_merge:
       if message.id not in existing_catalog:
         existing_catalog[message.id] = message
       else:
         existing_catalog[message.id].string = message.string
     existing_po_file = self.pod.open_file(pod_po_path, mode='w')
     pofile.write_po(existing_po_file, existing_catalog, width=80,
                     omit_header=True, sort_output=True, sort_by_file=True)
     text = 'Imported {} translations: {}'
     self.pod.logger.info(text.format(len(catalog_to_merge), babel_locale))
   else:
     abs_po_path = self.pod.abs_path(pod_po_path)
     abs_po_dir = os.path.dirname(abs_po_path)
     _mkdir(abs_po_dir)
     shutil.copyfile(po_path, abs_po_path)
     self.pod.logger.info('Imported new catalog: {}'.format(babel_locale))
开发者ID:codedcolors,项目名称:pygrow,代码行数:30,代码来源:importers.py

示例4: _build

    def _build(self, locale, messages, path, pathGlobal=None):
        '''
        Builds a catalog based on the provided locale paths, the path is used as the main source any messages that are not
        found in path locale but are part of messages will attempt to be extracted from the global path locale.

        @param locale: Locale
            The locale.
        @param messages: Iterable(Message)
            The messages to build the PO file on.
        @param path: string
            The path of the targeted PO file from the locale repository.
        @param pathGlobal: string|None
            The path of the global PO file from the locale repository.
        @return: file like object
            File like object that contains the PO file content
        '''
        assert isinstance(locale, Locale), 'Invalid locale %s' % locale
        assert isinstance(messages, Iterable), 'Invalid messages %s' % messages
        assert isinstance(path, str), 'Invalid path %s' % path
        assert pathGlobal is None or isinstance(pathGlobal, str), 'Invalid global path %s' % pathGlobal
        if isfile(path):
            with open(path) as fObj: catalog = read_po(fObj, locale)
        else:
            catalog = Catalog(locale, creation_date=datetime.now(), **self.catalog_config)
        if pathGlobal and isfile(pathGlobal):
            with open(pathGlobal) as fObj: catalogGlobal = read_po(fObj, locale)
        else:
            catalogGlobal = None

        self._processCatalog(catalog, messages, fallBack=catalogGlobal)
        catalog.revision_date = datetime.now()

        return catalog
开发者ID:AtomLaw,项目名称:Ally-Py,代码行数:33,代码来源:po_file_manager.py

示例5: test_update

    def test_update(self):
        template = Catalog()
        template.add("1")
        template.add("2")
        template.add("3")
        tmpl_file = os.path.join(self._i18n_dir(), 'temp-template.pot')
        with open(tmpl_file, "wb") as outfp:
            write_po(outfp, template)
        po_file = os.path.join(self._i18n_dir(), 'temp1.po')
        self.cli.run(sys.argv + ['init',
                                 '-l', 'fi',
                                 '-o', po_file,
                                 '-i', tmpl_file
                                 ])
        with open(po_file, "r") as infp:
            catalog = read_po(infp)
            assert len(catalog) == 3

        # Add another entry to the template

        template.add("4")

        with open(tmpl_file, "wb") as outfp:
            write_po(outfp, template)

        self.cli.run(sys.argv + ['update',
                                 '-l', 'fi_FI',
                                 '-o', po_file,
                                 '-i', tmpl_file])

        with open(po_file, "r") as infp:
            catalog = read_po(infp)
            assert len(catalog) == 4  # Catalog was updated
开发者ID:Mabusto,项目名称:babel,代码行数:33,代码来源:test_frontend.py

示例6: merge_file

def merge_file(source_filename, target_filename, template_filename):
    if source_filename.endswith('.po'):
        merge(pofile.read_po(open(source_filename)), target_filename)
    elif source_filename.endswith('.xml'):
        handler = XmbCatalogReader(pofile.read_po(open(template_filename)))
        xml.sax.parse(open(source_filename), handler)
        merge(handler.catalog, target_filename)
开发者ID:Aloknayan,项目名称:personfinder,代码行数:7,代码来源:merge_messages.py

示例7: run

    def run(self):
        if tx_executable is None:
            sys.exit('Transifex client executable (tx) not found.')

        from babel.messages import pofile

        if not self.skip_pull:
            txpull_cmd = [
                tx_executable,
                'pull',
                '--force',
                '--resource=musicbrainz.attributes,musicbrainz.countries',
                '--source',
                '--language=none',
            ]
            self.spawn(txpull_cmd)

        countries = dict()
        countries_potfile = os.path.join('po', 'countries', 'countries.pot')
        isocode_comment = u'iso.code:'
        with open(countries_potfile, 'rb') as f:
            log.info('Parsing %s' % countries_potfile)
            po = pofile.read_po(f)
            for message in po:
                if not message.id or not isinstance(message.id, unicode):
                    continue
                for comment in message.auto_comments:
                    if comment.startswith(isocode_comment):
                        code = comment.replace(isocode_comment, u'')
                        countries[code] = message.id
            if countries:
                self.countries_py_file(countries)
            else:
                sys.exit('Failed to extract any country code/name !')

        attributes = dict()
        attributes_potfile = os.path.join('po', 'attributes', 'attributes.pot')
        extract_attributes = (
            u'DB:cover_art_archive.art_type/name',
            u'DB:medium_format/name',
            u'DB:release_group_primary_type/name',
            u'DB:release_group_secondary_type/name',
        )
        with open(attributes_potfile, 'rb') as f:
            log.info('Parsing %s' % attributes_potfile)
            po = pofile.read_po(f)
            for message in po:
                if not message.id or not isinstance(message.id, unicode):
                    continue
                for loc, pos in message.locations:
                    if loc in extract_attributes:
                        attributes[u"%s:%03d" % (loc, pos)] = message.id
            if attributes:
                self.attributes_py_file(attributes)
            else:
                sys.exit('Failed to extract any attribute !')
开发者ID:Murat-Tulek,项目名称:picard,代码行数:56,代码来源:setup.py

示例8: test_abort_invalid_po_file

    def test_abort_invalid_po_file(self):
        invalid_po = '''
            msgctxt ""
            "{\"checksum\": 2148532640, \"cxt\": \"collector_thankyou\", \"id\": "
            "270005359}"
            msgid ""
            "Thank you very much for your time.\n"
            "If you have any questions regarding this survey, please contact Fulano "
            "at [email protected]"
            msgstr "Merci de prendre le temps de remplir le sondage.
            Pour toute question, veuillez communiquer avec Fulano  à [email protected]
            "
        '''
        invalid_po_2 = '''
            msgctxt ""
            "{\"checksum\": 2148532640, \"cxt\": \"collector_thankyou\", \"id\": "
            "270005359}"
            msgid ""
            "Thank you very much for your time.\n"
            "If you have any questions regarding this survey, please contact Fulano "
            "at [email protected]"
            msgstr "Merci de prendre le temps de remplir le sondage.
            Pour toute question, veuillez communiquer avec Fulano a [email protected]
            "
            '''
        # Catalog not created, throws Unicode Error
        buf = StringIO(invalid_po)
        output = None

        # This should only be thrown under py27
        if sys.version_info.major == 2:
            with self.assertRaises(UnicodeEncodeError):
                output = pofile.read_po(buf, locale='fr', abort_invalid=False)
            assert not output
        else:
            output = pofile.read_po(buf, locale='fr', abort_invalid=False)
            assert isinstance(output, Catalog)

        # Catalog not created, throws PoFileError
        buf = StringIO(invalid_po_2)
        output = None
        with self.assertRaises(pofile.PoFileError) as e:
            output = pofile.read_po(buf, locale='fr', abort_invalid=True)
        assert not output

        # Catalog is created with warning, no abort
        buf = StringIO(invalid_po_2)
        output = pofile.read_po(buf, locale='fr', abort_invalid=False)
        assert isinstance(output, Catalog)

        # Catalog not created, aborted with PoFileError
        buf = StringIO(invalid_po_2)
        output = None
        with self.assertRaises(pofile.PoFileError) as e:
            output = pofile.read_po(buf, locale='fr', abort_invalid=True)
        assert not output
开发者ID:Changaco,项目名称:babel,代码行数:56,代码来源:test_pofile.py

示例9: test_locale_gets_overridden_by_file

    def test_locale_gets_overridden_by_file(self):
        buf = StringIO(r'''
msgid ""
msgstr ""
"Language: en_US\n"''')
        catalog = pofile.read_po(buf, locale='de')
        self.assertEqual(Locale('en', 'US'), catalog.locale)
        buf = StringIO(r'''
msgid ""
msgstr ""
"Language: ko-KR\n"''')
        catalog = pofile.read_po(buf, locale='de')
        self.assertEqual(Locale('ko', 'KR'), catalog.locale)
开发者ID:Changaco,项目名称:babel,代码行数:13,代码来源:test_pofile.py

示例10: test_overwrite_po_success

def test_overwrite_po_success(minimal_i18n_settings, temp_builds_dir,
                              fixtures_settings):
    """
    safe_write_po usage for overwritting file
    """
    basepath = temp_builds_dir.join('i18n_overwrite_po_success')

    # Copy sample project to temporary dir
    samplename = 'minimal_i18n'
    samplepath = os.path.join(fixtures_settings.fixtures_path, samplename)
    destination = os.path.join(basepath.strpath, samplename)
    shutil.copytree(samplepath, destination)

    dummy_name = "dummy_pot.pot"
    dummy_pot = os.path.join(destination, dummy_name)

    # Get manager with settings
    settings = minimal_i18n_settings(destination)
    manager = I18NManager(settings)

    # Create a dummy catalog to write
    catalog = Catalog(header_comment="# Foobar")
    catalog.add('foo %(name)s', locations=[('main.py', 1)], flags=('fuzzy',))

    # Write it
    manager.safe_write_po(catalog, dummy_pot)

    # Check it
    with io.open(dummy_pot, 'rb') as f:
        dummy_catalog = read_po(f)

    assert dummy_catalog.header_comment == "# Foobar"

    # New dummy catalog to overwrite previous one
    catalog = Catalog(header_comment="# Zob")
    catalog.add('ping', string='pong', locations=[('nope.py', 42)])

    # Write it
    manager.safe_write_po(catalog, dummy_pot)

    # List existing pot file at root
    pots = [item for item in os.listdir(destination) if item.endswith('.pot')]
    # No other pot file
    assert pots == [dummy_name]

    # Check it again
    with io.open(dummy_pot, 'rb') as f:
        dummy_catalog = read_po(f)

    assert dummy_catalog.header_comment == "# Zob"
    assert dummy_catalog["ping"] == Message('ping', string='pong')
开发者ID:sveetch,项目名称:Optimus,代码行数:51,代码来源:05_safe_write_po.py

示例11: load_po

def load_po(filename):
    """read po/pot file and return catalog object

    :param unicode filename: path to po/pot file
    :return: catalog object
    """
    # pre-read to get charset
    with io.open(filename, 'rb') as f:
        cat = pofile.read_po(f)
    charset = cat.charset or 'utf-8'

    # To decode lines by babel, read po file as binary mode and specify charset for
    # read_po function.
    with io.open(filename, 'rb') as f:  # FIXME: encoding VS charset
        return pofile.read_po(f, charset=charset)
开发者ID:sphinx-doc,项目名称:sphinx-intl,代码行数:15,代码来源:catalog.py

示例12: write_po_files

def write_po_files(domain):
    for locale in GETTEXT_LANGUAGES_LOCALE:
        popath = os.path.join("locale", locale, "LC_MESSAGES", domain + ".po")
        potpath = os.path.join("locale", domain + ".pot")

        with open(popath, 'r') as po_f, open(potpath, 'r') as pot_f:
            template = read_po(pot_f)
            catalog = read_po(po_f)
            catalog.update(template)

        with open(popath, 'w') as po_f:
            write_po(po_f, catalog, ignore_obsolete=True)

    potpath = os.path.join("locale", domain + ".pot")
    os.unlink(potpath)
开发者ID:jgriffiths,项目名称:GreenAddressWebFiles,代码行数:15,代码来源:gettext_finder.py

示例13: test_overwrite_po_fail

def test_overwrite_po_fail(minimal_i18n_settings, temp_builds_dir,
                           fixtures_settings):
    """
    safe_write_po usage for overwritting file failing but left untouched
    initial file
    """
    basepath = temp_builds_dir.join('i18n_overwrite_po_fail')

    # Copy sample project to temporary dir
    samplename = 'minimal_i18n'
    samplepath = os.path.join(fixtures_settings.fixtures_path, samplename)
    destination = os.path.join(basepath.strpath, samplename)
    shutil.copytree(samplepath, destination)

    dummy_name = "dummy_pot.pot"
    dummy_pot = os.path.join(destination, dummy_name)

    # Get manager with settings
    settings = minimal_i18n_settings(destination)
    manager = I18NManager(settings)

    # Create a dummy catalog to write
    catalog = Catalog(header_comment="# Foobar")
    catalog.add('foo %(name)s', locations=[('main.py', 1)], flags=('fuzzy',))

    # Write it
    manager.safe_write_po(catalog, dummy_pot)

    # Check it
    with io.open(dummy_pot, 'rb') as f:
        dummy_catalog = read_po(f)

    assert dummy_catalog.header_comment == "# Foobar"

    # Try to overwrite with empty catalog to raise error
    with pytest.raises(TypeError):
        manager.safe_write_po(None, dummy_pot)

    # List existing pot file at root
    pots = [item for item in os.listdir(destination) if item.endswith('.pot')]
    # No other pot file
    assert pots == [dummy_name]

    # Check it again
    with io.open(dummy_pot, 'rb') as f:
        dummy_catalog = read_po(f)
    # Initial has been left untouched
    assert dummy_catalog.header_comment == "# Foobar"
开发者ID:sveetch,项目名称:Optimus,代码行数:48,代码来源:05_safe_write_po.py

示例14: test_with_context

    def test_with_context(self):
        buf = BytesIO(b'''# Some string in the menu
#: main.py:1
msgctxt "Menu"
msgid "foo"
msgstr "Voh"

# Another string in the menu
#: main.py:2
msgctxt "Menu"
msgid "bar"
msgstr "Bahr"
''')
        catalog = pofile.read_po(buf, ignore_obsolete=True)
        self.assertEqual(2, len(catalog))
        message = catalog.get('foo', context='Menu')
        self.assertEqual('Menu', message.context)
        message = catalog.get('bar', context='Menu')
        self.assertEqual('Menu', message.context)

        # And verify it pass through write_po
        out_buf = BytesIO()
        pofile.write_po(out_buf, catalog, omit_header=True)
        assert out_buf.getvalue().strip() == buf.getvalue().strip(), \
            out_buf.getvalue()
开发者ID:Changaco,项目名称:babel,代码行数:25,代码来源:test_pofile.py

示例15: main

def main():
    # Check arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('pot_filename', type=argparse.FileType('r'))
    parser.add_argument('po_filename', type=argparse.FileType('w'))
    parser.add_argument('locale')
    args = parser.parse_args()

    # read POT file
    pot_cat = pofile.read_po(args.pot_filename, ignore_obsolete=True)

    # Create the new Catalog
    new_cat = catalog.Catalog(locale=args.locale,
                              last_translator="pseudo.py",
                              charset="utf-8")
    num_plurals = new_cat.num_plurals

    # Process messages from template
    for msg in pot_cat:
        if msg.pluralizable:
            msg.string = [translate(u"{}:{}".format(i, msg.id[0]))
                          for i in range(num_plurals)]
        else:
            msg.string = translate(msg.id)
        new_cat[msg.id] = msg

    # Write "translated" PO file
    pofile.write_po(args.po_filename, new_cat, ignore_obsolete=True)
开发者ID:AlSayedGamal,项目名称:horizon,代码行数:28,代码来源:pseudo.py


注:本文中的babel.messages.pofile.read_po函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。