codekasten/translate-workflows/java_properties_converter.sh

150 lines
4.3 KiB
Bash
Raw Normal View History

#!/bin/sh
#
# convert a directory or java property files into po files and back
#
# The content of (translated) po files overrides the content of
# java properties files. Thus it is recommended to use a po file
# editor (e.g. Pootle) as the single primary source of translations.
#
# Java property template files are handled (converted into pot) as well.
#
# This is just the example script used for jsymphonic.
#
# Copyright 2009 - Lars Kruse (devel@sumpfralle.de)
#
# License:
#
# This package 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 package 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 package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
set -eu
PROP_DIR="$(dirname $0)/jsymphonic.properties.d"
PO_DIR="$(dirname $0)/jsymphonic.po.d"
POOTLE_DIR=/var/lib/pootle/jsymphonic
TEMP_DIR="$(dirname $0)/temp"
PROP_EXT=".properties"
PROP_LANG_SUFFIX_REGEX="_[a-z]{2}_[A-Z]{2}"
POT_EXT=".pot"
PO_EXT=".po"
POTOOLS_OPTS="--progress none"
get_all_prop_templates() {
(
cd "$PROP_DIR"
find -type f -name "*$PROP_EXT" | grep -vE ".*$PROP_LANG_SUFFIX_REGEX$PROP_EXT$"
)
}
get_translated_properties() {
local prop_template="$1"
(
cd "$PROP_DIR"
find -type f -name "*$PROP_EXT" | grep -E "${prop_template%$PROP_EXT}$PROP_LANG_SUFFIX_REGEX$PROP_EXT$"
)
}
template_prop2po() {
mkdir -p "$PO_DIR"
prop2po $POTOOLS_OPTS -P "$PROP_DIR/$1" "$PO_DIR/${1%$PROP_EXT}$POT_EXT"
}
# input: filename of a translated properties file
# output: filename of the properties template file
get_prop_template() {
echo -n "${1%$PROP_EXT}" | sed -r "s/$PROP_LANG_SUFFIX_REGEX$//g"
echo "$PROP_EXT"
}
# input: filename of a translated properties file
# output: filename of the corresponding pot template file
get_pot_template() {
echo -n "${1%$PROP_EXT}" | sed -r "s/$PROP_LANG_SUFFIX_REGEX$//g"
echo "$POT_EXT"
}
# input: filename of a translated properties file
# output: filename of the corresponding translated po file
get_po_filename() {
echo "${1%$PROP_EXT}$PO_EXT"
}
convert_to_po_merged() {
local prop_file="$PROP_DIR/$1"
local prop_template="$PROP_DIR/$(get_prop_template "$1")"
local pot_file="$PO_DIR/$(get_pot_template "$1")"
local po_file="$PO_DIR/$(get_po_filename "$1")"
local temp_po_file1="$TEMP_DIR/$(get_po_filename "$1")_1.$PO_EXT"
local temp_po_file2="$TEMP_DIR/$(get_po_filename "$1")_2.$PO_EXT"
mkdir -p "$TEMP_DIR"
if test -f "$po_file"; then
# merge the prop with the po file (priority: po file)
prop2po $POTOOLS_OPTS -t "$prop_template" -i "$prop_file" -o "$temp_po_file1"
pomerge $POTOOLS_OPTS --mergeblanks=no -t "$po_file" -i "$temp_po_file1" -o "$temp_po_file2"
mv "$temp_po_file2" "$po_file"
rm "$temp_po_file1"
else prop2po $POTOOLS_OPTS -t "$prop_template" -i "$prop_file" -o "$po_file"
fi
}
# convert a po file to a prop file
convert_po_to_prop() {
local prop_file="$PROP_DIR/$1"
local prop_template="$PROP_DIR/$(get_prop_template "$1")"
local po_file="$PO_DIR/$(get_po_filename "$1")"
po2prop $POTOOLS_OPTS -t "$prop_template" -i "$po_file" -o "$prop_file"
}
get_lang_code() {
echo "$1" | sed -r "s/^.*($PROP_LANG_SUFFIX_REGEX).*$/\1/" | sed 's/^_//' | sed 's/_$//'
}
create_symlink_po() {
local po_file="$PO_DIR/$(get_po_filename "$1")"
local lang_code="$(get_lang_code "$1")"
mkdir -p "$POOTLE_DIR/$lang_code"
ln -sfn "$(readlink -f "$po_file")" "$POOTLE_DIR/$lang_code/"
}
create_symlink_pot() {
local pot_file="$PO_DIR/$(get_pot_template "$1")"
ln -sfn "$(readlink -f "$pot_file")" "$POOTLE_DIR/"
}
# *************** do the conversion ***********************
get_all_prop_templates | while read prop; do
template_prop2po "$prop"
create_symlink_pot "$prop"
get_translated_properties "$prop" | while read prop_file; do
convert_to_po_merged "$prop_file"
convert_po_to_prop "$prop_file"
create_symlink_po "$prop_file"
done
done