在 Swift 中使用值参数时,genstrings 会阻塞
genstrings chokes when using the value parameter in 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")
}
}
当我 运行 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";
我做错了什么?
这是 Xcode 6.4 和 Xcode 7 beta 中的 genstrings 错误,因为
在 https://openradar.appspot.com/22133811 中报告:
In Swift files, genstrings Chokes On NSLocalizedString calls with more
than two parameters
Summary: When running genstrings against a Swift file, if there are
any NSLocalizedString calls that use more than the trivial case of
"value" and "comment" parameters, genstrings errors out. ...
显然 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 文件,则根本不再需要 运行 genstrings,因为 xCode
编辑器 > 本地化导出
命令将处理您的字符串 'behind the scenes'。
更新:
我在 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”;
我有一个基本的 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";
我做错了什么?
这是 Xcode 6.4 和 Xcode 7 beta 中的 genstrings 错误,因为 在 https://openradar.appspot.com/22133811 中报告:
In Swift files, genstrings Chokes On NSLocalizedString calls with more than two parameters
Summary: When running genstrings against a Swift file, if there are any NSLocalizedString calls that use more than the trivial case of "value" and "comment" parameters, genstrings errors out. ...
显然 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 文件,则根本不再需要 运行 genstrings,因为 xCode
编辑器 > 本地化导出
命令将处理您的字符串 'behind the scenes'。
更新: 我在 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”;