2017-09-13 125 views
0

我有代碼檢查插入只能從數字。應用程序本地化(IOS)

如何使用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") 
     } 
    }) 
+0

掃描[這些搜索結果] (https://stackoverflow.com/search?q=%5Bswift%5D+NSLocalizedString+variable)以獲取更多解決方案。 – rmaddy

回答

1

你應該爲NSLocalizedString使用字符串插值。 像這樣:

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

編輯:但作爲@rmmady建議,使用帶有變量本地化的字符串時,處理它的正確方法是讓你的本地化字符串接受變量

例子:

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

並調用它如下

String(format: NSLocalizedString("%@: cannot be pasted", comment: ""), "Your variable here") 
+0

謝謝,它的工作原理)) – BLC

+0

不,這不是用變量進行本地化的正確方法。如果在某些語言中,字符串需要在消息中的不同位置進行操作呢? – rmaddy

+0

我同意你@rmaddy,但我認爲這就是爲什麼他使用「**:**」來防止不同語言發生混淆 –