在Swift中使用value参数时,genstrings会出现阻塞

hen*_*nes 12 objective-c genstrings ios swift

我有一个基本的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")
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

$ cat Localizable.strings 
??/* comment 2 */
"key 2" = "key 2";
Run Code Online (Sandbox Code Playgroud)

但是,当我在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
Run Code Online (Sandbox Code Playgroud)

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

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

/* comment 2 */
"key 2" = "key 2";
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Cli*_*iff 27

显然苹果已经放弃了支持genstrings.而是使用:

xcrun extractLocStrings
Run Code Online (Sandbox Code Playgroud)

作为你的命令.例如,要为项目创建Localizable.strings:

find ./ -name "*.m" -print0 | xargs -0 xcrun extractLocStrings -o en.lproj
Run Code Online (Sandbox Code Playgroud)

对于Swift:

find ./ -name "*.swift" -print0 | xargs -0 xcrun extractLocStrings -o en.lproj
Run Code Online (Sandbox Code Playgroud)

请注意,如果要导出到.xliff文件,则根本不再需要运行genstrings作为xCode

编辑>导出本地化

命令将在幕后处理你的字符串.

更新:我在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
Run Code Online (Sandbox Code Playgroud)

这是我的测试:

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

以下是结果:

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

Localized.strings:
/* 2nd string */
"Second" = "2nd”;
Run Code Online (Sandbox Code Playgroud)

  • 这似乎不能解决问题。我收到完全相同的错误消息。`extractLocStrings` 的帮助输出也表明它只不过是 `genstrings` 的一个包装器。 (3认同)

Mar*_*n R 14

这是Xcode 6.4和Xcode 7 beta中的genstrings错误,如https://openradar.appspot.com/22133811中所述:

在Swift文件中,genstrings Chokes On NSLocalizedString调用具有两个以上的参数

简介:当针对Swift文件运行genstrings时,如果有任何NSLocalizedString调用使用的不仅仅是"value"和"comment"参数的简单情况,那么genstrings就会出错....

  • 这是Xcode 6,Xcode 7,Xcode 8的一个错误,Apple永远不会解决.他们有足够的时间这样做而且不在乎. (3认同)