As a followup to posting my Localising Cocoa Apps talk I thought I’d outline some of my favourite localised string failures.
The “I heard literal strings in code are bad” kind of localisation
NSLocalizedString(@"Next", @"");
NSLocalizedString(@"Next", nil);
The “WTF is the comment for anyway” kind of localisation
NSLocalizedString(@"Next", @"Next");
The “out of sight out of mind” kind of localisation
#define MYBadCompanyLocalizedString(key) NSLocalizedString((key), @"")
Yes, that’s redefining the macro to hide the comment…
The “entire book as a key” kind of localisation
NSLocalizedString(@"In order to determine your location, location services must be turned on in settings", @"In order to determine your location, location services must be turned on in settings");
The “key can be anything but I know what it is” kind of localisation
NSString *aStringFromAPI = [response objectForKey:@"next-title"];
NSLocalizedString(aStringFromAPI, @"api resposne for `next`");
The grey area
Duplicating keys
This is in the grey area because good distinct comments can make duplicating keys manageable as good comments show you the key’s appearances in your app in the .strings file.
NSLocalizedString(@"Next", @"RootViewController 'next' button title");
NSLocalizedString(@"Next", @"DetailViewController 'next page' button title");
//Localizable.strings
// DetailViewController 'next page' button title
// RootViewController 'next' button title
"Next" = "Next";
genstrings
will warn you about duplicate keys- Bad/stupid/empty comments make this a bad offender
- keys with
nil
or@""
comments aren’t picked up as duplicate
Using localised strings in format strings
This is in the grey area because it’s inflexible and implies a structure, but can be okay for small strings. Often you should consider NSNumberFormatter for numbers
button.titleLabel.text = [NSString stringWithFormat:@"4 %@, 3 %@",
NSLocalizedString(@"pineapples",@"plural pineapples"),
NSLocalizedString(@"pears",@"plural pears")];
Localising the format string too
This is in the grey area because at this point we’ve got localised pieces everywhere. But it’s good that you’re trying though…
button.titleLabel.text = [NSLocalizedString stringWithFormat:
NSLocalizedString(@"4 %@, 3 %@", @"fruit quantities format string"),
NSLocalizedString(@"pineapples",@"plural pineapples"),
NSLocalizedString(@"pears",@"plural pears")];