Skip to content

Instantly share code, notes, and snippets.

@thomasnordquist
Last active October 27, 2017 16:46
Show Gist options
  • Save thomasnordquist/b8c6bfe01c7f01477c6090c2272d0fef to your computer and use it in GitHub Desktop.
Save thomasnordquist/b8c6bfe01c7f01477c6090c2272d0fef to your computer and use it in GitHub Desktop.
Find uses of non-localized NSLocalizedString by scanning the projects files
#!/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;
@thomasnordquist
Copy link
Author

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

NSSting *title = NSLocalizedString(@"MyApp.title", nil);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment