Skip to content
February 9, 2013 / Ivan Suhinin

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:

Screen Shot 2013-02-09 at 11.51.00 AM

2. Get your localized string in code by magic string:

Screen Shot 2013-02-09 at 11.51.41 AM

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:

Screen Shot 2013-02-09 at 11.59.28 AM

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.

Enjoy!

P.S. If there is an existing solution for such stuff (that I did not find yesterday), please share :)

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 169 other followers