2017-09-13 119 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,但我认为这就是为什么他使用“**:**”来防止不同语言发生混淆 –