iphone - reading from Localizable.strings file as a key-value in a dictionary

后端 未结 4 1205
别跟我提以往
别跟我提以往 2020-12-08 16:56

I want to read the text from the localizable.strings file. I am collecting the strings for translation from several directories and file in one .strings file. But then I hav

相关标签:
4条回答
  • 2020-12-08 17:12

    What I was looking for is a way to read a json from a bundle file into a NSDictionary and then print it inside a UITextView. The result was not nicely formatted!

    I used a part of Karim's answer from above to create a method that generated a beautified json in a string:

    -(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);
    }
    
    0 讨论(0)
  • 2020-12-08 17:13

    I am happy to find an excellent API in NSString class. The code is below.

    - (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]];
    }
    }
    
    0 讨论(0)
  • 2020-12-08 17:23
      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"];
    
    0 讨论(0)
  • 2020-12-08 17:32

    Solution in Swift

    I created a swift code following the Objective-C code answered by @jason-moore above This solution is swift equivalent.

    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
        }
    
    0 讨论(0)
提交回复
热议问题