NSLocalizedString only retrieves the key, not the value in Localizable.strings (IOS)

46,858

Solution 1

Tested the app on an actual device and it worked

Solution 2

In my case it was because I had mistakenly named the file "Localization.strings" and hadn't noticed (it has to be named Localizable.strings). As explained previously the symptom is because the compiler cannot find the string. Otherwise the cause could be any number of things but usually it's a missing semi colon or quotation mark. These are hard to find when you're doing a lot of localizations at once. The lesson learned is to start building your localization file early on in your development process and build it as you go, so that they are easier to spot.

Solution 3

Same problem, solved using the filename: Localizable.strings

Solution 4

Change your .strings file name to Localizable.strings, it worked for me.

Solution 5

Double check that the Localizable.strings file is being added to

Targets -> BuildPhases -> Copy Bundle Resources

It hadn't been added automatically for me.

Edit 2021: with XCode 12 the Localizable.strings have to be added to

Targets -> Build Phases -> Compile resources

Share:
46,858

Related videos on Youtube

Smiden
Author by

Smiden

Updated on May 14, 2021

Comments

  • Smiden
    Smiden about 3 years

    I've made a strings file named "Localizable.strings" and added two languages to it, like so:

    "CONNECTIONERROR" = "Check that you have a working internet connection.";
    "CONNECTIONERRORTITLE" = "Network error";
    

    I have also converted the files to Unicode UTF-8 However, when I create a UIAlertView like this:

     UIAlertView *myAlert = [[UIAlertView alloc]
     initWithTitle:NSLocalizedString(@"CONNECTIONERRORITLE",nil)
     message:NSLocalizedString(@"CONNECTIONERROR",nil)                    
     delegate:self
     cancelButtonTitle:@"Ok"
     otherButtonTitles:nil];
    

    the alert view only shows the key text, not the value. It works if I, for example, set a UITextviews text to NSLocalizedString(@"CONNECTIONERROR",nil), but the alert view only displays the key. Anyone know what's wrong?

    • Costique
      Costique over 12 years
      NSLocalizedString() returns the key if it fails to find the key/value pair.
    • Smiden
      Smiden over 12 years
      Sorry, I didn't copy/paste the code so it was a type-o. The code in my app however, is correct.
    • Smiden
      Smiden over 12 years
      Found the problem. It doesn't work for me in the iPhone simulator so I tested it on an actual device and it worked.
    • Admin
      Admin over 11 years
      Check out this How to Answer[1], it may help. [1]: stackoverflow.com/a/8972349
    • clocksmith
      clocksmith almost 10 years
      Don't be afraid to use underscores or spaces in the key.
    • Coder_A_D
      Coder_A_D over 9 years
      I am also facing this issue. checked the all the files in Project Navigator. Localizable.strings file is added twice. so I removed one file, and Localize that file for Base(English), and other one Spanish.
    • TPG
      TPG almost 9 years
      I still not able to make it work. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"WARNING", nil) message:NSLocalizedString(@"LOGIN_NRIC_REQUIRED", nil) delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; is not working got LOGIN_NRIC_REQUIRED. Any idea?
  • mark
    mark about 12 years
    Another problem is that the Localizable.strings file doesn't get cleaned out and re-initialized properly; if you were testing on the device before and then created or significantly altered your Localizable.strings files the changes aren't always noticed, so you have to CLEAN your build AND delete the copy from the device before the changes propagate properly.
  • Tom
    Tom over 11 years
    Thanks, this comment helped me a lot!
  • cprcrack
    cprcrack over 9 years
    Uninstalling the app from the simulator worked for me.
  • Nova
    Nova about 9 years
    You are savior. This was the problem in my case.
  • Nico S.
    Nico S. about 8 years
    Lol, that helped. Thanks
  • He Yifei 何一非
    He Yifei 何一非 almost 8 years
    note that the filename is case sensitivity. So you have to use ".strings" instead of ".Strings"
  • ViruMax
    ViruMax over 7 years
    This is also a great way to find errors in Localizable.strings file.
  • Alex
    Alex about 7 years
    Thanks, this saved my day
  • Martin
    Martin about 7 years
    The file should be name Localizable.string, with a upper L, without a s... Convention over configuration... I hate this paradigm.
  • KingWu
    KingWu about 7 years
    Thanks. save my life
  • Nermin Sehic
    Nermin Sehic almost 7 years
    Localizable.strings in my case as well. Spent good two hours trying to find what was causing the problem.
  • Sharad Chauhan
    Sharad Chauhan about 6 years
    Changing name, cleaning and deleting app worked for me.
  • Walt Sellers
    Walt Sellers almost 6 years
    I find that deleting the app from the Build Products folder on the Mac is enough make Xcode use all the latest files.
  • Walt Sellers
    Walt Sellers almost 6 years
    We recently changed the names of enums to start with lowercase during a transition to newer Swift. But we forgot that we derive some long string keys from the enum names. The names no longer matched. The one different character was well into the middle so it was hard to notice. I was checking the string match by using Xcode's 'find' feature. Since that was case-insensitive, it found the keys and I assumed they matched. Your answer here made me double-check and find the error. Thanks. (Now our default value will change so we notice immediately in debug builds.)
  • Devil
    Devil almost 5 years
    yes , it is happened to me i check and resolve this ... thank you for help .
  • Larry Ricker
    Larry Ricker almost 5 years
    It appears to me that neither \u2028 nor \n work with localization. I gave you a +1 anyway for acknowledging the defects in localization which has not really been stable since xCode 9.2.
  • Sagar Daundkar
    Sagar Daundkar over 4 years
    Thanks a lot, saved lots of time to figure out the issue.
  • dzensik
    dzensik about 4 years
    Thanks. In my case it was one Plus char "+" before one of the keys in the Localizable.string file that was added by me while the merging process accidentally
  • Daniel
    Daniel over 3 years
    It worked for me after removing it from Copy Bundle Resources and adding it back in.
  • Christophe
    Christophe about 3 years
    Great! Very helpful! I updated the answer since the menus have slightly changed since the original answer, and I think this can still help a lot of people.
  • Andy Weinstein
    Andy Weinstein over 2 years
    I had a single backquote at the end of a line after a semicolon - one of these: ` , basically just looks like a fleck of dust on the screen. In terms of finding it - also possible to use binary search - first, I put a string definition for a string used in the main screen at line one of the file, then at the halfway point, then continue to split the remaining distance in half depending on whether the string comes out correctly or just as the key.