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:
help:
@egrep "^# target:" Makefile
.PHONY: all branding sass localization
messages:
./setup.py extract_messages --no-location -o foris/locale/foris.pot -F babel.cfg
./setup.py 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
import os
import glob
import re
from setuptools import setup
from setuptools.command.build_py import build_py
......@@ -8,13 +10,52 @@ from setuptools.command.build_py import build_py
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):
continue
# 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 msg.id not in foris_catalog:
# append to foris messages
foris_catalog[msg.id] = 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):
def run(self):
# prepare messages.po
merge_po_files()
# compile translation
from babel.messages import frontend as babel
cmd = babel.compile_catalog()
cmd.initialize_options()
cmd.directory = os.path.join(os.path.dirname(__file__), "foris", "locale")
cmd.domain = "messages"
cmd.finalize_options()
cmd.run()
......@@ -36,6 +77,7 @@ setup(
],
setup_requires=[
'babel',
'jinja2',
],
provides=[
"foris"
......
#!/bin/bash
# Foris - web administration interface for OpenWrt based on NETCONF
# Copyright (C) 2013 CZ.NIC, z.s.p.o. <http://www.nic.cz>
#
# 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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# 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 <http://www.gnu.org/licenses/>.
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 ]
then
echo "Merging messages in '$locale_dir'."
msgmerge -q -U "$locale_dir"/foris.po "$pot_path"
else
echo "Creating messages in '$locale_dir'."
msginit -i "$pot_path" -l $lang --no-translator -o "$locale_dir"/foris.po
fi
}
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")"
done
else
echo "Usage: $0 PATH [PACKAGE [LANGUAGE]] "
fi
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