请教如何在插值
Closed this issue · 2 comments
xiky commented
我有这样一条LocalizedString
"hello_somethine" = "Hello %@"
会生成这样一个方法
/// Hello %@
public static func helloSomething(_ p1: Any) -> String {
return tr("Localizable", "hello_something", String(describing: p1))
}
fileprivate func tr(_ table: String, _ key: String, _ locale: Locale = Locale.current, _ args: CVarArg...) -> String {
let path = Bundle.main.path(forResource: locale.identifier, ofType: "lproj") ?? ""
let format: String
if let bundle = Bundle(path: path) {
format = NSLocalizedString(key, tableName: table, bundle: bundle, comment: "")
} else {
format = BundleToken.bundle.localizedString(forKey: key, value: nil, table: table)
}
return String(format: format, locale: locale, arguments: args)
}
其中调用的 tr 方法,第三个参数是 Locale 类型,会报错
Cannot convert value of type 'String' to expected argument type 'Locale'
是否应该将生成的 p1 进行插值到这里呢?
/// Hello %@
public static func helloSomething(_ p1: Any) -> String {
return tr("Localizable", "hello_something \(String(describing: p1))")
}
如果是的话,String 模板应该如何修改?