Skip to content

Commit

Permalink
modify i18n extraction to flag messages properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco committed Aug 31, 2016
1 parent 1b4ce94 commit 2c1a0a5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pytest-re: env

_i18n_extract: env
@PYTHONPATH=. $(env_bin)/pybabel extract -F .babel_extract --no-wrap -o i18n/core.pot emails liberapay templates www
@PYTHONPATH=. $(env_bin)/python liberapay/utils/i18n.py po-reflag i18n/core.pot
@for f in i18n/*/*.po; do \
$(env_bin)/pybabel update -i i18n/core.pot -l $$(basename -s '.po' "$$f") -o "$$f" --ignore-obsolete --no-fuzzy-matching --no-wrap; \
done
Expand All @@ -85,6 +86,7 @@ _i18n_clean:
sed -E -e '/^"(POT?-[^-]+-Date|Last-Translator|X-Generator|Language): /d' \
-e 's/^("[^:]+: ) +/\1/' \
-e 's/^("Language-Team: .+? )<(.+)>\\n/\1"\n"<\2>\\n/' \
-e 's/^#(, .+)?, python-format(, .+)?$$/#\1\2/' \
-e '/^#: /d' "$$f" >"$$f.new"; \
mv "$$f.new" "$$f"; \
done
Expand Down
32 changes: 32 additions & 0 deletions liberapay/utils/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,35 @@ def extract_spt(fileobj, *args, **kw):
if extractor:
for match in extractor(f, *args, **kw):
yield match


if __name__ == '__main__':
import sys

from babel.messages.catalog import Message
from babel.messages.pofile import read_po, write_po

if sys.argv[1] == 'po-reflag':
# This adds the `python-brace-format` flag to messages that contain braces
# https://github.com/python-babel/babel/issues/333
pot_path = sys.argv[2]
print('rewriting PO template file', pot_path)
# read PO file
with open(pot_path, 'rb') as pot:
catalog = read_po(pot)
# tweak message flags
for m in catalog:
msg = m.id
contains_brace = any(
'{' in s for s in (msg if isinstance(msg, tuple) else (msg,))
)
if contains_brace:
m.flags.add('python-brace-format')
m.flags.discard('python-format')
# write back
with open(pot_path, 'wb') as pot:
write_po(pot, catalog, width=0)

else:
print("unknown command")
raise SystemExit(1)

0 comments on commit 2c1a0a5

Please sign in to comment.