A comment on a bitbucket fork of Murky led me to “Automatically localize your nibs when building“, which suggests a great way to automate the hard developer-side stuff in localizing in Xcode–pulling the original strings from the XIB files and putting the translated strings back in. You absolutely should read the original blog post there, because I cannot adequately explain the big-picture part of the idea with a short quote.
My one complaint with the setup described is that the script for the “Run Script” build phase described there is a maintenance headache that I could do without. Here’s my solution: Create two new build targets, both of the “Shell Script” type–these are targets that just run a shell script, so they are created with only a “Run Script” build phase. I called my two new targets “Create/Update English .strings files” and “Create/Update l10n XIBs” but you can call them whatever you want.
In the “Create/Update English .strings files” target’s “Run Script” phase, the script is:
-
for xibFile in "$PROJECT_DIR/English.lproj/"*.xib; do
-
ibtool –generate-strings-file "${xibFile}.strings" "$xibFile"
-
done
-
exit 0
This uses ibtool
to create a strings file for every XIB in the English localization (from foo.xib
, foo.xib.strings
will be created).
In the “Create/Update l10n XIBs” target’s “Run Script” phase, the script is:
-
originalResourceDirectory="$PROJECT_DIR/English.lproj"
-
for localizedDirectory in "$PROJECT_DIR/"*.lproj; do
-
if [ localizedDirectory != originalResourceDirectory ]; then
-
for xibFile in "${originalResourceDirectory}/"*.xib; do
-
xibBaseName=$(basename "${xibFile}")
-
ibtool –strings-file "${localizedDirectory}/${xibBaseName}.strings" \
-
–write "${localizedDirectory}/${xibBaseName}" \
-
"$xibFile"
-
done
-
fi
-
done
-
exit 0
This goes through every .lproj
directory except English.lproj
and uses ibtool
to apply the .xib.strings
files in those localizations to the XIB files in English.lproj
.
By having these as two separate targets, they aren’t run every time I build and each part can be run on its own, on demand. By using the power of shell scripting, I avoid having to alter the scripts for every new localization or XIB.