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?
If you have any extra semicolon in your localisable file, it builds but it fails to localise.
You should have more than one files named "Localizable.strings" in your target. Xcode fails to handle this issue. You can rename one, and use "NSLocalizedStringFromTable" to load from it. For detail, you can refer to: Localisation strings not always work having ShareKit in a project
A few minutes after asking for a solution, I found it. I had included another Localizable.strings from a third party. Once I removed that, no more lost translations every other run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With