[ACCEPTED]-NSLocalizedString only retrieves the key, not the value in Localizable.strings (IOS)-localizable.strings

Accepted answer
Score: 113

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

Score: 26

Same problem, solved using the filename: Localizable.strings

0

Score: 19

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

Score: 18

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

Targets -> BuildPhases -> Copy Bundle 4 Resources

It hadn't been added automatically 3 for me.

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

Targets -> Build Phases -> Compile 1 resources

Score: 13

I have been searching the solution for 5 9 hours, I tried everything I could to make 8 my app localization working.

The problem 7 was that one of the Pods of my project had 6 a Localizable.strings file (actually it 5 was Parse pod that had not renamed it). Therefore 4 my Localizable.strings file was not recognized 3 by my app.

I fixed the issue by changing 2 the name of the file to "MyappnameLocalizable.strings" and 1 using NSLocalizedString this way:

        NSLocalizedString("key", tableName: "MyappnameLocalizable", comment: "comment")
Score: 9

Tested the app on an actual device and it 1 worked

Score: 9

I was having problems with this on the iOS 8 Simulator. I ended up deleting the Localization.strings 7 file in the simulator directory

( /Users/(me)/Library/Application 6 Support/iPhone Simulator/5.0/Applications/(etc)/(project)/(application.app)

cd 5 to there and delete all copies of Localization.strings 4 found there.

For some reason the usual rubber 3 chicken voodoo of build clean, quit iOS 2 Simulator, quit XCode, etc wasn't working, but 1 this did. At least for me, today.

Score: 8

This is happening when the runtime can't 6 find the specified key, for whatever reason. In 5 your case, it's most likely due to a typo: CONNECTIONERRORITLE is 4 missing a T for TITLE. Also pay attention to any 3 warnings/error when compiling regarding 2 the Localizable.strings file: if there are unbalanced " or missing 1 ; the file cannot be compiled/read correctly.

Score: 7

Change the name of the file to Localizable.strings, make 5 sure the target in the file inspector is 4 set. To avoid syntax errors right click 3 on Localizable.strings file->open as->ASCII 2 property list Also, cleaning the project 1 and building again helped in my case.

Score: 6

NSLocalizedString usage means you need the 5 EXACT case and spelling of your key in order 4 to get the content from it. You'll notice 3 that this one says

NSLocalizedString(@"CONNECTIONERRORITLE",nil)

when it should be

NSLocalizedString(@"CONNECTIONERRORTITLE",nil)

If 2 you look at the last part of the first one, it 1 says 'ITLE', not 'TITLE'

Score: 6

If you wrote double semicolons at the end 3 of a line, NSLocalization does not working. You 2 should check Localizable.strings file if 1 there is ';;'

Score: 6

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

Score: 5

When you are developing an SDK. You need 19 some extra operation.

1) create Localizable.strings as usual 18 in YourLocalizeDemoSDK.

2) create the same 17 Localizable.strings in YourLocalizeDemo.

3) find your Bundle Path of YourLocalizeDemoSDK.

Swift4:

// if you use NSLocalizeString in NSObject, you can use it like this
let value = NSLocalizedString("key", tableName: nil, bundle: Bundle(for: type(of: self)), value: "", comment: "")

Bundle(for: type(of: self)) helps 16 you to find the bundle in YourLocalizeDemoSDK. If 15 you use Bundle.main instead, you will get a wrong value(in 14 fact it will be the same string with the 13 key).

But if you want to use the String extension 12 mentioned by dr OX. You need to do some more. The 11 origin extension looks like this.

extension String {
    var localized: String {
        return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
    }
}

As we know, we 10 are developing an SDK, Bundle.main will get the bundle 9 of YourLocalizeDemo's bundle. That's not 8 what we want. We need the bundle in YourLocalizeDemoSDK. This 7 is a trick to find it quickly.

Run the code 6 below in a NSObject instance in YourLocalizeDemoSDK. And 5 you will get the URL of YourLocalizeDemoSDK.

let bundleURLOfSDK = Bundle(for: type(of: self)).bundleURL
let mainBundleURL = Bundle.main.bundleURL

Print 4 both of the two url, you will find that 3 we can build bundleURLofSDK base on mainBundleURL. In 2 this case, it will be:

let bundle = Bundle(url: Bundle.main.bundleURL.appendingPathComponent("Frameworks").appendingPathComponent("YourLocalizeDemoSDK.framework")) ?? Bundle.main

And the String extension 1 will be:

extension String {
    var localized: String {
        let bundle = Bundle(url: Bundle.main.bundleURL.appendingPathComponent("Frameworks").appendingPathComponent("YourLocalizeDemoSDK.framework")) ?? Bundle.main
        return NSLocalizedString(self, tableName: nil, bundle: bundle, value: "", comment: "")
    }
}

Hope it helps.

Score: 4

To find out if the Localizable.strings file 5 is being found, check the content of your 4 .app build and you can also do this in code:

//en 3 is for English for example so specify yours 2 here NSString *path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];

If 1 path is NULL, it means file not found.

Score: 3

If you have any extra semicolon in your 1 strings file, it doesnt localise.

Score: 2

I faced a similar problem, suddenly my localizable 6 strings didn't work at all. Then I used 5 file-compare with the old .strings copy, and 4 at-last I found I have accidentally deleted 3 a key in it.

So Definitely if the format 2 is wrong Xcode will not read the strings 1 for you.

This is the format it expects "Key" = "Value";

Score: 2

Because I stumbled upon this when looking 5 for the answer to a similar problem, I'm 4 leaving my solution here:

If your key has 3 "\u2028" in it instead of "\n", it will 2 always return just the key and not the value. Seems 1 to be a bug with localization.

Score: 2

We were dealing with an issue in which some 24 keys were not getting their values despite 23 all of them existing in Localizable.strings.

Turns 22 out that the developer who built the file 21 added two semi-colons at one line, and that 20 caused the parsing of this file to be quite 19 erratic.

After I found the two semi-colons 18 one next to the other and got rid of one 17 of them, the app wouldn't compile anymore. At 16 this point I was able to find some lines 15 that didn't have semi-colons. After fixing 14 those and the app compiled, all my strings 13 worked fine.

So having two semi-colons in 12 the same line caused the file to compile 11 despite it missing semi-colons in other 10 lines.

It looks like Apple's parser for .strings file 9 is very primitive.

Unfortunately, the plist 8 linter was useless in our case. To find 7 and fix the issue, I copied and pasted the 6 entire contents of our Localizable.strings 5 file (which is over 2000 lines long), and 4 started copying it back in 20 line chunks 3 and compiling each time. It was the only 2 way to find an issue that the linter would 1 not catch.

Score: 1

Put an ; at end of the lines in the Localizable.strings 1 files.

Score: 1

Resetting the simulator settings worked 2 for me.

iOS Simulator > Reset Content and 1 Settings...

Score: 1

In my case, I tried clean project and delete 3 derived data still not work. I remove several 2 line breaks in the strings file and then 1 Xcode find the strings again.

Score: 1

None of the suggested solutions worked for 2 me, but I did solve the issue by deleting 1 the .app file in Products

Score: 1

I had the issue when one language was working 17 properly and other language worked half 16 of the time and other time I was getting 15 the key, instead of localized version of 14 that key.

Problem in my case was that after 13 manually merging version control conflict, there 12 was extra line of '>>>>>' left, project 11 didn't complain (not like when you miss 10 the semicolon, it complains) and was building 9 properly, so every key before that '>>>>>' was 8 being translated and every key after, not.

If 7 more simpler solutions fails, you might 6 have to go through every line and look for 5 extra characters (not only '>>>>>', other 4 extra characters too), or if you are using 3 the version control get older, stable version 2 of Localizable.strings file and manually 1 go through changes and add later commits.

Score: 0

The first line of the localization file 2 must contain a mapping, otherwise the SDK 1 won't read the file.

Score: 0

Be sure to not put '@' in front of your 3 key or value like so:

@"key" = @"value";

It should be just:

"key" = "value";

The '@' only 2 goes in front of the key when accessing 1 it:

NSWebLocalizedString(@"key", @"label for coder");
Score: 0

Did you tried cleaning and rebuilding the 1 project?

Score: 0

I had everything working when suddenly localization 11 just stopped translating strings. This means 10 the file is somehow unreadable to Xcode.

This 9 had happened to me because I had pasted 8 a bad character in the Localized.strings file. When I deleted 7 the few lines with the offending character 6 (a non unicode character? bad quote signs? Couldn't 5 tell) everything went back to normal.

To 4 find the offending lines I made a breakpoint, and 3 manually translated the strings in my file 2 in the debugger, until I hit the first one 1 that won't translate.

Score: 0

For xcode 9.2 removing "Localizable.strings" files 7 from the simulators for the project using 6 console solved it for me. Here is the commands 5 for the lazy ones like me ;)

Do not forget 4 to replace YOUR_APP_NAME_HERE with your project name

Shell 3 script:

cd
cd Library/Developer/CoreSimulator/Devices/
find . -name "Localizable.strings" | grep "YOUR_APP_NAME_HERE.app" | xargs rm

If you are experiencing the problem 2 with Unit Tests it will work in simulator 1 ;)

Score: 0

Swift 4:

If you use more than one bundle 1 such as you use it in an external framework:

var currentBundle = Bundle.main
NSLocalizedString("CONNECTIONERRORITLE", tableName: nil, bundle: currentBundle, value: "", comment: "")
Score: 0

I resolved it using this approach.

The localize file 8 was created with the name main.Strings. Then, I open 7 the main.Strings files (for each language added) and 6 I renamed them manually with the name localize.strings and 5 add them one by one to my project, also 4 I deleted the main.strings.

The second thing to evaluate 3 is: check . your file, all the keys have to 2 end with ; and be sure all the quotes are properly 1 opened and closed.

And you can use in swift 4 :

myButon.setTitle(NSLocalizedString("forgotpassword.key", comment: ""), for: UIControlState.normal)

More Related questions