【问题标题】:Is there a way to make `genstrings` work with `LocalizedStringKey`?有没有办法让 `genstrings` 与 `LocalizedStringKey` 一起工作?
【发布时间】:2020-06-10 09:44:20
【问题描述】:

有没有办法让 Apple 的 genstrings 命令行工具识别从 SwiftUI 的 LocalizedStringKey 初始化程序定义的可本地化字符串键?

对于这个输入文件 (testing-genstrings.swift): ...

import UIKit
import SwiftUI

enum L10n {
    static let test0 = NSLocalizedString("TEST0", comment: "")

    static let test1 = LocalizedStringKey("TEST1")

    static func test2(_ parameter: String) -> LocalizedStringKey {
        return LocalizedStringKey("TEST2_\(parameter)")
    }

    static func test3(_ parameter: String) -> String {
        return NSLocalizedString("TEST3_\(parameter)", comment: "")
    }

    static func test4(_ parameter: String) -> String {
        return String.localizedStringWithFormat(NSLocalizedString("TEST4", comment: ""), parameter)
    }
}

let test5 = "TEST5"
let test6 = "TEST6"
let test7 = "TEST7"

struct TestView: View {
    var body: some View {
        VStack {
            Text(L10n.test0)
            Text(L10n.test1)
            Text(L10n.test2("foo"))
            Text(L10n.test3("bar"))
            Text(test5)
            Text(LocalizedStringKey(test6))
            Text(NSLocalizedString(test7, ""))
            Text("TEST8")
            Text("TEST9_\("baz")")
        }
    }
}

...genstrings 生成此输出:

$ genstrings -SwiftUI -s LocalizedStringKey testing-genstrings.swift && iconv -c -f utf-16 -t utf-8 Localizable.strings
genstrings: error: bad entry in file testing-genstrings.swift (line = 9): Argument is not a literal string.
genstrings: error: bad entry in file testing-genstrings.swift (line = 11): Argument is not a literal string.
genstrings: error: bad entry in file testing-genstrings.swift (line = 12): Argument is not a literal string.
genstrings: error: bad entry in file testing-genstrings.swift (line = 36): Argument is not a literal string.
genstrings: error: bad entry in file testing-genstrings.swift (line = 37): Argument is not a literal string.
genstrings: error: bad entry in file testing-genstrings.swift (line = 37): Argument is not a literal string.
/* No comment provided by engineer. */
"bar" = "bar";

/* No comment provided by engineer. */
"foo" = "foo";

/* No comment provided by engineer. */
"TEST0" = "TEST0";

/* No comment provided by engineer. */
"TEST3_\(parameter)" = "TEST3_\(parameter)";

/* No comment provided by engineer. */
"TEST4" = "TEST4";

/* No comment provided by engineer. */
"TEST8" = "TEST8";

/* No comment provided by engineer. */
"TEST9_%@" = "TEST9_%@";

您可以看到它识别通过NSLocalizedString 和Text 的初始化器Text() 定义的键,该初始化器使用ExpressibleByStringInterpolation(示例中为TEST9_%@),但在使用@987654332 定义的所有键上都失败@。

【问题讨论】:

    标签: ios swift swiftui genstrings


    【解决方案1】:

    genstrings 比较幼稚。它正在寻找一个有两个参数的函数,第一个未命名,第二个命名为“comment”。

    如果您添加了以下扩展:

    public extension LocalizedStringKey {
        init(_ value: String, comment: String) {
            self.init(value)
        }
    }
    

    并且一直使用它,您可以通过将-s LocalizedStringKey 传递给genstrings 来使用LocalizedStringKey。

    请记住,如果您将LocalizedStringKey 声明为返回类型或变量,也会产生genstrings 错误。因此,您需要一个单独的 typealias LocalizedStringKeyResult = LocalizedStringKey,在引用 LocalizedStringKey 时使用它,但不希望 genstrings 抱怨。

    当然,你不会得到你想要的插值,因为 genstrings 仅适用于 Text。

    真正的答案是……不要使用LocalizedStringKey。尽可能使用Text(以获得插值)。不能使用时使用NSLocalizedString。

    【讨论】:

      猜你喜欢
      • 2020-03-09
      • 2013-03-27
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      • 2023-03-28
      • 2011-01-13
      相关资源
      最近更新 更多