问题描述
有人可以帮助我吗?我在Swift UI中找不到任何有关本地化的描述.任何人都可以请教或更好地举例说明如何进行本地化,例如Text()
?
Can anybody help me? I can't find any description of the localization in Swift UI. Can anyone please give advice or better an example of how to localize for example Text()
?
推荐答案
当您查看Text文档时,您会发现它在其初始值设定项中使用LocalizedStringKey而不是String:
When you look at documentation for Text you can see that it takes LocalizedStringKey not a String into its initializer:
init(_ key: LocalizedStringKey, tableName: String? = nil, bundle: Bundle? = nil, comment: StaticString? = nil)
它使本地化非常简单.您所要做的就是:
It make localizing very straightforawar. All you have to do is:
- 创建一个类型为Strings File的新文件,将其命名为Localizable
- 选择新文件并在右侧面板中导航到File Inspector,然后单击Localize ...
- 将您的项目文件转到本地化"部分,然后在列表中添加另一种语言-Xcode将为您创建本地化文件
当您选择Localizable.strings时,您将看到它包含原始语言和刚添加的语言的文件.那就是您要翻译的地方,即键-本地化的文本对.
When you select you Localizable.strings you will see that it contains files for the original language and the language you have just added. That's where you put your translations, i.e. key - localized text pairs.
如果您有这样的文字,则是您的应用程序:
If you have a text like this is your app:
Text("Hello World!")
您现在必须在您的Localizable中添加翻译字符串:
You have to now add to your Localizable.strings your translations:
您的基本语言:
"Hello World!" = "Hello World!";
以及您的第二种语言(在本例中为德语):
and for your second language (in this case German):
"Hello World!" = "Hallo Welt!";
要查看已本地化的预览,可以按以下方式定义它们:
To see your previews localised you can define them like this:
struct ContentViewView_Previews: PreviewProvider {
static var previews: some View {
ForEach(["en", "de"], id: \.self) { id in
ContentView()
.environment(\.locale, .init(identifier: id))
}
}
}
这篇关于如何在Swift UI中实现本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!