2

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
RjC
  • 827
  • 2
  • 14
  • 33
  • 5
    The mapping should be in "Localizable.strings". – Did you check the return values from NSLocalizedString? – Martin R Oct 29 '17 at 11:52
  • So this is another file in addition to both files Xcode created for both languages? (Main.strings)? – RjC Oct 29 '17 at 11:57
  • That's what I know, see also https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/MaintaingYourOwnStringsFiles/MaintaingYourOwnStringsFiles.html. – Note that this is *not* a problem of UIAlertController. – Martin R Oct 29 '17 at 12:00
  • Thanks! Surprisingly that works! But where would I put these files (especially when I have more than one for different languages)? – RjC Oct 29 '17 at 12:02
  • 1
    I have added a link to Apple's documentation to my previous comment. *"Add the Localizable.strings file to all the language folders."* – Martin R Oct 29 '17 at 12:03
  • 1
    Main.strings contain keys and values for localized strings from Main.stroyboard. – Losiowaty Oct 29 '17 at 19:46

0 Answers0