是否有与 NSLocalizedString(...)
等效的 Swift?
在Objective-C
中,我们通常使用:
NSString *string = NSLocalizedString(@"key", @"comment");
如何在 Swift 中实现相同的目的?我找到了一个函数:
func NSLocalizedString(
key: String,
tableName: String? = default,
bundle: NSBundle = default,
value: String = default,
#comment: String) -> String
但是,它很长,一点也不方便。
最佳答案
我使用下一个解决方案:
1) 创建扩展:
extension String {
var localized: String {
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
}
}
2) 在Localizable.strings 文件中:
"Hi" = "Привет";
3) 使用示例:
myLabel.text = "Hi".localized
尽情享受吧! ;)
--更新:--
对于带有注释的情况,您可以使用此解决方案:
1) 扩展:
extension String {
func localized(withComment:String) -> String {
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: withComment)
}
}
2) 在 .strings 文件中:
/* with !!! */
"Hi" = "Привет!!!";
3) 使用:
myLabel.text = "Hi".localized(withComment: "with !!!")
关于ios - Swift 中的 NSLocalizedString 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25081757/