【发布时间】: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";
我做错了什么?
【问题讨论】:
-
如需
genstrings的替代品,它确实有效且不会太慢,请查看github.com/kayak/SwiftGenStrings。
标签: ios objective-c swift genstrings