有没有办法使genstrings与LocalizedStringKey一起使用? [英] Is there a way to make `genstrings` work with `LocalizedStringKey`?

查看:76
本文介绍了有没有办法使genstrings与LocalizedStringKey一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使Apple的 genstrings 命令行工具识别从 SwiftUI LocalizedStringKey 初始值设定项定义的可本地化的字符串键?

Is there a way get Apple's genstrings command line tool to recognize localizable string keys defined from SwiftUI's LocalizedStringKey initializer?

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

For this input file (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 generates this output:

$ 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 _%@ ),但使用 LocalizedStringKey 定义的所有键均失败.

You can see that it recognizes the keys defined via NSLocalizedString and Text's initializer Text() initializer that uses ExpressibleByStringInterpolation (TEST9_%@ in the example), but fails on all keys defined using LocalizedStringKey.

推荐答案

genstrings 相对幼稚.它正在寻找具有两个参数的函数,第一个未命名,第二个命名为"comment".

genstrings is relatively naive. It is looking for a function with two parameters, the first unnamed, the second named "comment".

如果添加了以下扩展名:

If you added the following extension:

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

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

and always used that, you'd be able to use LocalizedStringKey by passing -s LocalizedStringKey to genstrings.

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

Keep in mind that if you declared LocalizedStringKey as a return type or a variable, that would give a genstrings error, too. So you'd need a separate typealias LocalizedStringKeyResult = LocalizedStringKey that you use when reference LocalizedStringKey but don't want genstrings to complain.

当然,您不会获得所需的插值,因为 genstrings 仅适用于 Text .

And, of course, you wouldn't get the interpolation you want because genstrings applies that only to Text.

真正的答案是...不要使用 LocalizedStringKey .如果可以,请使用 Text (以获取插值).不能使用时,请使用 NSLocalizedString .

The real answer is... don't use LocalizedStringKey. Use Text when you can (to get interpolation). Use NSLocalizedString when you can't.

这篇关于有没有办法使genstrings与LocalizedStringKey一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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