Last active
June 1, 2018 13:05
-
-
Save iegik/2262949b95be865dfb6b87fa38c34d3e to your computer and use it in GitHub Desktop.
```
Usage: translate.sh [OPTION]... [FILE]... --template use template [ mustashe | csharp ] -o|--output Output path for {prefix}.json files -p|--prefix Replace pr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
declare -a List=(en it de es pt fr et fi) | |
USAGE=$(cat <<-EOM | |
Usage: ${0##*/} [OPTION]... [FILE]... | |
EOM | |
) | |
INFO=$(cat <<-EOM | |
Example: | |
${0} -t -a -o App_Data/Localization/client --template mustashe Content/scripts/**/*.js | |
Translates stdin in ${List[*]} languages and saves to output path as .json file: | |
{ | |
"{prefix}:{tag}": { | |
"{lang}": "{translated text}" | |
} | |
} | |
for future convertion with https://github.com/mikeedwards/po2json | |
EOM | |
) | |
save=0 | |
debug=0 | |
translate=0 | |
prefix="" | |
output="" | |
template="mustashe" | |
files=() | |
KEY="" | |
for i in "$@" ; do | |
case "$i" in | |
--template ) shift ; template=$1 ; shift ;; # use template [ mustashe | csharp ] | |
-o|--output ) shift ; output=$1 ; shift ;; # Output path for {prefix}.json files | |
-k|--key ) shift ; KEY=$1 ; shift ;; # Yandex API key | |
-p|--prefix ) shift ; prefix=$1 ; shift ;; # Replace prefix for tag | |
-t|--translate ) shift ; translate=1 ;; # Requests translation of the text for each language | |
-d|--debug ) shift ; debug=1 ;; # Switch to debug mode | |
-a|--apply ) shift ; save=1 ;; # Replace in affected files | |
-h ) echo "$USAGE" ; fgrep -h ')'' ' $0 | awk -F')'' | #'' ' '{printf "%-40s %s\n", $1, $3}' ; echo "$INFO" ; exit ; # Print this line | |
esac | |
done | |
if [ $debug -eq 1 ] ; then | |
echo "DEBUG MODE IS ENABLED" | |
echo "args: $@" | |
fi | |
# найти текстовые строки | trim | uniq | сортировать | |
TEXT=$(grep -hroP '([А-Яа-яЁё]+[\s\-\,\.\!\?\:«»\d\w\(\)]*)*' $@ | sed 's/^[ \t]*//gim' | uniq | awk '{ print length, $0 }' | sort -n -s -r | cut -d" " -f2- | grep -v '^$') | |
if [ $debug -eq 1 ] ; then | |
echo "TEXT: $TEXT" | |
echo "template: $template" | |
echo "output: $output" | |
echo "prefix: $prefix" | |
echo "translate: $translate" | |
echo "debug: $debug" | |
echo "apply: $save" | |
fi | |
if [ -z "$TEXT" ] ; then | |
echo "No text input found" | |
exit; | |
fi | |
while IFS= read -r LINE ; do | |
if [ ${#LINE} -lt 3 ] ; then | |
break; | |
fi | |
if [ $debug -eq 1 ] ; then | |
echo -e "LINE: $LINE" | |
fi | |
path=$(grep -rl "$LINE" $@ | awk -F: '{ print $1 }') | |
basename="${path##*/}" | |
filename="${basename%.*}" | |
if [ -z "$prefix" ] ; then | |
prefix="$filename" | |
fi | |
outfile="$output/$prefix.json" | |
files=("${files[@]}" "$outfile") | |
affected=$(grep -re "$LINE" $@ -l) | |
if [ $debug -eq 1 ] ; then | |
echo "path: $path" | |
echo "basename: $basename" | |
echo "filename: $filename" | |
echo "Affected files: $affected" | |
tag="..." | |
if [ $template = "mustashe" ] ; then | |
echo "$LINE {{$prefix:$tag}}" | |
elif [ $template = "csharp" ] ; then | |
echo "$LINE @L10n.Get(\"$prefix:$tag\")" | |
fi | |
fi | |
out="" | |
for lang in "${List[@]}"; do | |
if [ $translate -eq 1 ] ; then | |
translated=$(curl -s --data-urlencode "text=$LINE" "https://translate.yandex.net/api/v1.5/tr.json/translate?key=$KEY&lang=ru-$lang&format=plain" | python3 -c "import sys, json; print(json.load(sys.stdin)['text'][0])") | |
fi | |
if [ $lang = "en" ] ; then | |
tag="..." | |
if [ ! -e $outfile ] ; then | |
touch $outfile; | |
out+="{\n"; | |
else | |
out+=",\n"; | |
fi | |
if [ $translate -eq 1 ] ; then | |
tag=$(echo "$translated" | sed 's/\W//g') | |
fi | |
out+="\"$prefix:$tag\": {\n" | |
out+=" \"ru\": \"$LINE\",\n" | |
if [ $save -eq 1 ] ; then | |
if [ $template = "mustashe" ] ; then | |
sed -i "s/$LINE/{{$prefix:$tag}}/gm" $affected | |
elif [ $template = "csharp" ] ; then | |
sed -i "s/$LINE/@L10n.Get(\"$prefix:$tag\")/gm" $affected | |
fi | |
fi | |
fi | |
out+=" \"$lang\": \"$translated\"" | |
if [ $lang != "fi" ] ; then | |
out+="," | |
fi | |
out+="\n" | |
done | |
out+="}" | |
if [ $debug -eq 0 ] ; then | |
printf "$out" >> $outfile | |
else | |
echo "outfile: $outfile" | |
fi | |
done <<< "$(printf '%s\n' "$TEXT")"; | |
while read outfile ; do | |
if [ $debug -eq 0 ] ; then | |
printf "}\n" >> $outfile | |
fi | |
done <<< "$(echo -e $files)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment