How to localize TableView
cells in Swift?
如何在Swift中本地化TableView单元格?
For one String variable it's:
对于一个String变量,它是:
let alertTitle = NSLocalizedString("a", comment: "")
But how can I localize array for TableView
?
但是如何为TableView本地化数组呢?
let array = ["a", "b", "c", "d", "e"]
3 个解决方案
#1
4
You need to localize each string separately as you do with with alertTitle
. If you do not use the comment for the localized string (as most people do) you can simplify localization with an extension
您需要像使用alertTitle一样单独本地化每个字符串。如果您不使用本地化字符串的注释(与大多数人一样),您可以使用扩展来简化本地化
extension String {
var localized: String {
return NSLocalizedString(self, comment: "")
}
}
and define your array with
并使用。定义您的数组
let array = ["a".localized, "b".localized, "c".localized, "d".localized, "e".localized]
or even shorter with
甚至更短
let let array = ["a", "b", "c", "d", "e"].map({ $0.localized })
#2
2
Using @sundance's String extension:
使用@ sundance的String扩展:
extension String {
var localized: String {
return NSLocalizedString(self, comment: "")
}
}
I would prefer using this extension over [String]
:
我更喜欢在[String]上使用此扩展名:
extension Array where Element == String {
var localized: [Element] {
return self.map({ $0.localized })
}
}
Then you can use this:
然后你可以使用这个:
let array = ["a", "b", "c", "d", "e"].localized
#3
1
let array = [NSLocalizedString("a", comment: ""), NSLocalizedString("b", comment: ""), NSLocalizedString("c", comment: ""), NSLocalizedString("d", comment: ""), NSLocalizedString("e", comment: "")]
some way I like when localizing group of related data
在本地化相关数据组时我喜欢某种方式
let arrayLocalized = [string]()
for i in 1...10
{
arrayLocalized.append(NSLocalizedString("a\(i)", comment: ""))
}
where localization file looke like this
本地化文件喜欢这样的地方
"a1"="first value";
"a2"="second value";
.
.
.
"a10"="last value";
#1
4
You need to localize each string separately as you do with with alertTitle
. If you do not use the comment for the localized string (as most people do) you can simplify localization with an extension
您需要像使用alertTitle一样单独本地化每个字符串。如果您不使用本地化字符串的注释(与大多数人一样),您可以使用扩展来简化本地化
extension String {
var localized: String {
return NSLocalizedString(self, comment: "")
}
}
and define your array with
并使用。定义您的数组
let array = ["a".localized, "b".localized, "c".localized, "d".localized, "e".localized]
or even shorter with
甚至更短
let let array = ["a", "b", "c", "d", "e"].map({ $0.localized })
#2
2
Using @sundance's String extension:
使用@ sundance的String扩展:
extension String {
var localized: String {
return NSLocalizedString(self, comment: "")
}
}
I would prefer using this extension over [String]
:
我更喜欢在[String]上使用此扩展名:
extension Array where Element == String {
var localized: [Element] {
return self.map({ $0.localized })
}
}
Then you can use this:
然后你可以使用这个:
let array = ["a", "b", "c", "d", "e"].localized
#3
1
let array = [NSLocalizedString("a", comment: ""), NSLocalizedString("b", comment: ""), NSLocalizedString("c", comment: ""), NSLocalizedString("d", comment: ""), NSLocalizedString("e", comment: "")]
some way I like when localizing group of related data
在本地化相关数据组时我喜欢某种方式
let arrayLocalized = [string]()
for i in 1...10
{
arrayLocalized.append(NSLocalizedString("a\(i)", comment: ""))
}
where localization file looke like this
本地化文件喜欢这样的地方
"a1"="first value";
"a2"="second value";
.
.
.
"a10"="last value";