Last active
October 27, 2017 16:46
-
-
Save thomasnordquist/b8c6bfe01c7f01477c6090c2272d0fef to your computer and use it in GitHub Desktop.
Find uses of non-localized NSLocalizedString by scanning the projects files
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 | |
IFS=$'\n' | |
# List of all localized strings by cat over all string files, then removing comments, empty lines | |
# result is a list of strings like this (including paranthesis): "order_list.title" | |
# - Remove comments from result: | grep -v '/\*' \ | |
# - Remove empty lines: | grep -v '^$' \ | |
LOCALIZED_STRINGS=$(\ | |
find . -name '*.strings' -not -path './Pods/*' -print0 | xargs -0 cat \ | |
| grep -v '/\*' \ | |
| grep -v '^$' \ | |
| awk '{ print $1 }' \ | |
) | |
# List of used localized strings, find lines in files with grep, extract string with perl, then filtering out duplicates | |
# egrep -o is used to split multiple matches in a single line into multiple lines | |
OCCURANCES=$( \ | |
grep --exclude-dir="Pods" -hR LocalizedString\(@\".* . \ | |
| egrep -o 'LocalizedString\(@"[^"]*[^)]*)' \ | |
| perl -n -e'/LocalizedString\(@"([^"]+)"/ && print $1 . "\n"' \ | |
| sort \ | |
| uniq \ | |
) | |
function countLocalizations { | |
UNTRIMMED=$(echo "${LOCALIZED_STRINGS}" | grep "^\"$1\"$" | wc -l) | |
echo $UNTRIMMED | sed 's/[[:space:]]//g' | |
} | |
function isLocalisationUsed { | |
LOCALISATION=`echo $1 | sed s/\"//g` | |
MATCH=$(echo "${OCCURANCES}" | grep "^$LOCALISATION$") | |
echo $MATCH | |
} | |
echo Count localisation definitions | |
for I in $(echo "${OCCURANCES}"); do | |
echo $(countLocalizations "$I") - $I; | |
done; | |
echo Show unused localisations | |
for I in $(echo "${LOCALIZED_STRINGS}" | sort | uniq); do | |
if [ "`isLocalisationUsed $I`" = "" ]; then | |
echo $I is not used | |
fi; | |
done; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Finds all occurences of localizations by grepping for "LocalizedString".
Then counting how often this string appears in
*.strings
files.This allows you to check if a string has been translated for all your languages.
This would match to
MyApp.title
and test your *.strings files how many localizations exist for this string