Skip to content

Instantly share code, notes, and snippets.

@Ereza
Last active March 26, 2020 10:05
Show Gist options
  • Save Ereza/1ba16fd0c22eaba55f5a9063b0182d05 to your computer and use it in GitHub Desktop.
Save Ereza/1ba16fd0c22eaba55f5a9063b0182d05 to your computer and use it in GitHub Desktop.
Werewolf for Telegram export/import to CSV for Weblate
#!/bin/bash
insidecomment=0
insidevalue=0
insidedeprecated=0
while IFS= read -r line
do
case "$line" in
*\<\?xml*)
#ignore
;;
*\<strings\>*)
#ignore
;;
*\</strings\>*)
#ignore
;;
*\<language*)
#ignore
;;
*\<!--*--\>*)
if [ $insidedeprecated -eq 0 ]
then
comment=`echo $line | sed -E 's/<!--//g' | sed -E 's/-->//g'`
if [ "$currentcom" == "" ]
then
currentcom=$comment
else
currentcom="$currentcom
$comment"
fi
fi
;;
*\<!--*)
if [ $insidedeprecated -eq 0 ]
then
comment=`echo $line | sed -E 's/<!--//g'`
if [ "$currentcom" == "" ]
then
currentcom=$comment
else
currentcom="$currentcom
$comment"
fi
insidecomment=1
fi
;;
*--\>*)
if [ $insidedeprecated -eq 0 ]
then
comment=`echo $line | sed -E 's/-->//g'`
if [ "$currentcom" == "" ]
then
currentcom=$comment
else
currentcom="$currentcom
$comment"
fi
insidecomment=0
fi
;;
*\</string\>*)
currentkey=""
currentcom=""
currentval=""
insidedeprecated=0
#echo "current key: $currentkey"
;;
*\<string*)
currentkey="`echo "$line" | sed -E 's/.* key ?="([^"]*)".*/\1/g'`"
if [[ "$line" == *"isgif="* ]]
then
currentkey="$currentkey:isgif"
fi
if [[ "$line" == *"deprecated="* ]]
then
insidedeprecated=1
echo "Skipping deprecated key $currentkey"
fi
i=0
#echo "current key: $currentkey"
;;
*\<value*\>*\</value\>*)
if [ $insidedeprecated -eq 0 ]
then
currentval=`echo "$line" | sed -E 's/.*<value ?>//g' | sed -E 's/<\/value>.*//g'`
#echo "\"$currentkey:$i\",\"${currentval//\"/\"\"}\",\"${currentcom//\"/\"\"}\"" >> en.csv
echo "\"$currentkey:$i\",\"${currentval//\"/\"\"}\"" >> en.csv
i=$((i+1))
fi
;;
*\<value*\>*)
if [ $insidedeprecated -eq 0 ]
then
currentval=`echo "$line" | sed -E 's/.*<value ?>//g'`
insidevalue=1
fi
;;
*\</value\>*)
if [ $insidedeprecated -eq 0 ]
then
newval=`echo "$line" | sed -E 's/<\/value>.*//g'`
currentval="$currentval
$newval"
insidevalue=0
#echo "\"$currentkey:$i\",\"${currentval//\"/\"\"}\",\"${currentcom//\"/\"\"}\"" >> en.csv
echo "\"$currentkey:$i\",\"${currentval//\"/\"\"}\"" >> en.csv
i=$((i+1))
fi
;;
*):
if [ $insidevalue -eq 1 ]
then
currentval="$currentval
$line"
elif [ $insidecomment -eq 1 ]
then
currentcom="$currentcom
$line"
elif [ $insidedeprecated -eq 0 ]
then
echo "UNTREATED LINE: $line"
fi
;;
esac
done < English.xml
#!/bin/bash
insidecomment=0
insidevalue=0
echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" > Catalan.xml
echo "<strings>" >> Catalan.xml
echo " <language name=\"Català\" base=\"Català\" variant=\"Estàndard\" owner=\"\" code=\"ca\"/>" >> Catalan.xml
rows=`csvtool height ca.csv`
i=1
while [ $i -le $rows ]
do
csvkey=`csvtool sub $i 1 1 1 ca.csv | csvtool format '%(1)' -`
key=`echo "$csvkey" | sed -E 's/:[0-9]//g'`
val=`csvtool sub $i 2 1 1 ca.csv | csvtool format '%(1)' -`
if [[ $csvkey == *":0"* ]]
then
if [ $i -gt 1 ]
then
echo " </string>" >> Catalan.xml
fi
if [[ "$key" == *":isgif"* ]]
then
echo " <string key=\"${key/:isgif/}\" isgif=\"true\">" >> Catalan.xml
else
echo " <string key=\"$key\">" >> Catalan.xml
fi
fi
echo " <value>$val</value>" >> Catalan.xml
i=$((i+1))
done
echo " </string>" >> Catalan.xml
echo "</strings>" >> Catalan.xml
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment