Swift:如何在枚举值上添加本地化?

标签 swift enums localization

我尝试在 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/

相关文章:

C++ 将枚举传递给对象构造函数

java - MyBatis 将属性映射到错误的 Enum

java - 在这种情况下使用合适的嵌套枚举?

c# - 如何以编程方式检索 EPi 服务器网站的所有已启用语言的列表?

localization - Meteor accounts.ui 本地化

swift - 使用 Task.all 的示例

swift - 变量中的变量

json - HTTP Post 请求 API - 解析用单引号括起来的 JSON

ios - NSLocationWhenInUseUsageDescription 的 Xamarin.iOS 本地化

ios - 如果一个变异函数设置它自己等于另一个函数,这意味着什么