GenLoc – stop using magic strings for localizing iOS/OSX projects
As you know, I come from .NET background and I’m used to good editor capabilities. Xcode bugged me a lot when I was just starting iOS development. Later I got used to some stuff and overcame some stuff by code and practices. All was fine but recently I’ve encountered something not that easy to bypass: magic strings in localization. Out of box default way to have a localized string is to:
1. Add localized strings by key to your Localizable.strings file:
2. Get your localized string in code by magic string:
Not good, especially when you need to use same text (like @”TEXT_OK”) all over the project.
How to change that and a. avoid strings b. get proper autocompletion support in Xcode? I’ve thought a bit, and googled upon Patrick Hughes’ solution for iOS image name generation that used Python as a language for pre-build script that does the good stuff. Inspired by it, I’ve created a Ruby script that generates an .h file with a list of constants, each one representing a key in Localizable.strings. This allowed me to have this:
I was very happy to refresh my Ruby skills and, just to feel complete, decided to share the project on Github. Here it is, with a sample project and installation instructions.
P.S. If there is an existing solution for such stuff (that I did not find yesterday), please share