NSLocalizedString only retrieves the key, not the value in Localizable.strings (IOS)
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
Related videos on Youtube
Smiden
Updated on May 14, 2021Comments
-
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 over 12 years
NSLocalizedString()
returns the key if it fails to find the key/value pair. -
Smiden over 12 yearsSorry, I didn't copy/paste the code so it was a type-o. The code in my app however, is correct.
-
Smiden over 12 yearsFound the problem. It doesn't work for me in the iPhone simulator so I tested it on an actual device and it worked.
-
Admin over 11 yearsCheck out this How to Answer[1], it may help. [1]: stackoverflow.com/a/8972349
-
clocksmith almost 10 yearsDon't be afraid to use underscores or spaces in the key.
-
Coder_A_D over 9 yearsI 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 almost 9 yearsI 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 about 12 yearsAnother 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 over 11 yearsThanks, this comment helped me a lot!
-
cprcrack over 9 yearsUninstalling the app from the simulator worked for me.
-
Nova about 9 yearsYou are savior. This was the problem in my case.
-
Nico S. about 8 yearsLol, that helped. Thanks
-
He Yifei 何一非 almost 8 yearsnote that the filename is case sensitivity. So you have to use ".strings" instead of ".Strings"
-
ViruMax over 7 yearsThis is also a great way to find errors in Localizable.strings file.
-
Alex about 7 yearsThanks, this saved my day
-
Martin about 7 yearsThe file should be name Localizable.string, with a upper L, without a s... Convention over configuration... I hate this paradigm.
-
KingWu about 7 yearsThanks. save my life
-
Nermin Sehic almost 7 yearsLocalizable.strings in my case as well. Spent good two hours trying to find what was causing the problem.
-
Sharad Chauhan about 6 yearsChanging name, cleaning and deleting app worked for me.
-
Walt Sellers almost 6 yearsI find that deleting the app from the Build Products folder on the Mac is enough make Xcode use all the latest files.
-
Walt Sellers almost 6 yearsWe 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 almost 5 yearsyes , it is happened to me i check and resolve this ... thank you for help .
-
Larry Ricker almost 5 yearsIt 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 over 4 yearsThanks a lot, saved lots of time to figure out the issue.
-
dzensik about 4 yearsThanks. 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 over 3 yearsIt worked for me after removing it from Copy Bundle Resources and adding it back in.
-
Christophe about 3 yearsGreat! 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 over 2 yearsI 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.