【问题标题】:genstrings chokes when using the value parameter in Swift在 Swift 中使用 value 参数时,genstrings 窒息
【发布时间】:2015-08-14 19:56:18
【问题描述】:

我有一个基本的 Swift 文件 Test.swift,其中包含

import Foundation
import UIKit

class Test: NSObject {
    let a: String
    let b: String

    override init() {
        a = NSLocalizedString("key 1", tableName: nil,
            bundle: NSBundle.mainBundle(), value: "value 1", comment: "comment 1")
        b = NSLocalizedString("key 2", comment: "comment 2")
    }
}

当我在这个文件上运行 genstrings 时,我收到了一个意外警告

$ genstrings -u Test.swift
Bad entry in file Test.swift (line = 9): Argument is not a literal string.

并且生成的Localizable.strings 文件缺少"key 1" 的条目

$ cat Localizable.strings 
??/* comment 2 */
"key 2" = "key 2";

但是,当我在 Objective-C 中使用文件 Test.m 中的以下代码执行等效操作时

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface Test: NSObject

@property (nonatomic, strong) NSString *a;
@property (nonatomic, strong) NSString *b;

@end

@implementation Test

- (id)init {
    self = [super init];
    if (self) {
        self.a = NSLocalizedStringWithDefaultValue(@"key 1", nil, [NSBundle mainBundle], @"value 1", @"comment 1");
        self.b = NSLocalizedString(@"key 2", @"comment 2");
    }
    return self;
}

@end

genstrings 命令按预期工作,我得到了"key 1" 的条目。

$ genstrings -u Test.m 
$ cat Localizable.strings 
??/* comment 1 */
"key 1" = "value 1";

/* comment 2 */
"key 2" = "key 2";

我做错了什么?

【问题讨论】:

标签: ios objective-c swift genstrings


【解决方案1】:

显然,Apple 已经不再支持 genstrings。而是使用:

xcrun extractLocStrings

听从你的命令。例如,为您的项目创建 Localizable.strings:

find ./ -name "*.m" -print0 | xargs -0 xcrun extractLocStrings -o en.lproj

对于 Swift:

find ./ -name "*.swift" -print0 | xargs -0 xcrun extractLocStrings -o en.lproj

请注意,如果您要导出到 .xliff 文件,则根本不需要像 xCode 那样运行 genstrings

编辑器 > 本地化导出

命令将在“幕后”处理您的字符串。

更新: 我在 xCode 7.3.1 上,在我的系统上 xtractLocStrings 是一个二进制文件。

$ file /Applications/Xcode.app//Contents/Developer/usr/bin/extractLocStrings
    /Applications/Xcode.app//Contents/Developer/usr/bin/extractLocStrings: Mach-O 64-bit executable x86_64

这是我的测试:

let _ = NSLocalizedString("1st", comment: "1st string")
let _ = NSLocalizedString("Second", tableName: "Localized", bundle: NSBundle.mainBundle(), value: "2nd", comment: "2nd string”)

结果如下:

Localizable.strings:
/* 1st string */
"1st" = "1st”;

Localized.strings:
/* 2nd string */
"Second" = "2nd”;

【讨论】:

  • 这似乎不能解决问题。我得到完全相同的错误消息。 extractLocStrings 的帮助输出也表明它只是 genstrings 的包装。
  • 当我在您提供的测试代码上运行它时,我在 extractLocStrings 中得到一个 index-out-of-bounds 异常。当我在问题中的代码示例上运行它时,我得到了 genstrings 报告的相同错误,并且字符串文件仅包含一个字符串。有什么方法可以检查我们俩都在运行什么版本的 extractLocStrings/genstrings?老实说,我很惊讶地看到了不同。
  • 感谢 Cliff,extractLocStrings 的第二个命令在 Xcode 7.3.1 中使用我的 Swift 文件为我工作。
【解决方案2】:

这是 Xcode 6.4 和 Xcode 7 beta 中的 genstrings 错误,因为 报道于https://openradar.appspot.com/22133811:

在 Swift 文件中, genstrings 在 NSLocalizedString 调用时阻塞更多 多于两个参数

总结: 当对 Swift 文件运行 genstrings 时,如果有 任何 NSLocalizedString 调用使用的不仅仅是简单的情况 “value”和“comment”参数,genstrings 出错了。 ...

【讨论】:

  • 我花了很长时间在网上搜索这个问题,但找不到任何有用的东西,然后发现有一个雷达使用几乎相同的措辞。也许我使用了错误的搜索引擎。 :D 谢谢!
  • 我刚刚为此创建了一个雷达...希望这将得到修复,但现在我必须找到一些丑陋的解决方法(从 ObjC 文件加载本地化字符串):-/
  • 这是 Xcode 6、Xcode 7、Xcode 8 的 bug,Apple 永远不会解决。他们有足够的时间来做这件事而且不在乎。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 2016-08-26
  • 2010-10-08
  • 1970-01-01
  • 2011-02-11
相关资源
最近更新 更多