IOS - NSLocalizedString only returns a key string

I am having trouble debugging my NSLocalizedString implementation. It should be simple, but whatever I do, the function returns a KEY string.

I use Xcode 4.5 and iOS6, so I:

  • A new file named File.strings .
  • In the settings of my project, I added English and Spanish as the language settings.
  • Click "Make Localized" in the file inspector and make sure that both English and Spanish are selected and that the target membership is selected for my purpose.
  • Added "KEY" = "TestEnglish"; in my english File.strings
  • Added "KEY" = "TestSpanish"; in my spanish File.strings
  • Added NSLog(@"Localization: %@\n", NSLocalizedString(@"KEY", nil)); into my .m file.

When I launch the application, the value "KEY" always displayed in NSLog format.

To get to this a bit more, I also tried this:

 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 is "KEY ", but path is a valid path.

Does anyone know how to debug this? I feel like I read every question / answer, but none of the suggestions will help.

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

I also uninstalled / cleaned the application about a dozen times.

+6
source share
3 answers

If you specify table:nil , then the NSBundle will try to retrieve the localization from the default table (such as in SOMELANG.lproj/Localizable.strings ). If you have localization elsewhere, you must explicitly specify the table using table:@"File" (or use the NSLocalizedStringFromTable() macro NSLocalizedStringFromTable() same way:

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

In my case, the problem was in the case of the string: "bla.bla.blabla.Book S lot", while Localizable.strings defined it as "bla.bla.blabla.Book s lot"

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

+1
source

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

0
source

All Articles