I'm (like all others) using NSLocalizedString
to localize my app.
Unfortunately, there are several "drawbacks" (not necessarily the fault of NSLocalizedString itself), including
- No autocompletition for strings in Xcode. This makes working not only error-prone but also tiresome.
- You might end up redefining a string simply because you didn't know an equivalent string already existed (i.e. "Please enter password" vs. "Enter password first")
- Similarily to the autocompletion-issue, you need to "remember"/copypaste the comment strings, or else
genstring
will end up with multiple comments for one string - If you want to use
genstring
after you've already localized some strings, you have to be careful to not lose your old localizations. - Same strings are scattered througout your whole project. For example, you used
NSLocalizedString(@"Abort", @"Cancel action")
everywhere, and then Code Review asks you to rename the string toNSLocalizedString(@"Cancel", @"Cancel action")
to make the code more consistent.
What I do (and after some searches on SO I figured many people do this) is to have a seperate strings.h
file where I #define
all the localize-code. For example
// In strings.h
#define NSLS_COMMON_CANCEL NSLocalizedString(@"Cancel", nil)
// Somewhere else
NSLog(@"%@", NSLS_COMMON_CANCEL);
This essentially provides code-completion, a single place to change variable names (so no need for genstring anymore), and an unique keyword to auto-refactor. However, this comes at the cost of ending up with a whole bunch of #define
statements that are not inherently structured (i.e. like LocString.Common.Cancel or something like that).
So, while this works somewhat fine, I was wondering how you guys do it in your projects. Are there other approaches to simplify the use of NSLocalizedString? Is there maybe even a framework that encapsulates it?