有没有一种简单的方法可以使用 swift 将 NSLocalizedStrings 放入数组中?
Is there a simple way to put NSLocalizedStrings in an array using swift?
我想在 swift 应用程序中添加一个本地化字符串数组作为集合视图的数据源。
我写了这个:
let text = [NSLocalizedString("s0",comment: ""),NSLocalizedString("s1",comment: ""),NSLocalizedString("s2",comment: ""),NSLocalizedString("s3",comment: ""),NSLocalizedString("s4",comment: ""),NSLocalizedString("s5",comment: ""),NSLocalizedString("s6",comment: ""),NSLocalizedString("s7",comment: ""),NSLocalizedString("s8",comment: "")]
有没有更简单的方法将所有字符串放入数组中,因为我有 1000 个本地化字符串,这看起来不对。
您始终可以使用一种格式(例如 s%d.4 )设置您的字符串并围绕该格式构建您的数组。
var text:NSMutableArray = NSMutableArray()
for index in 1...10 {
let formattedString = String(format: "s%.4d", index)
let localizedString = NSLocalizedString(formattedString, comment: "comment")
text.addObject(localizedString)
}
然后声明字符串,如:s0001、s0002、s0003 等
或者您也可以这样做:
HelperClass:
static func getLocalized(withKey key: String, targetSpecific:Bool) -> String{
if targetSpecific{
return NSLocalizedString(key, tableName:"TargetSpecific", comment: "")
}
else{
return NSLocalizedString(key, comment: "")
}
}
static func getLocalizedArray(withKey key: String, targetSpecific:Bool) -> [String]{
return getLocalized(withKey: key, targetSpecific: targetSpecific).components(separatedBy: ",")
}
Localizable.string
"optionsArray" = "Show devices,Add device";
所以你可以这样称呼它:
let optionsArray:[String] = AppHelper.getLocalizedArray(withKey: "optionsArray", targetSpecific: false)
编辑:意识到你可以分两步而不是三步来写这个。简化了我的回答。
编写一个辅助函数来定位数组
public func Localized(_ key: String) -> String {
return NSLocalizedString(key, comment: "")
}
使用地图调用
// for example
let localized = ["s0", "s1", "s2"].map { Localized([=11=]) }
我想在 swift 应用程序中添加一个本地化字符串数组作为集合视图的数据源。
我写了这个:
let text = [NSLocalizedString("s0",comment: ""),NSLocalizedString("s1",comment: ""),NSLocalizedString("s2",comment: ""),NSLocalizedString("s3",comment: ""),NSLocalizedString("s4",comment: ""),NSLocalizedString("s5",comment: ""),NSLocalizedString("s6",comment: ""),NSLocalizedString("s7",comment: ""),NSLocalizedString("s8",comment: "")]
有没有更简单的方法将所有字符串放入数组中,因为我有 1000 个本地化字符串,这看起来不对。
您始终可以使用一种格式(例如 s%d.4 )设置您的字符串并围绕该格式构建您的数组。
var text:NSMutableArray = NSMutableArray()
for index in 1...10 {
let formattedString = String(format: "s%.4d", index)
let localizedString = NSLocalizedString(formattedString, comment: "comment")
text.addObject(localizedString)
}
然后声明字符串,如:s0001、s0002、s0003 等
或者您也可以这样做:
HelperClass:
static func getLocalized(withKey key: String, targetSpecific:Bool) -> String{
if targetSpecific{
return NSLocalizedString(key, tableName:"TargetSpecific", comment: "")
}
else{
return NSLocalizedString(key, comment: "")
}
}
static func getLocalizedArray(withKey key: String, targetSpecific:Bool) -> [String]{
return getLocalized(withKey: key, targetSpecific: targetSpecific).components(separatedBy: ",")
}
Localizable.string
"optionsArray" = "Show devices,Add device";
所以你可以这样称呼它:
let optionsArray:[String] = AppHelper.getLocalizedArray(withKey: "optionsArray", targetSpecific: false)
编辑:意识到你可以分两步而不是三步来写这个。简化了我的回答。
编写一个辅助函数来定位数组
public func Localized(_ key: String) -> String {
return NSLocalizedString(key, comment: "")
}
使用地图调用
// for example
let localized = ["s0", "s1", "s2"].map { Localized([=11=]) }