2015-08-21 108 views
16

我正在以编程方式创建Localizable,字符串文件。我正在从服务器下载文件并显示该文件的本地化。检查Localizable.strings文件是否有效

但是,如果在文件中有一些错误,那么我的本地化不起作用。它显示了我的钥匙。如果服务器我编辑的本地化文件,我添加字符串作为

“参考hello world” =你好

这里,关键是正确的,但值的格式不正确。格式应为

“HELLO_WORLD”=“你好”;

如何以编程方式在运行时检查Localizable.strings文件是否不包含任何错误并且是有效的。

请从终端帮助

回答

42

使用plutil

plutil -lint Localizable.strings 
+0

十分感谢。 Xcode 9和终端被使用并仍然在帮助调试 –

1

我有同样的问题,并发现多是不是“详细”就够了。编辑文件的人们想要的东西会告诉他们更确切地说出了什么问题

plutil太宽了。

,所以我写了一个快速&肮脏的Java工具来测试一个字符串文件:

https://github.com/Daij-Djan/parseAndValidateAppleStringsFile

免责声明:我的代码

13

除了@ Aderstedt的回答是:

plutil -lint Localizable.strings做工作,但是你必须为每个版本的文件运行它。 E.g

  1. cd到项目根
  2. cd en.lproj - 你可以与你正在使用的任何本地化替换此。
  3. plutil -lint Localizable.strings

当您运行第3步,你要么显示一个错误,告诉你什么是错的文件。或者你会被告知该文件是OK

4

如上plutil(属性列表实用程序)提到的是验证.plist.strings文件,如果你手工编辑一个伟大的工具。您可以将它与find相结合,将它应用到您的所有文件.strings。在项目目录中运行

find . -name *.strings -exec plutil -lint {} \; 

或使用

find . -path ./DerivedData -prune -o -name *.strings -exec plutil -lint {} \; 

如果你想排除您DerivedData目录(因为我通常做)。

0

有很多答案,但他们并没有注重的要点是“编程检查在运行时”。 我的建议是:

  1. 编程方式找到你下载的文件程序的路径(比如... /文件/ YourApp.bundle/FR-FR.lproj/Localizable.strings)

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable" ofType:@"strings" inDirectory:nil forLocalization:@"ja"]; 
    
  2. 它转换为串

    NSString *fileContents = [NSString stringWithContentsOfFile:localizablePath encoding:NSUTF8StringEncoding error:nil]; 
    NSArray *allLinedStrings = [fileContents componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]]; 
    
  3. 检查从手动或使用正则表达式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"); 
        } 
    }