wavky/SwiftGenConfigForSwiftUI

请教如何在插值

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 模板应该如何修改?

wavky commented

@xiky
你好呀。
哎其实我是搞 Android 的啦,iOS 当初摸了一下,现在都全忘记咯,你写的我也看不太懂了现在,这个项目是国外大佬写的,我只是改了一下配置文件啦,你去原来的仓库里问问大佬们比较好哦

机票:https://github.com/SwiftGen/SwiftGen

xiky commented

@wavky 好的,谢谢大佬~