So I was working on Xcode 7.1.1 (using Swift 2.1) on my new app
, and decided to localize it. I'm relatively new to Swift and iOS development, but so far. so good. I wanted to share a couple of "gotchas" I ran into while trying to internationalize my app. My biggest first stumbling block was not naming the localization strings file correctly. It has to be named:
When I created my strings file, I had named it "localization.strings", and was wondering why in the world the NSLocalizedString()
was not converting the key value to the translated one as specified in my file, as well as the language sub-versions.
At first I thought it may have been an issue with needing double quotes for the key value in the strings file. I had left the keys without double quotes, like this:
failsendtitle = "Failed to Send.";
but changing that to:
"failsendtitle" = "Failed to Send.";
did not make any difference. Then I thought perhaps the target memberships were not set when I created the file, but going to File Inspector it showed that they were. What fixed things was renaming the strings file to: Localizable.strings. Immediately after that, re-running on the simulator showed localization was working. I did not have to do a "clean".
I do wonder why during file creation of the strings file in Xcode, Apple does not provide a hint to indicate that the file must be named as such if using it for localization. Perhaps I missed it, but I also checked the plist/project settings and there was no option to point the project to use a particular strings file. But I do hope that this revelation of mine will help those of you who are having issues wondering why NSLocalizedString() is not substituting the specified key values from your strings file! What was especially frustrating (and dumb on my part) was that I had tried renaming it to "Localizable.strings", but made a typo. So you can save yourself potential trouble by copying and pasting the name.
BTW if you get the Build Failed error message of "Read failed: The data couldn't be read because it isn't in the correct format", it is likely that you forgot a semicolon
somewhere in the "Localizable.strings" file for one or more of the languages. It can easily happen because obviously in Swift you don't need to use a semicolon to end a line.