【发布时间】:2020-05-03 17:18:58
【问题描述】:
我正在使用一个简单的视图在 NumberFormatter 中显示特定语言环境的文本
struct CurrencyView: View {
let currency:Currency
var body: some View {
Text(currency.getFormattedCurrency())
}
}
struct CurrencyView_Previews: PreviewProvider {
static var previews: some View {
CurrencyView(currency: Currency(currencyValue: 1.0)).previewLayout(.fixed(width: 300.0, height: 55.0)).environment(\.locale, .init(identifier: "fr_Fr"))
}
}
struct Currency{
let currencyValue:Double
func getFormattedCurrency() -> String{
let formatter = NumberFormatter()
formatter.numberStyle = .currency
let formatterCurrency = formatter.string(for: self.currencyValue) ?? ""
return formatterCurrency
}
}
我期待预览会以 French 显示货币和€,正如我已经在预览中的语言环境中提到的那样。
【问题讨论】:
标签: localization swiftui nsnumberformatter