Do I have to hard-code / localise myself the "OK" button when generating an alert in swift?

时间:2016-02-03 03:47:46

标签: ios swift localization

I have a convenience method in Swift (iOS 8+) for displaying an error message. So far it looks like this:

// Supplied with a category code and an error code, generates an error dialogue box.
// Codes rather than strings because this needs to be localisable.
func showErrorDialogue(categoryCode: String, _ errorCode: String) -> () {
    // Fetch the actual strings from the localisation database.
    let localisedCategory = NSLocalizedString(categoryCode, comment: categoryCode)
    let localisedError    = NSLocalizedString(errorCode,    comment: errorCode)

    // Create an alert box
    let alertController = UIAlertController(
        title:          localisedCategory,
        message:        localisedError,
        preferredStyle: .Alert
    )
    alertController.addAction(
        UIAlertAction(
            title:   "OK", // FIXME: why isn't this localised?
            style:   .Default,
            handler: { (action) in return }
        )
    )
    self.presentViewController(alertController, animated: true) { return }
}

It seems odd that I can't just say "I'm only adding one button to this alert box, so please assume it's going to be the locale-default OK button". The best solution I've found so far with limited Googling appears to be Steal them from the System and hope which is more than a little dodgy.

1 个答案:

答案 0 :(得分:0)

As far as I know, there are no better ways to do this than stealing it from the system like you found.

Though it's not the hardest thing to localize, I agree it would be great if Apple made that automatic in a future version of the OS.