JiaYow JiaYow - 1 year ago 127
iOS Question

Best practice using NSLocalizedString

I'm (like all others) using

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
    will end up with multiple comments for one string

  • If you want to use
    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 to
    NSLocalizedString(@"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
file where I
all the localize-code. For example

// In strings.h
#define NSLS_COMMON_CANCEL NSLocalizedString(@"Cancel", nil)
// Somewhere else

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
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?

Answer Source

NSLocalizedString has a few limitations, but it is so central to Cocoa that it's unreasonable to write custom code to handle localization, meaning you will have to use it. That said, a little tooling can help, here is how I proceed:

Updating the strings file

genstrings overwrites your string files, discarding all your previous translations. I wrote update_strings.py to parse the old strings file, run genstrings and fill in the blanks so that you don't have to manually restore your existing translations. The script tries to match the existing string files as closely as possible to avoid having too big a diff when updating them.

Naming your strings

If you use NSLocalizedString as advertised:

NSLocalizedString(@"Cancel or continue?", @"Cancel notice message when a download takes too long to proceed");

You may end up defining the same string in another part of your code, which may conflict as the same english term may have different meaning in different contexts (OK and Cancel come to mind). That is why I always use a meaningless all-caps string with a module-specific prefix, and a very precise description:

NSLocalizedString(@"DOWNLOAD_CANCEL_OR_CONTINUE", @"Cancel notice window title when a download takes too long to proceed");

Using the same string in different places

If you use the same string multiple times, you can either use a macro as you did, or cache it as an instance variable in your view controller or your data source. This way you won't have to repeat the description which may get stale and get inconsistent among instances of the same localization, which is always confusing. As instance variables are symbols, you will be able to use auto-completion on these most common translations, and use "manual" strings for the specific ones, which would only occur once anyway.

I hope you'll be more productive with Cocoa localization with these tips!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download