NSLocalizedString works only half of the time where I get back the expected the value for the referenced key. The other times I get back the key name specified in NSLocalizedString and consistently happens on every other run.
Currently, I am only supporting English at the moment.
I'm calling:
NSString *someText = NSLocalizedString(@"mystring.keyname", nil);
Contents of en.lproj/Localizable.strings:
"mystring.keyname" = "Hello there!";
When it's not working correctly, someText's
value is mystring.keyname
.
Here's how I'm testing reliability:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSLog(@"Strings file: %@", [bundle pathForResource:@"Localizable" ofType:@".strings"]);
NSLog(@"Localizations: %@", [bundle localizations]);
NSLog(@"Local Dict: %@", [bundle localizedInfoDictionary]);
NSLog(@"localizedStringForKey: %@", [bundle localizedStringForKey:@"mystring.keyname"value:@"Wha happened?" table:nil]);
NSLog(@"Localized String: %@", NSLocalizedString(@"mystring.keyname", @"Are you sure you want to start a new game?"));
}
This prints out the expected values every the other run:
Run #1:
Local Dict: (null)
localizedStringForKey: Hello there!
Localized String: Hello there!
Run #2:
Local Dict: (null)
localizedStringForKey: Wha happened?
Localized String: mystring.keyname
My files are laid out as such:
WORKSPACE_DIR/
my_workspace_name.xcworkspace
PROJECT_DIR/
- my_project_name.xcodeproj
RESOURCES_DIR/
- en.lproj/
Localizable.strings
XCode's project-level settings:
1) Build Phases has Localizable.strings listed in the "Copy Bundle Resources".
2) Build Settings:
Convert Copied Files => YES
Property List Output Encoding => binary
Strings file Output Encoding => UTF-16
3) Info.plist's: CFBundleDevelopmentRegion => 'en'
Localizable.strings is configured as UTF-16 although I do not see a Text Settings option in the File Inspector with the strings file selected. But I've verified the encoding type by opening the file in TextWrangler which does show encoding as Unicode (UTF-16) with Unix LF.
Any suggestions on how to get this working all of the time?