I am trying to localize my app and I have set up under the Info tab a localization for two languages. Xcode has created under Main.storyboard two new files called Main.strings (English) and Main.strings (German).
Now when I change the different elements within those files (the ones Xcode has created for buttons, labels, titles etc.) everything works fine.
But I need to localize an alert controller as well. Here is what I did, but this doesn't work and I don't know why, because as far search engines help, I did it the right way:
In the Main.strings files for both languages I added this under the Xcode created elements at the bottom of the file:
"alert" = "Nothing to Share!";
"message" = "You must enter a value and hit SPLIT to share with your friends.";
"cancel" = "Cancel";
(and obviously the same for the German Main.Strings file)
In my viewController I added the alert controller like this:
let alertTitle = NSLocalizedString("alert", comment: "")
let alertMessage = NSLocalizedString("message", comment: "")
let cancelButtonText = NSLocalizedString("cancel", comment: "")
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: cancelButtonText, style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(cancelAction)
present(alert, animated: true, completion: nil)
The alert controller works fine, but it displays the keys I added for NSLocalizedString. So in this case the controller displays:
- alert
- alertmessage
- cancel
But Xcode doesn't pull the corresponding strings from the keys I added in both Main.Strings files. If I would change the key "alert" to "test" the controller would display the word test instead.
So what am I doing wrong here and how to solve this? Do I have to specify in the Main.strings files something I did not or do I have to link to these files? Since changing all the Xcode generated elements in Main.strings works, I though this process is completely automatic? Thanks.