我尝试在 SwiftUI 中的应用程序上添加本地化,并且它在纯文本(“...”)上运行良好。但不适用于枚举。
我使用枚举的值,如下所示,header.name.text = HomeSection.allCases[indexPath.section].rawValue
。我对 case1 - NowPlaying 做了一个实验,但是它不起作用,应用程序启动后显示文本“NowPlayingSection”。
那么我应该如何处理本地化中的枚举?
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionView.elementKindSectionHeader:
guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: HeaderView.reuseId, for: indexPath) as? HeaderView else {
return UICollectionReusableView()
}
header.name.text = HomeSection.allCases[indexPath.section].rawValue
header.onSeeAllClicked = { [weak self] in
print("%%% Click %$$$$")
self?.parent.seeAllforSection(HomeSection.allCases[indexPath.section])
}
return header
default:
return UICollectionReusableView()
}
}
枚举
enum HomeSection: String, CaseIterable {
case NowPlaying = "NowPlayingSection"
case Popular
case Upcoming
case TopActor = "Hot Actors"
}
可本地化的字符串
"NowPlayingSection" = "Now playing";
最佳答案
实际上你所做的不是 SwiftUI 部分,所以与 NSLocalizedString
一起使用
header.name.text =
NSLocalizedString(HomeSection.allCases[indexPath.section].rawValue,
comment: "")
关于Swift:如何在枚举值上添加本地化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61341694/