48

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?

Smiden
  • 645
  • 1
  • 6
  • 11
  • 7
    `NSLocalizedString()` returns the key if it fails to find the key/value pair. – Costique Feb 12 '12 at 18:04
  • Sorry, I didn't copy/paste the code so it was a type-o. The code in my app however, is correct. – Smiden Feb 12 '12 at 18:06
  • Found the problem. It doesn't work for me in the iPhone simulator so I tested it on an actual device and it worked. – Smiden Feb 12 '12 at 18:08
  • Check out this [answer][1], it may help. [1]: http://stackoverflow.com/a/8972349 –  Jan 04 '13 at 02:55
  • Don't be afraid to use underscores or spaces in the key. – clocksmith Sep 04 '14 at 20:51
  • 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. – Coder_A_D Mar 18 '15 at 09:38
  • 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? – TPG Aug 07 '15 at 14:48

30 Answers30

113

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.

Jim Rota
  • 1,781
  • 2
  • 16
  • 21
27

Same problem, solved using the filename: Localizable.strings

Pelanes
  • 3,451
  • 1
  • 34
  • 32
20

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

Christophe
  • 68,716
  • 7
  • 72
  • 138
virsunen
  • 494
  • 5
  • 8
19

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

ali ozkara
  • 5,425
  • 2
  • 27
  • 24
13

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

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

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

        NSLocalizedString("key", tableName: "MyappnameLocalizable", comment: "comment")
aiwis31
  • 154
  • 2
  • 8
9

Tested the app on an actual device and it worked

Smiden
  • 645
  • 1
  • 6
  • 11
  • 19
    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. – mark Jun 05 '12 at 17:01
  • Uninstalling the app from the simulator worked for me. – cprcrack Jan 18 '15 at 19:42
  • 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 Jul 18 '18 at 18:25
9

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

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

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

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

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Andrew Lewis
  • 93
  • 1
  • 4
8

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

DarkDust
  • 90,870
  • 19
  • 190
  • 224
7

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

Nick Greg
  • 221
  • 4
  • 7
6

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

BurtK
  • 1,016
  • 13
  • 12
6

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

Pelanes
  • 3,451
  • 1
  • 34
  • 32
6

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

NSLocalizedString(@"CONNECTIONERRORITLE",nil)

when it should be

NSLocalizedString(@"CONNECTIONERRORTITLE",nil)

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

CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • 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.) – Walt Sellers Jul 18 '18 at 18:45
5

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

1) create Localizable.strings as usual in YourLocalizeDemoSDK.

2) create the same 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 you to find the bundle in YourLocalizeDemoSDK. If you use Bundle.main instead, you will get a wrong value(in fact it will be the same string with the key).

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

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

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

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

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

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

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

And the String extension 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.

Liam
  • 172
  • 1
  • 10
4

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

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

If path is NULL, it means file not found.

Komposr
  • 1,946
  • 1
  • 12
  • 4
3

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

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
2

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

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

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

sandyios
  • 41
  • 2
2

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

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

Luke
  • 648
  • 7
  • 15
  • 1
    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. – Larry Ricker Aug 17 '19 at 15:07
2

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

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

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

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

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

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

Andy Ibanez
  • 12,104
  • 9
  • 65
  • 100
  • 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. – Andy Weinstein Jan 19 '22 at 10:57
1

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

Vincent
  • 4,342
  • 1
  • 38
  • 37
1

Resetting the simulator settings worked for me.

iOS Simulator > Reset Content and Settings...

clocksmith
  • 6,226
  • 3
  • 32
  • 45
1

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

BillChan
  • 85
  • 1
  • 3
1

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

Matthew
  • 837
  • 7
  • 20
1

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

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

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

sabius
  • 704
  • 7
  • 10
  • 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 – dzensik Jun 24 '20 at 19:43
0

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

0

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

@"key" = @"value";

It should be just:

"key" = "value";

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

NSWebLocalizedString(@"key", @"label for coder");
craned
  • 2,991
  • 2
  • 34
  • 38
0

Did you tried cleaning and rebuilding the project?

Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70
ChrisVollo
  • 157
  • 1
  • 3
  • 15
0

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

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

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

Eran Goldin
  • 980
  • 12
  • 21
0

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

Do not forget to replace YOUR_APP_NAME_HERE with your project name

Shell 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 with Unit Tests it will work in simulator ;)

ergunkocak
  • 3,334
  • 1
  • 32
  • 31
0

Swift 4:

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

var currentBundle = Bundle.main
NSLocalizedString("CONNECTIONERRORITLE", tableName: nil, bundle: currentBundle, value: "", comment: "")
Ahmed Lotfy
  • 3,806
  • 26
  • 28
0

I resolved it using this approach.

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

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

And you can use in swift 4 :

myButon.setTitle(NSLocalizedString("forgotpassword.key", comment: ""), for: UIControlState.normal)
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157