Google Translate your .desktop files
Posted: Sun 21 Aug 2011, 13:14
This script will automatically Translate your .desktop files into the given language, using Google.
NOTE, translations will not be perfect, sometimes really bad, but it is a start.
Existing translations for the chosen language will not be replaced.
Save the code below into a script called 'dotdesktop_translate' and make it executable.
Move it to /usr/local/bin, or similar, then run the script through the terminal, like so:
LANG could be any Google supported language code: de, es, fr, ru, nl, jp, and so on.
NOTE, translations will not be perfect, sometimes really bad, but it is a start.
Existing translations for the chosen language will not be replaced.
Save the code below into a script called 'dotdesktop_translate' and make it executable.
Move it to /usr/local/bin, or similar, then run the script through the terminal, like so:
Code: Select all
dotdesktoptranslate LANG
Code: Select all
#!/bin/sh
[ "$1" = "" ] && echo "Usage: $(basename $0) LANG
LANG can be any language supported by Google translate:
es, de, fr, ru, nl, jp, etc" && exit
LOC=$1
DIR="/tmp/${LOC}"
[ -d ${DIR} ] || mkdir ${DIR}
for file in /usr/share/applications/*; do
name=""
name_loc=""
# get the english name
[ -f $file ] && name=$(cat $file | grep '^Name=')
# cut out anything google translate does not like
name=${name#*[}
name=${name%]*}
name=${name#*=}
# do not translate the word Puppy
name=${name/Puppy /xpupx }
name=${name/cli /console } # fix for other langs
name=${name/CLI /console } # fix for other langs
desc=`echo ${name#* }`
desc_lower=`echo $desc | tr A-Z a-z` # make lower case
[ "$desc" != "$name" ] && name=${name/$desc/$desc_lower} # make description lower case in name
# now check $file for translation we created
checkname=$(cat "$file" | grep "^Name\[$LOC\]")
if [ "$checkname" = "" ];then
echo "Please wait, translating '$(basename $file)' to $LOC..."
# translation not found, so get translation from Google, sleep a bit, or Google reports 'abuse of service' as a translation :(
sleep 4.${RANDOM}
name_loc=`wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=${name}&langpair=en|${LOC}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/'`
# put 'Puppy' back in
name_loc=${name_loc/xpupx /Puppy }
name_loc=${name_loc/\u0026\#39\;/\`} # correction for french
# create proper menu entry
name_loc=Name[${LOC}]=${name_loc}
# create file containing the translation, for backup purposes
#echo $name_loc > $DIR/$(basename $file)
# if all is well, add the translation to $file
[ "$(echo $name_loc | grep responseData)" = "" ] && [ "$name_loc" != "" ] && echo "$name_loc" >> "$file"
echo "Added $LOC translation to $(basename $file)...
"
else
echo "Translation for $LOC already exists in $(basename $file)...
"
fi
done
#remove backup files created
#rm $DIR/*
#rmdir $DIR