cschwarz
cschwarz

Reputation: 1205

iOS Localization: Create strings-file for native language?

If you're an English programmer and write your code like...

NSLocalizedString(@"Hello", ...)

... do you create a en.lproj/Localizable.strings although there's the same string in there as in the NSLocalizedString macro?

Upvotes: 1

Views: 1325

Answers (3)

Jeff
Jeff

Reputation: 4209

First, the Apple docs answer your question in the Foundations Functions Reference for NSLocalizedString when it reads "The initial value for key in the strings file will be key."

Second, this question is ranked high in my searches, so I'd like to add 1 situation in which I do create a strings file in my native language/locale.

My native language/locale is English. I create a strings file for programmatically generated singular nouns that I sometimes need to pluralize. Example:

@interface XYZFeline : NSObject
...
@interface XYZCanine : NSObject
...
@implementation XYZAnimalController
NSDictionary *classToWord = @{ @"XYZFeline": @"cat", @"XYZCanine" : @"dog" };

- (NSString *)describeQuantityOfAnimal:(id)animal
{
    NSString *typeWord = classToWord[NSStringFromClass(animal)];
    NSString *sentence = [NSString stringWithFormat:@"%@ %lu %@.",
        NSLocalizedStringFromTable(@"You have", @"phrases.strings", nil),
        animal.count,
        [animal.count == 1
            ? NSLocalizedStringFromTable(typeWord, @"nouns.strings", nil)
            : NSLocalizedStringFromTable(typeWord, @"nouns-plural.strings", nil),
    ];
    return sentence;
}

The nouns.strings for English does not exist, but the English nouns-plural.strings file looks like this:

"dog" = "dogs";
"cat" = "cats";

Example output:

You have 3 dogs.
You have 1 cat.

This will allow you to get proper singular or plural nouns in your output without putting language rules in your code.

Note: I am aware that my coded sentence structure of subject, predicate, object and adjective-before-noun is limited to only some Indo-European languages, but that is a problem to solve in another question.

Upvotes: 0

es1
es1

Reputation: 1556

The best tutorial on Localization I found is by Ray Wenderlich: How to localize an iphone app

Upvotes: 0

Wain
Wain

Reputation: 119021

Generally, no, you don't need to if you use the string as the key to NSLocalizedString. But you can then use the Localizable.strings file to replace strings in the UI without changing the code files.

Also, you don't have to use the actual string as the key to NSLocalizedString, you could use a set of generic identifiers for the purpose (like the comment parameter is intended to help with). Then you need the Localizable.strings file to fill in the true UI values.

Upvotes: 2

Related Questions