0

I have code to check for insertion only from digits.

How can I translate ": can not be pasted" using Localizable.strings?

let pastAction = UIAlertAction(title: NSLocalizedString("Past",comment: ""), style: .default, handler: {
        (alert: UIAlertAction!) -> Void in
        if UIPasteboard.general.string?.onlyNumbers() == "" {
            let alertController = UIAlertController(title: "Calc Pro", message: "\(UIPasteboard.general.string ?? ""): cannot be pasted", preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alertController, animated: false, completion: nil)
            alertController.view.tintColor = UIColor(colorLiteralRed: 235/255, green: 92/255, blue: 48/255, alpha: 1)
            print("Cannot be pasted")
        } else {
            self.displayResultLabel.text = UIPasteboard.general.string
            print("Pasted")
        }
    })
rmaddy
  • 314,917
  • 42
  • 532
  • 579
BLC
  • 97
  • 7
  • Scan [these search results](https://stackoverflow.com/search?q=%5Bswift%5D+NSLocalizedString+variable) for more solutions. – rmaddy Sep 13 '17 at 05:31

1 Answers1

2

You should just use String interpolation for NSLocalizedString as well. Like this:

"\(UIPasteboard.general.string ?? "") \(NSLocalizedString(": cannot be pasted", comment: ""))"

EDIT: But as @rmmady suggested, when using localized strings with variables, the proper way to handle it is to let your localized string to accept variables

Example:

"%@: cannot be pasted" = "%@ : cannot be pasted"

and call it as follows

String(format: NSLocalizedString("%@: cannot be pasted", comment: ""), "Your variable here")
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ace Rivera
  • 620
  • 4
  • 14