Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS app test if Localization translations have same keys

In my iOS app I have localization files such as Localizable.strings. I want to check that they have same keys and there is no missed keys in each localization.

I thought about performing this in Unit Tests.

  1. Is Unit testing the right place for this? Maybe there is much easier tool for it?
  2. How this Unit testing can be done?

I found article on this topic in Obj-C https://www.cocoanetics.com/2013/03/localization-unit-test/ that is 5 years old. Maybe something else can be used?

like image 707
moonvader Avatar asked Oct 17 '22 19:10

moonvader


1 Answers

You can just use this shell script I made up and run it as a Run Script (within Build Phases) of your App Target.

First, create the shell script and save it somewhere in your App Project, I saved it as check_missing_keys_in_translations.sh for example:

#!/bin/sh

#  check_missing_keys_in_translations.sh
#
#  Overview: Script will be executed from Build Phases > Run Script
#
#  Script: Extract sorted localization keys using "$1" as language code
#  given as parameter to locate the right Localizable.strings.
#
#  Build Phases > Run Script: The result will be compared to see if 
#  translations have the same keys or if one of them have more or less.

plutil -convert json 'AppProject/Resources/Localization/'"$1"'.lproj/Localizable.strings' -o - | ruby -r json -e 'puts JSON.parse(STDIN.read).keys.sort'

Just change the path AppProject/Resources/Localization/ to the path where your en.lproj, it.lproj... localization folders are located in your app (in this case called AppProject).

Second, go to your App Project, select the App Target and under Build Phases put this script code within Run Script:

# Check missing localization keys in translations
MISSING_KEYS_TRANSLATIONS=$(diff <($SRCROOT/tools/localization/check_missing_keys_in_translations.sh en) <($SRCROOT/tools/localization/check_missing_keys_in_translations.sh it))
if [ "$MISSING_KEYS_TRANSLATIONS" ]; then
echo "warning: $MISSING_KEYS_TRANSLATIONS"
fi

As always, check the path and adapted to where you saved the script created before. I saved it under App Project/tools/localization/... as you can see. You might want to adapt this script to better reflect your situation as I had only 2 localizable I was interested in checking they had the same keys. Is just shell scripting.

Check screenshot below:

like image 113
denis_lor Avatar answered Oct 21 '22 09:10

denis_lor