SwiftUI-动态文本的本地化 [英] SwiftUI - Localization of a dynamic Text

查看:67
本文介绍了SwiftUI-动态文本的本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力处理我的一些TextField.通常,如果我要翻译的文本是这样硬编码的,则Text()或TextField()的常规"本地化在我的应用程序中不会出现任何问题:

I am struggling with the locilization of some of my TextFields. Usually a "normal" localization of a Text() or TextField() works without any problem in my App if the text I want to translate is hardcoded like this:

Text("English Text")

我将其翻译成我的Localizable.strings:

I translate it in my Localizable.strings like this:

"English Text" = "German Text";

现在,我想翻译更具动态性的TextField,但是我知道每个可能的条目:

Now I want to translate TextFields which are more dynamic, but where I know each possible entry:

                TextField("New note" + (refresh ? "" : " "),text: $newToDo, onCommit: {
                    self.addToDo()
                    self.refresh.toggle()
                })

(由于SwiftUI错误有时不再次显示占位符文本,因此刷新是必要的.)

(The refresh is necessary because of a SwiftUI bug sometimes not showing the placeholder-text again.)

另一个例子是:

    func dayWord() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone.current
        dateFormatter.locale = Locale(identifier: "de_DE")
        dateFormatter.dateFormat = "EEEE"
        return dateFormatter.string(from: self)
    }

    var day: String {
        return data.date.dateFromMilliseconds().dayWord()
    }

   Text(day.prefix(2))

Text(day.prefix(2))只有七个可能的状态,但是我不知道在Localizable.strings中要写什么作为键.

The Text(day.prefix(2)) has only seven possible states, but I don't know what to write as a key in my Localizable.strings.

推荐答案

使用NSLocalizedString,例如

Use NSLocalizedString, like

TextField(NSLocalizedString("New note", comment: "") + (refresh ? "" : " "), ...

这篇关于SwiftUI-动态文本的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆