Swift: Как добавить локализацию для значений перечисления? - PullRequest
0 голосов
/ 21 апреля 2020

Я пытаюсь добавить локализацию в свое приложение в SwiftUI, и оно работает нормально на чистом тексте ("..."). Но не работает с перечислениями.

Я использую значение перечисления, как показано ниже, header.name.text = HomeSection.allCases[indexPath.section].rawValue. И я провел эксперимент на case1 - NowPlaying, однако он не работает, после запуска приложения отображается текст «NowPlayingSection».

Так что же мне делать, чтобы иметь дело с enum при локализации?

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()
            }
        }

Enums

enum HomeSection: String, CaseIterable {
    case NowPlaying = "NowPlayingSection"
    case Popular
    case Upcoming
    case TopActor = "Hot Actors"
}

Localizable.strings

"NowPlayingSection" = "Now playing";

1 Ответ

1 голос
/ 21 апреля 2020

На самом деле то, что вы делаете, не является частью SwiftUI, поэтому используйте с NSLocalizedString

header.name.text = 
   NSLocalizedString(HomeSection.allCases[indexPath.section].rawValue, 
       comment: "")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...