我以编程方式创建Localizable,strings文件。我从服务器下载文件并显示该文件的本地化。
但是如果文件中有错误,那么我的本地化就不起作用了。它向我展示了钥匙。如果在服务器上我编辑本地化文件,我添加了字符串
"程序hello_world"你好
此处,密钥正确但值不正确。格式应为
"程序hello_world" ="你好&#34 ;;
如果我的Localizable.strings文件不包含任何错误且有效,我如何在运行时以编程方式检查。
请帮忙
答案 0 :(得分:72)
使用终端中的plutil
:
plutil -lint Localizable.strings
答案 1 :(得分:26)
除了@ Aderstedt的回答:
plutil -lint Localizable.strings
确实有效,但你必须为每个版本的文件运行它。 E.g
cd
到您的项目根目录cd en.lproj
- 您可以将其替换为您正在使用的任何本地化。plutil -lint Localizable.strings
当您运行第3步时,您将看到错误,告诉您文件有什么问题。或者你会被告知文件没问题
答案 2 :(得分:9)
如上所述plutil
(属性列表实用程序)是一个很好的工具,可以在您手动编辑.plist
和.strings
文件时对其进行验证。您可以将其与.strings
结合使用,将其应用于您的所有find
文件。在项目目录中运行
find . -name *.strings -exec plutil -lint {} \;
或使用
find . -path ./DerivedData -prune -o -name *.strings -exec plutil -lint {} \;
如果您想要排除DerivedData
目录(就像我通常那样)。
答案 3 :(得分:1)
我遇到了同样的问题,发现复数并不是详细的'足够。那些编辑文件的人想要的东西可以更准确地告诉他们什么是错误的
plutil太宽泛了。
所以我写了一个快速而又脏的java工具来测试一个字符串文件:
https://github.com/Daij-Djan/parseAndValidateAppleStringsFile
免责声明:我的代码
答案 4 :(得分:0)
有很多答案,但他们并不专注于要点<&34; 以编程方式检查在运行时&#34;。 我的建议是:
以编程方式查找下载的文件程序的路径(如... / Documents / YourApp.bundle / fr-FR.lproj / Localizable.strings)
NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable" ofType:@"strings" inDirectory:nil forLocalization:@"ja"];
将其转换为字符串数组
NSString *fileContents = [NSString stringWithContentsOfFile:localizablePath encoding:NSUTF8StringEncoding error:nil];
NSArray *allLinedStrings = [fileContents componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];
手动或使用正则表达式检查allLinedStrings中的所有行。以下代码是使用一些简单规则手动检查的示例:
for (NSString *line in allLinedStrings) {
if (line.length >= 2) {
NSString *firstTwoCharacters = [line substringToIndex:2];
if (![firstTwoCharacters isEqualToString:@"//"]){
if (![line containsString:@"\";"]) {
NSLog(@"Invalid line");
}
NSUInteger numberOfOccurrences = [[line componentsSeparatedByString:@"\""] count];
if (numberOfOccurrences < 4) {
NSLog(@"Invalid line");
}
}
}
else if (line.length > 0) {
NSLog(@"Invalid line");
}
}
答案 5 :(得分:-2)
使用以下比plutil更好的工具。它还有其他功能可以检测丢失的本地化字符串。
https://github.com/Rocker007/XCLocale
并使用以下选项来验证您的本地化字符串文件
./XCLocale.py -cf <iOS project path>
此工具将告诉您synatax错误的确切行号,并且还会检测到一些最新Xcode版本未检测到的语法错误
例如
#"screen.title" = " screen 1";