我有一个基本的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中使用文件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;
}
@endgenstrings命令按预期工作,我得到了"key 1"的条目。
$ genstrings -u Test.m
$ cat Localizable.strings
??/* comment 1 */
"key 1" = "value 1";
/* comment 2 */
"key 2" = "key 2";我做错了什么?
发布于 2015-08-14 20:16:00
这是Xcode 6.4和Xcode 7 beta中的genstring错误,正如https://openradar.appspot.com/22133811中所报告的那样
在Swift文件中,genstring通过两个以上的参数对NSLocalizedString调用进行阻塞。 摘要:当针对Swift文件运行genstring时,如果有任何NSLocalizedString调用使用“值”和“注释”参数这两种简单的情况更多,则泛型字符串会出错。..。
发布于 2016-07-01 11:06:52
显然,苹果已经不再支持基因字符串了。相反,请使用:
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的形式运行genstring。
编辑器>本地化导出
命令将“幕后”处理您的字符串。
更新:我使用的是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”;https://stackoverflow.com/questions/32017752
复制相似问题