Prusa-Firmware/lang/lang-export.sh

215 lines
6.0 KiB
Bash
Raw Normal View History

2019-09-06 06:30:10 +00:00
#!/bin/bash
#
# Version 1.0.1 Build 18
2022-01-11 12:18:48 +00:00
#
# lang-export.sh - multi-language support script
# for generating lang_xx.po
#
2022-01-11 12:18:48 +00:00
#############################################################################
# Change log:
# 9 Nov. 2018, XPila, Initial
# 10 Dec. 2018, jhoblitt, make all shell scripts executable
2022-01-11 12:18:48 +00:00
# 14 Sep. 2019, 3d-gussner, Prepare adding new language
# 6 Sep. 2019, DRracer, change to bash
2022-01-11 12:18:48 +00:00
# 1 Mar. 2019, 3d-gussner, Move `Dutch` language parts
# Add templates for future community languages
# 17 Dec. 2021, 3d-gussner, Use one config file for all languages
# Fix missing last translation
# 21 Dec. 2021, 3d-gussner, Add Swedish, Danish, Slovanian, Hungarian,
# Luxembourgish, Croatian
# 3 Jan. 2022, 3d-gussner, Add Lithuanian
# Cleanup outaded code
2022-01-11 12:18:48 +00:00
# 11 Jan. 2022, 3d-gussner, Added version and Change log
# colored output
# Add Community language support
# Use `git rev-list --count HEAD lang-export.sh`
# to get Build Nr
# 25 Jan. 2022, 3d-gussner, Replace German HD44780 A00 ROM 'äöüß' to UTF-8 'äöüß'
2022-01-11 12:18:48 +00:00
#############################################################################
# Config:
if [ -z "$CONFIG_OK" ]; then eval "$(cat config.sh)"; fi
2022-01-11 12:18:48 +00:00
if [ -z "$CONFIG_OK" ] | [ $CONFIG_OK -eq 0 ]; then echo "$(tput setaf 1)Config NG!$(tput sgr 0)" >&2; exit 1; fi
echo "$(tput setaf 2)lang-export.sh started$(tput sgr 0)" >&2
if [ ! -z "$COMMUNITY_LANGUAGES" ]; then
LANGUAGES+=" $COMMUNITY_LANGUAGES"
fi
2022-01-11 12:18:48 +00:00
echo "$(tput setaf 2)lang-export languages:$LANGUAGES$(tput sgr 0)" >&2
# relative path to source folder
SRCDIR="../Firmware"
# selected language is 1st argument (cz, de, ...)
LNG=$1
# if no arguments, 'all' is selected (all po and also pot will be generated)
if [ -z "$LNG" ]; then LNG=all; fi
# if 'all' is selected, script will generate all po files and also pot file
if [ "$LNG" = "all" ]; then
./lang-export.sh en
for lang in $LANGUAGES; do
./lang-export.sh $lang
done
exit 0
fi
# language code (iso639-1) is equal to LNG
LNGISO=$LNG
# exception for 'cz' (code='cs')
if [ "$LNG" = "cz" ]; then LNGISO=cs; fi
# po/pot creation/revision date
DATE=$(date)
# if 'en' is selected, generate pot instead of po
if [ "$LNG" = "en" ]; then
INFILE=lang_en.txt
OUTFILE=po/Firmware.pot
LNGNAME="English"
else
# language name in english
LNGNAME=$(\
case "$LNG" in
*cz*) echo "Czech" ;;
*de*) echo "German" ;;
*es*) echo "Spanish" ;;
*fr*) echo "French" ;;
2021-03-01 07:39:26 +00:00
*it*) echo "Italian" ;;
*pl*) echo "Polish" ;;
#Community language support
#Dutch
*nl*) echo "Dutch" ;;
2021-12-21 13:33:35 +00:00
#Swedish
*sv*) echo "Swedish" ;;
2021-12-21 14:15:23 +00:00
#Danish
*da*) echo "Danish" ;;
2022-02-10 18:01:04 +00:00
#Slovak
2022-02-11 11:50:28 +00:00
*sk*) echo "Slovak" ;;
2021-12-21 14:24:06 +00:00
#Slovanian
*sl*) echo "Slovanian" ;;
2021-12-21 14:28:33 +00:00
#Hugarian
*hu*) echo "Hugarian" ;;
2021-12-21 14:32:54 +00:00
#Luxembourgish
*lb*) echo "Luxembourgish" ;;
2021-12-21 14:36:34 +00:00
#Croatian
*hr*) echo "Croatian" ;;
2022-01-03 14:00:45 +00:00
#Lithuanian
*lt*) echo "Lithuanian" ;;
2022-01-12 09:09:01 +00:00
#Romanian
*ro*) echo "Romanian" ;;
#Use the 2 lines below as a template and replace 'qr' and 'New language'
##New language
# *qr*) echo "New language" ;;
esac)
# unknown language - error
if [ -z "LNGNAME" ]; then
2022-01-11 12:18:48 +00:00
echo "Invalid argument $(tput setaf 1)'$LNG'$(tput sgr 0).">&2
exit 1
fi
INFILE=lang_en_$LNG.txt
OUTFILE=po/Firmware_$LNGISO.po
fi
# remove output file if exists
if [ -e $OUTFILE ]; then rm -f -v $OUTFILE; fi
#total strings
CNTTXT=$(grep '^#' -c $INFILE)
#not translated strings
CNTNT=$(grep '^\"\\x00\"' -c $INFILE)
2022-01-11 12:18:48 +00:00
echo " $(tput setaf 2)$CNTTXT$(tput sgr 0) texts, $(tput setaf 3)$CNTNT$(tput sgr 0) not translated" >&2
# list .cpp, .c and .h files from source folder
SRCFILES=$(ls "$SRCDIR"/*.cpp "$SRCDIR"/*.c "$SRCDIR"/*.h)
2022-01-11 12:18:48 +00:00
echo " selected language=$(tput setaf 2)$LNGNAME$(tput sgr 0)" >&2
# write po/pot header
(
echo "# Translation of Prusa-Firmware into $LNGNAME."
echo "#"
echo 'msgid ""'
echo 'msgstr ""'
echo '"MIME-Version: 1.0\n"'
echo '"Content-Type: text/plain; charset=UTF-8\n"'
echo '"Content-Transfer-Encoding: 8bit\n"'
echo '"Language: '$LNGISO'\n"'
echo '"Project-Id-Version: Prusa-Firmware\n"'
echo '"POT-Creation-Date: '$DATE'\n"'
echo '"PO-Revision-Date: '$DATE'\n"'
echo '"Language-Team: \n"'
echo '"X-Generator: Poedit 2.0.7\n"'
echo '"X-Poedit-SourceCharset: UTF-8\n"'
echo '"Last-Translator: \n"'
echo '"Plural-Forms: nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;\n"'
echo
) >$OUTFILE
#loop over all messages
s0=''
s1=''
s2=''
num=1
(cat $INFILE | sed "s/\\\\/\\\\\\\\/g" | while read -r s; do
#start debug
#if [ "${s:0:1}" = "\"" ]; then
# echo >&2
# echo "s = $s ." >&2
# echo "s0 = $s0 ." >&2
# echo "s1 = $s1 ." >&2
#fi
#end debug
if [ "${s:0:1}" = "\"" ]; then
if [[ "${s0:0:1}" = "\"" || "$LNG" = "en" ]]; then
2022-01-11 12:18:48 +00:00
echo -ne " processing $num of $CNTTXT\033[0K\r" >&2
# write po/pot item
(
if [ "$LNG" = "en" ]; then s1=$s0; s0=$s; fi
search=$(/bin/echo -e "$s0")
found=$(grep -m1 -n -F "$search" $SRCFILES | head -n1 | cut -f1-2 -d':' | sed "s/^.*\///")
echo "$s1" | sed 's/ c=0//;s/ r=0//;s/^#/# /'
#echo "$s1" | sed 's/ c=0//;s/ r=0//;s/^#/# /' >&2
echo "#: $found"
#echo "#: $found" >&2
/bin/echo -e "msgid $s0"
if [[ "$s" = "\"\\\\x00\"" || "$LNG" = "en" ]]; then
echo 'msgstr ""'
else
/bin/echo -e "msgstr $s"
fi
echo
)
num=$((num+1))
fi
fi
s1=$s0
s0=$s
done >>$OUTFILE) 2>&1
#replace LF with CRLF
sync
sed -i 's/$/\r/' $OUTFILE
#replace HD44780 A00 'äöüß' to UTF-8 'äöüß'
if [ "$LNG" = "de" ]; then
#replace 'A00 ROM ä' with 'ä'
sed -i 's/\\xe1/\xc3\xa4/g' $OUTFILE
#replace 'A00 ROM ü' with 'ü'
sed -i 's/\\xf5/\xc3\xbc/g' $OUTFILE
#replace 'A00 ROM ö' with 'ö'
sed -i 's/\\xef/\xc3\xb6/g' $OUTFILE
#replace 'A00 ROM ß' with 'ß'
sed -i 's/\\xe2/\xc3\x9f/g' $OUTFILE
fi
#replace HD44780 A00 'μ' to UTF-8 'μ'
#replace 'A00 ROMμ' with ' μ'
sed -i 's/\\xe4/\xce\xbc/g' $OUTFILE
2022-01-11 12:18:48 +00:00
echo >&2
echo "$(tput setaf 2)lang-export.sh finished$(tput sgr 0)">&2
exit 0