refs #3329: emit warnings if po file is invalid and can't read it. Also writing mo too.

This commit is contained in:
shimizukawa 2017-01-13 00:11:35 +09:00
parent 34dd31994d
commit 2254da3782
4 changed files with 16 additions and 7 deletions

View File

@ -258,7 +258,7 @@ class Sphinx(object):
for catinfo in find_catalog_source_files(
user_locale_dirs, self.config.language, domains=['sphinx'],
charset=self.config.source_encoding):
catinfo.write_mo(self.config.language)
catinfo.write_mo(self.config.language, self.warn)
locale_dirs = [None, path.join(package_dir, 'locale')] + user_locale_dirs
else:
locale_dirs = []

View File

@ -167,7 +167,7 @@ class Builder(object):
for catalog in self.app.status_iterator(
catalogs, 'writing output... ', darkgreen, len(catalogs),
cat2relpath):
catalog.write_mo(self.config.language)
catalog.write_mo(self.config.language, self.warn)
def compile_all_catalogs(self):
catalogs = i18n.find_catalog_source_files(

View File

@ -53,10 +53,19 @@ class CatalogInfo(LocaleFileInfoBase):
not path.exists(self.mo_path) or
path.getmtime(self.mo_path) < path.getmtime(self.po_path))
def write_mo(self, locale):
with io.open(self.po_path, 'rt', encoding=self.charset) as po:
with io.open(self.mo_path, 'wb') as mo:
write_mo(mo, read_po(po, locale))
def write_mo(self, locale, warnfunc):
with io.open(self.po_path, 'rt', encoding=self.charset) as file_po:
try:
po = read_po(file_po, locale)
except Exception:
warnfunc('reading error: %s' % self.po_path)
return
with io.open(self.mo_path, 'wb') as file_mo:
try:
write_mo(file_mo, po)
except Exception:
warnfunc('writing error: %s' % self.mo_path)
def find_catalog(docname, compaction):

View File

@ -51,7 +51,7 @@ def test_catalog_outdated(tempdir):
def test_catalog_write_mo(tempdir):
(tempdir / 'test.po').write_text('#')
cat = i18n.CatalogInfo(tempdir, 'test', 'utf-8')
cat.write_mo('en')
cat.write_mo('en', lambda *a, *kw: None)
assert os.path.exists(cat.mo_path)
with open(cat.mo_path, 'rb') as f:
assert read_mo(f) is not None