Skip to content

Commit

Permalink
Ignore 'Language' header in PO-templates
Browse files Browse the repository at this point in the history
The tools 'xgettext' from GNU gettext and 'pot-create' from lingua
both produce PO-template files containing a 'Language' header with
an empty string as value.

Babel has issues creating catalogs out of such templates.

It wants to parse the PO-template as a normal PO-catalog using the
locale of the target catalog, because it helps producing the correct
plurals. But it overwrites this locale when parsing the 'Language'
header and then it can not produce a valid catalog.

Ignoring the 'Language' header when it is known that the file being
processed is a template avoids this issue.

GitHub: #665
  • Loading branch information
sinoroc committed Oct 7, 2019
1 parent 2abad80 commit e769fd2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
6 changes: 4 additions & 2 deletions babel/messages/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def __init__(self, locale=None, domain=None, header_comment=DEFAULT_HEADER,
project=None, version=None, copyright_holder=None,
msgid_bugs_address=None, creation_date=None,
revision_date=None, last_translator=None, language_team=None,
charset=None, fuzzy=True):
charset=None, fuzzy=True, is_template=False):
"""Initialize the catalog object.
:param locale: the locale identifier or `Locale` object, or `None`
Expand Down Expand Up @@ -300,6 +300,8 @@ def __init__(self, locale=None, domain=None, header_comment=DEFAULT_HEADER,
self._num_plurals = None
self._plural_expr = None

self._is_template = is_template

def _set_locale(self, locale):
if locale is None:
self._locale_identifier = None
Expand Down Expand Up @@ -424,7 +426,7 @@ def _set_mime_headers(self, headers):
self.msgid_bugs_address = value
elif name == 'last-translator':
self.last_translator = value
elif name == 'language':
elif name == 'language' and not self._is_template:
value = value.replace('-', '_')
self._set_locale(value)
elif name == 'language-team':
Expand Down
4 changes: 2 additions & 2 deletions babel/messages/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ def run(self):
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 = read_po(infile, locale=self.locale, is_template=True)

catalog.locale = self._locale
catalog.revision_date = datetime.now(LOCALTZ)
Expand Down Expand Up @@ -738,7 +738,7 @@ def run(self):
domain = os.path.splitext(os.path.basename(self.input_file))[0]

with open(self.input_file, 'rb') as infile:
template = read_po(infile)
template = read_po(infile, is_template=True)

for locale, filename in po_files:
self.log.info('updating catalog %s based on %s', filename, self.input_file)
Expand Down
6 changes: 4 additions & 2 deletions babel/messages/pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ def _invalid_pofile(self, line, lineno, msg):
print(u"WARNING: Problem on line {0}: {1}".format(lineno + 1, line))


def read_po(fileobj, locale=None, domain=None, ignore_obsolete=False, charset=None, abort_invalid=False):
def read_po(fileobj, locale=None, domain=None, ignore_obsolete=False,
charset=None, abort_invalid=False, is_template=False):
"""Read messages from a ``gettext`` PO (portable object) file from the given
file-like object and return a `Catalog`.
Expand Down Expand Up @@ -372,7 +373,8 @@ def read_po(fileobj, locale=None, domain=None, ignore_obsolete=False, charset=No
:param charset: the character set of the catalog.
:param abort_invalid: abort read if po file is invalid
"""
catalog = Catalog(locale=locale, domain=domain, charset=charset)
catalog = Catalog(locale=locale, domain=domain, charset=charset,
is_template=is_template)
parser = PoFileParser(catalog, ignore_obsolete, abort_invalid=abort_invalid)
parser.parse(fileobj)
return catalog
Expand Down
1 change: 1 addition & 0 deletions tests/messages/data/project/i18n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ msgstr ""
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: Non existent language\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
Expand Down
1 change: 1 addition & 0 deletions tests/messages/data/project/i18n/messages_non_fuzzy.pot
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ msgstr ""
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
Expand Down

0 comments on commit e769fd2

Please sign in to comment.