Verified Commit 3cd6c654 authored by Štěpán Henek's avatar Štěpán Henek 🌩

make translations via babel + translation compile fix

parent 2d386aca
...@@ -47,4 +47,8 @@ clean: ...@@ -47,4 +47,8 @@ clean:
help: help:
@egrep "^# target:" Makefile @egrep "^# target:" Makefile
.PHONY: all branding sass localization messages:
./ extract_messages --no-location -o foris/locale/foris.pot -F babel.cfg
./ update_catalog -D foris -i foris/locale/foris.pot -d foris/locale/
.PHONY: all branding sass messages
[jinja2: **/templates/**.j2]
encoding = utf-8
[python: **.py]
#!/usr/bin/env python #!/usr/bin/env python
import os import os
import glob
import re
from setuptools import setup from setuptools import setup
from setuptools.command.build_py import build_py from setuptools.command.build_py import build_py
...@@ -8,13 +10,52 @@ from setuptools.command.build_py import build_py ...@@ -8,13 +10,52 @@ from setuptools.command.build_py import build_py
from foris import __version__ from foris import __version__
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
def merge_po_files():
from babel.messages.pofile import read_po, write_po
trans_dirs = glob.glob(os.path.join(BASE_DIR, "foris/locale/*/LC_MESSAGES/"))
# iterate through translations
for trans_dir in trans_dirs:
locale = re.match(r".*\/([a-zA-Z_]+)\/LC_MESSAGES/", trans_dir).group(1)
foris_path = os.path.join(trans_dir, "foris.po")
if not os.path.exists(foris_path):
# read foris translations
with open(foris_path) as f:
foris_catalog = read_po(f, locale=locale, domain="messages")
# read tzinfo translations
tzinfo_path = os.path.join(trans_dir, "tzinfo.po")
if os.path.exists(tzinfo_path):
with open(tzinfo_path) as f:
tzinfo_catalog = read_po(f, locale=locale, domain="messages")
for msg in tzinfo_catalog:
# foris messages will be preffered
if not in foris_catalog:
# append to foris messages
foris_catalog[] = msg
# write merged catalogs
target_path = os.path.join(trans_dir, "messages.po")
with open(target_path, "w") as f:
write_po(f, foris_catalog, no_location=True)
class BuildCmd(build_py): class BuildCmd(build_py):
def run(self): def run(self):
# prepare messages.po
# compile translation # compile translation
from babel.messages import frontend as babel from babel.messages import frontend as babel
cmd = babel.compile_catalog() cmd = babel.compile_catalog()
cmd.initialize_options() cmd.initialize_options() = os.path.join(os.path.dirname(__file__), "foris", "locale") = os.path.join(os.path.dirname(__file__), "foris", "locale")
cmd.domain = "messages"
cmd.finalize_options() cmd.finalize_options()
...@@ -36,6 +77,7 @@ setup( ...@@ -36,6 +77,7 @@ setup(
], ],
setup_requires=[ setup_requires=[
'babel', 'babel',
], ],
provides=[ provides=[
"foris" "foris"
# Foris - web administration interface for OpenWrt based on NETCONF
# Copyright (C) 2013 CZ.NIC, z.s.p.o. <>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <>.
set -e
function make_pot() {
local path="$1"
local package="$2"
local output="$3"
echo "Creating/overriding pot file in '$output'."
find "$path" \( -iname "*.py" -o -iname "*.tpl" \) -exec cat {} \; | sed 's/"\?{{!* \(_\|trans\|\(\w*\)gettext\)\((".*")\|(''.*'')\).*}}"\?/\n\2gettext\3\n#/g' | \
xgettext --package-name="$package" -d messages --no-location --language=Python --from-code=UTF-8 --output="$output" -
find $1 -iname "*.tpl" -exec sh -c 'grep -zq "%include.*_(" $0 && echo "WARNING: _() after %include in $0"' {} \;
function make_po() {
local locale_dir="$1"
local pot_path="$2"
local lang="$3"
if [ -f $locale_dir/foris.po ]
echo "Merging messages in '$locale_dir'."
msgmerge -q -U "$locale_dir"/foris.po "$pot_path"
echo "Creating messages in '$locale_dir'."
msginit -i "$pot_path" -l $lang --no-translator -o "$locale_dir"/foris.po
function make_messages() {
local path="$1"
local package="$2"
local lang="$3"
local locale_dir="$path/locale/$lang/LC_MESSAGES"
local pot_path="$path/locale/foris.pot"
[ -d "$locale_dir" ] || mkdir -p "$locale_dir"
make_pot "$path" "$package" "$pot_path"
make_po "$locale_dir" "$pot_path" "$lang"
if [ $# -eq 3 ]; then
make_messages "$1" "${2:-Foris}" "$3"
echo "Message making completed."
elif [ $# -lt 3 -a $# -gt 0 ]; then
for dir in "$1"/locale/?? ; do
make_messages "$1" "${2:-Foris}" "$(basename "$dir")"
echo "Usage: $0 PATH [PACKAGE [LANGUAGE]] "
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment