Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSLocalizedString loads the string sometimes, not always

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?

like image 838
Howard Spear Avatar asked Jan 23 '13 18:01

Howard Spear


3 Answers

If you have any extra semicolon in your localisable file, it builds but it fails to localise.

like image 169
Teja Nandamuri Avatar answered Oct 16 '22 15:10

Teja Nandamuri


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

like image 39
sunzhuoshi Avatar answered Oct 16 '22 16:10

sunzhuoshi


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.

like image 1
Rool Paap Avatar answered Oct 16 '22 15:10

Rool Paap