代码之家  ›  专栏  ›  技术社区  ›  karim

iphone-将Localizable.strings文件作为字典中的键值读取

  •  13
  • karim  · 技术社区  · 14 年前

    所以我只需要从.strings文件中读取字符串(而不是注释),然后 -然后分类, -删除重复的字符串

    是否可以读取字符串文件并将键字符串和翻译后的值保存在字典中。我指的是任何读取.text文件的内置方法,只有“key”=“value”部分,避免/*…*/或#评论部分。就像读取配置文件一样。

    4 回复  |  直到 14 年前
        1
  •  62
  •   Jason Moore    14 年前
      NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable"
                                                       ofType:@"strings"                                                       
                                                  inDirectory:nil
                                              forLocalization:@"ja"];
    
      // compiled .strings file becomes a "binary property list"
      NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    
      NSString *jaTranslation = [dict objectForKey:@"hello"];
    
        2
  •  8
  •   karim    14 年前

    我很高兴在NSString类中找到一个优秀的API。代码如下。

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application 
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Localizable" ofType:@"strings"];
    NSString *fileText = [NSString stringWithContentsOfFile:filePath encoding: NSUnicodeStringEncoding error:nil];
    NSDictionary *stringDictionary = [fileText propertyListFromStringsFileFormat];
    
    NSArray *allKeys = [stringDictionary allKeys];
    
    NSArray *sortedKeys = [allKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    
    NSString *documentsDirectory;   
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);  
    if ([paths count] > 0)  {       
        documentsDirectory = [paths objectAtIndex:0];       
    }
    
    NSString *outputPath = [documentsDirectory stringByAppendingString:@"/Localizable.strings"];
    NSLog(@"Strings contents are writing to: %@",outputPath);
    [[NSFileManager defaultManager] createFileAtPath:outputPath contents:nil attributes:nil];
    NSFileHandle *outputHandle = [NSFileHandle fileHandleForWritingAtPath:outputPath];
    [outputHandle seekToEndOfFile];
    
    for(id key in sortedKeys){
        NSString *line = [NSString stringWithFormat:@"\"%@\" = \"%@\";\n", key, [stringDictionary objectForKey:key]];
        NSLog(@"%@",line);
        [outputHandle writeData:[line dataUsingEncoding:NSUnicodeStringEncoding]];
    }
    }
    
        3
  •  2
  •   Akarsh SEGGEMU    5 年前

    Swift解决方案

    above 这个解是等价的。

    guard let path = Bundle.main.url(forResource: "Localizable", withExtension: "strings", subdirectory: nil, localization: "ja") else {
                return nil
            }
    guard let dict = NSDictionary(contentsOf: path) else {
            return nil
        }
    guard let jaTranslation = dict.value(forKey: "hello") as? String else {
            return nil
        }
    
        4
  •  0
  •   Gal    8 年前

    我要寻找的是一种将json从捆绑文件读入 NSDictionary 然后打印在 UITextView 结果格式不好!

    我使用上面Karim的部分答案创建了一个方法,该方法在字符串中生成了一个漂亮的json:

    -(void)setText:(NSDictionary *)json
    {
        NSArray *allKeys = [json allKeys];
    
        _beautyStr = @"";
        for(id key in allKeys){
           NSString *line = [NSString stringWithFormat:@"\"%@\" = \"%@\";\n", key, [text objectForKey:key]];
        _beautyStr = [NSString stringWithFormat:@"%@%@",_beautyStr, line];
        }
    
        NSLog(@"%@",_beautyStr);
    }