bug in "django-admin.py makemessages" or xgettext call? -> "warning: unterminated string"
Asked Answered
A

1

0

django-admin.py makemessages dies with errors "warning: unterminated string" on cases where really long strings are wrapped:

string = "some text \
          more text\
          and even more"

These strings don't even need to be translated - e.g. sql query strings. The problem goes away when I concatenate the string, but the result looks ugly and it takes time to join them...

Does anyone have a problem like this? Have you found a way to fix it?

I have the following versions of the tools involved:

xgettext-0.17, gettext-0.17, django-1.0.2, python-2.6.2

There was a ticket on this issue, but it was closed probably because the error appears only in some combination of component versions.

EDIT: found the source of problem - xgettext prints warning messages to sterr and django takes them as fatal errors and quits.

return status of xgettext call is 0 - "success". I guess that django should recognize it as success and not quit because of warnings.

Interestinly xgettext still extracts backslash-wrapped strings if they need to be translated, but gives warnings in stderr ("unterminated string") and .po file ("internationalized messages should not contain the `\r' escape sequence")

xgettext call is the following:

xgettext -d django -L Python --keyword=gettext_noop \
         --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 \
         --keyword=ugettext_noop --keyword=ugettext_lazy \
         --keyword=ungettext_lazy:1,2 
         --from-code UTF-8 -o - source_file.py

called from django/core/management/commands/makemessages.py

Ardeen answered 19/6, 2009 at 23:16 Comment(0)
B
2

I can think of two possibilities: you might have an extra space after your backslash at the end of the line; or you might be somehow ending up with the wrong line-ending characters in your source (e.g. Windows-style when your Python is expecting Unix-style, thus disabling the backslashes).

Either way, I would take advantage of C-style automatic string concatenation:

>>> string = ("some text "
...           "more text "
...           "and even more")
>>> string
'some text more text and even more'

Alternatively, if you don't mind newlines ending up in there, use multi-line strings:

>>> string = """some text
...             more text
...             and even more"""

IMO these look much nicer, and are much less fragile when refactoring.

Does this help?

Bookout answered 20/6, 2009 at 9:20 Comment(2)
@chrispy This helps, thank you. Just double-checked - I don't have space after backslash. It's either Django or xgettext not getting the backslash string wrapping.Ardeen
From your subsequent edit about \r, I'm pretty confident it's a line-ending issue. Try an editor which lets you specify Unix file format instead of Windows.Bookout

© 2022 - 2024 — McMap. All rights reserved.