本文整理汇总了Python中babel.messages.pofile.read_po方法的典型用法代码示例。如果您正苦于以下问题:Python pofile.read_po方法的具体用法?Python pofile.read_po怎么用?Python pofile.read_po使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类babel.messages.pofile
的用法示例。
在下文中一共展示了pofile.read_po方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from babel.messages import pofile [as 别名]
# 或者: from babel.messages.pofile import read_po [as 别名]
def main():
try:
import jieba # noqa: F401
except ImportError:
return
pofile.normalize = _normalize
for root, dirs, files in os.walk('.'):
if 'zh' not in root:
continue
for f in files:
if not f.endswith('.po'):
continue
path = os.path.join(root, f)
# only modify recent-changed files
modify_time = datetime.datetime.fromtimestamp(os.path.getmtime(path))
if (datetime.datetime.now() - modify_time).total_seconds() > 120:
continue
with open(path, 'rb') as inpf:
catalog = pofile.read_po(inpf)
with open(path, 'wb') as outf:
pofile.write_po(outf, catalog)
示例2: run
# 需要导入模块: from babel.messages import pofile [as 别名]
# 或者: from babel.messages.pofile import read_po [as 别名]
def run(self):
log.info('creating catalog %r based on %r', self.output_file,
self.input_file)
infile = open(self.input_file, 'r')
try:
# Although reading from the catalog template, read_po must be fed
# the locale in order to correctly calculate plurals
catalog = read_po(infile, locale=self.locale)
finally:
infile.close()
catalog.locale = self._locale
catalog.revision_date = datetime.now(LOCALTZ)
catalog.fuzzy = False
outfile = open(self.output_file, 'wb')
try:
write_po(outfile, catalog, width=self.width)
finally:
outfile.close()
示例3: run
# 需要导入模块: from babel.messages import pofile [as 别名]
# 或者: from babel.messages.pofile import read_po [as 别名]
def run(self):
self.log.info(
'creating catalog %s based on %s', self.output_file, self.input_file
)
with open(self.input_file, 'rb') as infile:
# Although reading from the catalog template, read_po must be fed
# the locale in order to correctly calculate plurals
catalog = read_po(infile, locale=self.locale)
catalog.locale = self._locale
catalog.revision_date = datetime.now(LOCALTZ)
catalog.fuzzy = False
with open(self.output_file, 'wb') as outfile:
write_po(outfile, catalog, width=self.width)
示例4: _for_library
# 需要导入模块: from babel.messages import pofile [as 别名]
# 或者: from babel.messages.pofile import read_po [as 别名]
def _for_library(self, mod) -> MessageLocalizer:
"""Return a `MessageLocalizer` for the messages of, say, `cjwmodule.i18n`"""
catalogs = {}
for locale_id in supported_locales:
try:
with importlib.resources.open_binary(mod, f"{locale_id}.po") as pofile:
catalogs[locale_id] = read_po(pofile, abort_invalid=True)
except (FileNotFoundError, ModuleNotFoundError) as err:
if locale_id != default_locale:
# This will help us support new languages out-of-order
# i.e., translate `cjworkbench` before translating `cjwmodule`.
logger.exception(
"%r does not support locale %s: %s",
mod.__name__,
locale_id,
err,
)
catalogs[locale_id] = Catalog(locale_id)
else:
raise
return MessageLocalizer(catalogs)
示例5: _create_localizer_for_module_zipfile
# 需要导入模块: from babel.messages import pofile [as 别名]
# 或者: from babel.messages.pofile import read_po [as 别名]
def _create_localizer_for_module_zipfile(
cls, module_zipfile: ModuleZipfile
) -> Optional[MessageLocalizer]:
catalogs = {}
for locale_id in supported_locales:
try:
catalogs[locale_id] = read_po(
BytesIO(module_zipfile.read_messages_po_for_locale(locale_id)),
abort_invalid=True,
)
except PoFileError as err:
logger.exception(
"Invalid po file for module %s in locale %s: %s",
module_zipfile.module_id_and_version,
locale_id,
err,
)
pass
except KeyError:
pass
if not catalogs:
return None
return MessageLocalizer(catalogs)
示例6: merge
# 需要导入模块: from babel.messages import pofile [as 别名]
# 或者: from babel.messages.pofile import read_po [as 别名]
def merge(source, target_filename):
"""Merges the messages from the source Catalog into a .po file at
target_filename. Creates the target file if it doesn't exist."""
if os.path.exists(target_filename):
target = pofile.read_po(open(target_filename))
for message in source:
if message.id and message.string and not message.fuzzy:
log_change(message.id in target and target[message.id], message)
# This doesn't actually replace the message! It just updates
# the fields other than the string. See Catalog.__setitem__.
target[message.id] = message
# We have to mutate the message to update the string and flags.
target[message.id].string = message.string
target[message.id].flags = message.flags
else:
for message in source:
log_change(None, message)
target = source
target_file = create_file(target_filename)
pofile.write_po(target_file, target,
no_location=True, sort_output=True, ignore_obsolete=True)
target_file.close()
示例7: read_po
# 需要导入模块: from babel.messages import pofile [as 别名]
# 或者: from babel.messages.pofile import read_po [as 别名]
def read_po(file):
"""pofile.read_po() with a bug workaround.
pofile.read_po() produces an incorrect string if either msgid or msgstr
spans multiple lines and the first line content string is not empty e.g.
msgid "hoge "
"foo"
This function workarounds this bug by converting this to:
msgid ""
"hoge "
"foo"
"""
converted = ''
for line in file:
line = line.rstrip('\n')
m = re.search(r'^(msgid|msgstr) "(.+)"$', line)
if m:
converted += '%s ""\n' % m.group(1)
converted += '"%s"\n' % m.group(2)
else:
converted += '%s\n' % line
return pofile.read_po(StringIO.StringIO(converted))
示例8: validate_pofile
# 需要导入模块: from babel.messages import pofile [as 别名]
# 或者: from babel.messages.pofile import read_po [as 别名]
def validate_pofile(path: str) -> None:
with open(path, mode='rb+') as f:
catalog = pofile.read_po(f)
messages = list(catalog)
for message in messages:
if message.id and message.string:
validate_string(message, catalog)
f.seek(0)
f.truncate()
pofile.write_po(f, catalog)
示例9: load_po
# 需要导入模块: from babel.messages import pofile [as 别名]
# 或者: from babel.messages.pofile import read_po [as 别名]
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)
示例10: parse_po_file
# 需要导入模块: from babel.messages import pofile [as 别名]
# 或者: from babel.messages.pofile import read_po [as 别名]
def parse_po_file(in_file):
with open(in_file, "r") as in_fd:
catalog = read_po(in_fd)
return catalog
示例11: read_po_catalog
# 需要导入模块: from babel.messages import pofile [as 别名]
# 或者: from babel.messages.pofile import read_po [as 别名]
def read_po_catalog(filename: str) -> Catalog:
""" Try to read a po catalog from the given path.
Throw on failure.
"""
with open(filename, "r") as catalog_file:
return read_po(catalog_file)
示例12: merge_file
# 需要导入模块: from babel.messages import pofile [as 别名]
# 或者: from babel.messages.pofile import read_po [as 别名]
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)
示例13: update_po
# 需要导入模块: from babel.messages import pofile [as 别名]
# 或者: from babel.messages.pofile import read_po [as 别名]
def update_po(locale, new_msgids, header_comment):
"""Updates a .po file to contain the given set of messages and the given
header comment, and returns the number of missing translations."""
filename = get_po_filename(locale)
translations = read_po(open(filename))
# Remove unused messages.
for message in translations:
if message.id and message.id not in new_msgids:
del translations[message.id]
# Add new messages.
for id in new_msgids:
if id and id not in translations:
translations.add(id, '')
for message in translations:
# django_admin('compilemessages') fails if message.id starts/ends with
# '\n' but message.string doesn't, or vice versa.
# Fix message.string to make it consistent.
if message.string:
if (message.id.startswith(u'\n') and
not message.string.startswith(u'\n')):
message.string = u'\n' + message.string
if (not message.id.startswith(u'\n') and
message.string.startswith(u'\n')):
message.string = message.string.lstrip('\n')
if (message.id.endswith(u'\n') and
not message.string.endswith(u'\n')):
message.string = message.string + u'\n'
if (not message.id.endswith(u'\n') and
message.string.endswith(u'\n')):
message.string = message.string.rstrip('\n')
# .po file generated by the translation system sometimes has a flag
# "fuzzy". A translation with a flag "fuzzy" is not used by Django.
# We want to use such translations anyway.
if 'fuzzy' in message.flags:
message.flags.remove('fuzzy')
translations.header_comment = header_comment
write_clean_po(filename, translations)
return len([message for message in translations
if message.fuzzy or not message.string])