在SwiftUI中使用字符串插值进行本地化 [英] Localization with String interpolation in SwiftUI

查看:66
本文介绍了在SwiftUI中使用字符串插值进行本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对SwiftUI Watch应用进行本地化.我对静态字符串没有任何问题.我在文本"视图中使用LocalizedKeyStrings,并将翻译添加到Localizable.strings文件中.例如:

I am trying to localize my SwiftUI Watch app. I don't have any problems with static strings. I use LocalizedKeyStrings in my Text views and add my translations in Localizable.strings files. For example:

Text("history")

在Localizable.strings中:

in Localizable.strings:

"history" = "Historique";

结果:历史记录"

但是我也想使用插值法来定位st.例如:

But I also want to localize stings using interpolation. For example:

Text("startCustom \(format: "%.1f",customDistance)")

在Localizable.strings中,我尝试使用不同的语法:

In Localizable.strings, I have tried with different syntax:

"startCustom %@" = "Course de %@ km";

"startCustom %f" = "Course de %f km";

"startCustom %.1f" = "Course de %.1f km";

没有任何效果.我没有找到任何相关文档...

Nothing works. I don't find any documentation for that ...

推荐答案

以下简单的方法就可以正常工作(已通过Xcode 11.4测试)

The following simple just works (tested with Xcode 11.4)

Text(String(format: NSLocalizedString("startCustom %.1f", comment: ""), 
     self.customDistance))

Localizable.string具有

with Localizable.string having

"startCustom %.1f" = "Course de %.1f km";

这篇关于在SwiftUI中使用字符串插值进行本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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