Skip to content

Instantly share code, notes, and snippets.

@Zuhair-Kj
Last active May 9, 2021 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zuhair-Kj/c9404aab513335aa4d6190604c26d2e3 to your computer and use it in GitHub Desktop.
Save Zuhair-Kj/c9404aab513335aa4d6190604c26d2e3 to your computer and use it in GitHub Desktop.
Android string relocator
# Moves string translations from one module to anothe in android project.
## How to use?
# Navigate to Android studio root directory, paste the script and invoke it from terminal:
# ./stringRelocator.sh ${source_module} ${destination_module} ${string_key}
# example: ./stringRelocator app core login_header
# In case you need to support more translation, just add the proper values directory name to the array list (line 44)
# The script moves stirngs line by line
# If the destination file does not exist then the script does not create the file for you but it does not remove the string from the source either.
# Happy coding!
insertToStringFile() {
if (fileExist $2) then
echo "$1"
C=$(echo "$1" | sed 's/\//\\\//g')
sed -i '' "/<\/resources>/ s/.*/${C}\n&/" $2
return 0
else
return 1
fi
}
fileExist() {
if [ -f "$1" ]; then
return 0
else
echo "The file $1 does not exist"
return 1
fi
}
moveStringTranslation() {
while IFS= read -r line; do
# echo $line
if [[ $line == *"<string name=\"$3\""* ]]; then
if (insertToStringFile "$line" "$2") then
grep -v "$line" $1 > tmpfile && mv tmpfile $1
fi
fi
done < "$1"
}
sourceModule="$(pwd)/$1"
destinationModule="$(pwd)/$2"
stringKey=$3
locales=("values" "values-in" "values-ja" "values-ko" "values-ms" "values-th" "values-vi" "values-zh-rCN" "values-zh-rHK" "values-zh-rTW")
echo "$sourceModule/src/main/res/values/strings.xml"
for i in "${locales[@]}"
do
echo "moving \"$3\" from :$1 to :$2"
moveStringTranslation "$sourceModule/src/main/res/$i/strings.xml" "$destinationModule/src/main/res/$i/strings.xml" $stringKey
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment