Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iphone-将Localizable.strings文件作为字典中的键值读取_Iphone_File_Text_Localizable.strings - Fatal编程技术网

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

iphone-将Localizable.strings文件作为字典中的键值读取,iphone,file,text,localizable.strings,Iphone,File,Text,Localizable.strings,我想从localizable.strings文件中读取文本。我正在从多个目录和一个.strings文件中收集用于翻译的字符串。但是我有几个相同翻译字符串的副本。我想以编程方式删除此项。 所以我只需要从.strings文件中读取字符串(而不是注释),然后 -然后把它们分类, -删除重复的字符串 然后创建一个新的.strings文件 是否可以读取字符串文件并将键字符串和翻译后的值保存在字典中。我指的是读取.text文件的任何内置方法,只有“key”=“value”部分,避免使用/*…*/或#评论部分

我想从localizable.strings文件中读取文本。我正在从多个目录和一个.strings文件中收集用于翻译的字符串。但是我有几个相同翻译字符串的副本。我想以编程方式删除此项。 所以我只需要从.strings文件中读取字符串(而不是注释),然后 -然后把它们分类, -删除重复的字符串 然后创建一个新的.strings文件


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

我很高兴在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]];
}
}

我所寻找的是一种将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);
}

Swift中的解决方案

我根据@jason moore回答的Objective-C代码创建了一个swift代码 这个解决方案是swift等价的

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
    }

我发现.strings文件被编译成“二进制属性列表”。这可以通过以下方式加载:[NSDictionary Dictionary WithContentsOfFile:path];实际上,它也适用于常规(utf-8和utf-16).strings文件.mxg-您可能指的是答案。
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
    }