babel icon indicating copy to clipboard operation
babel copied to clipboard

Can not initialize a catalog from a template with an empty string value for "Language" header

Open sinoroc opened this issue 6 years ago • 2 comments

GNU gettext's xgettext and lingua's pot-create both create PO-template files containing a Language header with an empty string as its value ("Language: \n").

Babel does not seem to be able to handle this empty header and produces the error and stacktrace below.

Removing the Language header from the template file allows Babel to initialize the catalogs without issue.

  • Babel 2.7.0
  • lingua 4.13
  • xgettext 0.19.8.1
  • Python 3.6.8
python3 setup.py init_catalog --domain project --input src/project/locale/project.pot --output-file src/project/locale/en/LC_MESSAGES/project.po --locale en
running init_catalog
creating catalog src/project/locale/en/LC_MESSAGES/project.po based on src/project/locale/project.pot
Traceback (most recent call last):
  File "setup.py", line 35, in <module>
    _do_setup()
  File "setup.py", line 30, in _do_setup
    version=version,
  File "/home/sinoroc/workspace/project/.tox/develop/lib/python3.6/site-packages/setuptools/__init__.py", line 145, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib/python3.6/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib/python3.6/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/home/sinoroc/workspace/project/.tox/develop/lib/python3.6/site-packages/babel/messages/frontend.py", line 622, in run
    catalog = read_po(infile, locale=self.locale)
  File "/home/sinoroc/workspace/project/.tox/develop/lib/python3.6/site-packages/babel/messages/pofile.py", line 377, in read_po
    parser.parse(fileobj)
  File "/home/sinoroc/workspace/project/.tox/develop/lib/python3.6/site-packages/babel/messages/pofile.py", line 308, in parse
    self._process_comment(line)
  File "/home/sinoroc/workspace/project/.tox/develop/lib/python3.6/site-packages/babel/messages/pofile.py", line 267, in _process_comment
    self._finish_current_message()
  File "/home/sinoroc/workspace/project/.tox/develop/lib/python3.6/site-packages/babel/messages/pofile.py", line 204, in _finish_current_message
    self._add_message()
  File "/home/sinoroc/workspace/project/.tox/develop/lib/python3.6/site-packages/babel/messages/pofile.py", line 198, in _add_message
    self.catalog[msgid] = message
  File "/home/sinoroc/workspace/project/.tox/develop/lib/python3.6/site-packages/babel/messages/catalog.py", line 629, in __setitem__
    self.mime_headers = _parse_header(message.string).items()
  File "/home/sinoroc/workspace/project/.tox/develop/lib/python3.6/site-packages/babel/messages/catalog.py", line 430, in _set_mime_headers
    self._set_locale(value)
  File "/home/sinoroc/workspace/project/.tox/develop/lib/python3.6/site-packages/babel/messages/catalog.py", line 318, in _set_locale
    self._locale = Locale.parse(locale)
  File "/home/sinoroc/workspace/project/.tox/develop/lib/python3.6/site-packages/babel/core.py", line 268, in parse
    parts = parse_locale(identifier, sep=sep)
  File "/home/sinoroc/workspace/project/.tox/develop/lib/python3.6/site-packages/babel/core.py", line 1094, in parse_locale
    raise ValueError('expected only letters, got %r' % lang)
ValueError: expected only letters, got ''

sinoroc avatar Oct 07 '19 14:10 sinoroc

Looks like the Catalog used to represent the PO-template gets its locale rewritten once the parser reaches the Language header. An easy fix would be to simply ignore this header if a template is being processed.

sinoroc avatar Oct 07 '19 16:10 sinoroc

This is a bit puzzling...

On one side: https://github.com/python-babel/babel/blob/2abad80c9f2fc70bde71f53ab8270ca950100595/babel/messages/frontend.py#L619-L622

On the other: https://github.com/python-babel/babel/blob/2abad80c9f2fc70bde71f53ab8270ca950100595/babel/messages/pofile.py#L328-L369 and https://github.com/python-babel/babel/blob/2abad80c9f2fc70bde71f53ab8270ca950100595/babel/messages/catalog.py#L242-L254

sinoroc avatar Oct 07 '19 17:10 sinoroc