LoginSignup
1
2

More than 1 year has passed since last update.

【SwiftUI】言語設定を作成する

Posted at

はじめに

アプリ内で言語の設定ができるようにPickerを作成しました。

こんな感じ

スクリーンショット 2023-03-31 21.23.55.png

実装

public extension Locale {
    static var availableLocalizedLocales: [String] {
        return Locale.availableIdentifiers.compactMap {
            Locale(identifier: $0).localizedString(forIdentifier: $0)
        }
        .sorted()
    }

    var languageName: String {
        localizedString(forLanguageCode: language.languageCode?.identifier ?? "") ?? ""
    }
}
import SwiftUI

struct ContentView: View {
    @State private var selectedLanguage: String = ""
    var body: some View {
        Picker(selection: $selectedLanguage) {
            ForEach(Locale.availableLocalizedLocales, id: \.self) { localizedLocales in
                Text(localizedLocales).tag(localizedLocales)
            }
        } label: {
            Text("Language")
        }
        .pickerStyle(.menu)
        .padding()
    }
}

おわり

この実装のいいところは自分で言語リストを作らなくていいところです!
標準で用意されている言語リストを使用しています

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2