[SwiftUI]DatePickerの言語を設定するには?

SwiftUI

どうも、ちょげ(@chogetarou)です。

DatePickerの言語を設定する方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

DatePickerの言語を設定するには、environment修飾子を使います。

まず、DatePickerにenvironment修飾子を付与します。

そして、environementの第1引数に「\.locale」、第2引数にLocale()を指定します。

言語は、Localeの引数「identifier」で設定します。

DatePicker("", selection: $date)
    .environment(\.locale, Locale(identifier: "ロケール識別子"))

Localeの引数「identifier」に指定されたロケール識別子の言語が、DatePickerに設定されます。

使用例

以下は、使用例です。

struct ContentView: View {
    @State var date = Date()
    var body: some View {
        VStack{
            DatePicker("ドイツ語", selection: $date)
                .environment(\.locale, Locale.init(identifier: "de_DE"))
        }
    }
}

コメント

タイトルとURLをコピーしました