I'm having some trouble debugging my NSLocalizedString implementation. Should be simple, but whatever I do, the function only returns the KEY string.

I'm using XCode 4.5 and iOS6, so I:

  1. Added a new file called File.strings.
  2. In my project settings I added English and Spanish as language settings.
  3. Clicked "Make Localized" in the file inspector, and made sure that both English and Spanish options were selected, and also that the Target membership to my target was selected.
  4. Added "KEY" = "TestEnglish"; to my english File.strings
  5. Added "KEY" = "TestSpanish"; to my spanish File.strings
  6. Added NSLog(@"Localization: %@\n", NSLocalizedString(@"KEY", nil)); to my .m file.

When I run the app, the value "KEY" is always displayed printed in the NSLog.

To jump into this a bit more, I tried this as well:

NSString *path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
NSString *str = [[NSBundle bundleWithPath:path] localizedStringForKey:@"KEY" value:@"" table:nil];
NSLog(@"Localization: %@\n", str); 

and still the value "KEY" is printed, yet, path is a valid path.

Does anyone have any clue how to debug this? I feel like I've read every SO question/answer out there, but none of the suggestions help.

I realize that NSLocalizedString returns the KEY string when it cannot match a key, but I don't see how I can debug why my app might not be matching the KEY.

I've also deleted/cleaned the app about a dozen times.

有帮助吗?

解决方案

If you specify table:nil, then NSBundle will try to fetch the localization from the default table (the one in SOMELANG.lproj/Localizable.strings). If you have the localization elsewhere, you should explicitly specify the table using table:@"File" (or use the NSLocalizedStringFromTable() macro in a similar manner:

NSString *value = NSLocalizedStringFromTable(@"key", @"File", nil);

其他提示

Rename the InfoPlist.strings file to Localizable.strings (double clic) and then you will get the correct string for that key.

In my case the issue was with the case of the string: "bla.bla.blabla.BookSlot" whereas the Localizable.strings had it defined as "bla.bla.blabla.Bookslot"

So, double-check that the key string is in the correct case. Better yet, copy-paste.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top