Search code examples
iosobjective-clocalizationtranslationlocalizable.strings

iOS what is the proper way to handle inserting localized text into an app programmatically?


I'm working on the localization of my app and am intersted if it is a good idea to "centralize" all localizable strings in my app to be provided from some static method: [AppStrings stringWithType:type].

On one hand, it seems like a centralized method would make editing the localizable strings easier in the future, but on the other hand, the string itself is no longer readable, and I will have to define a lot of enumerated types.

What is the proper way to do localization of strings in a large project (100+ strings to be localized)? Do I embed NSLocalizedString() in code, or should I try to somehow centralize providing these strings?

typedef enum : NSUInteger {
    ksCheckingCredentials,
    ksError
} kStringType;

+(NSString*)stringWithType:(int)type

{
    switch (type) {
        case ksCheckingCredentials:
            return NSLocalizedString(@"Checking Credentials",
                              @"Inform the user that credentials check is being performed"); 
            break;
         case    ksError:
            return NSLocalizedString(@"Error",
                                     @"Generic error message to display to the user");
            break;
         default:
            break;
    }
}

-- OR-- Do I just use the code below at a 100 different places in my app?

self.errorLabel.text = NSLocalizedString(@"Error",@"Generic error message to display to the user");

Solution

  • Chances are you're going to want to reuse the translations throughout the app. For instance, odds are everywhere there is an error you want that same localized error string. The way i've done this in the past is:

    1. Make a LocalizedDefs.h file (name isn't important), and import it throughout your app. In there you'll put a series of #define's for your NSLocalized strings.

    Example:

    #define ksLocalizedError NSLocalizedString(@"Error", @"Generic error message to display to the user")

    Then in your code wherever you want that error, you'll simply put in ksLocalizedError.

    1. Finally you want your translations (obviously) so in your Localizeable.strings files simply setup the translations.

    Example: "Error" = "Error";