iPhone - القراءة من ملف acverizable.strings كقوة مفتاح في القاموس

StackOverflow https://stackoverflow.com/questions/3153600

سؤال

أرغب في قراءة النص من ملف torleizable.strings. أقوم بجمع سلاسل الترجمة من عدة أدلة وملف في ملف .strings واحد. ولكن بعد ذلك لدي عدة نسخ من سلاسل الترجمة نفسها. أريد إزالة هذا برمجيا. لذلك أحتاج إلى قراءة الأوتار فقط (وليس التعليقات) من ملف .strings ، ثم - ثم فرزها ، - إزالة السلاسل المتكررة ثم قم بإنشاء ملف .strings جديد.

هل من الممكن قراءة ملف السلاسل والحفاظ على سلسلة المفاتيح والقيمة المترجمة في القاموس. أعني أي طريقة مدمجة لقراءة ملف .text ، فقط جزء "المفتاح" = "القيمة" ، تجنب / * ... * / أو # التعليقات. مثل قراءة ملف التكوين.

هل كانت مفيدة؟

المحلول 2

يسعدني أن أجد واجهة برمجة تطبيقات ممتازة في فصل NSString. الكود أدناه.

- (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]];
}
}

نصائح أخرى

  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"];

الحل في سريع

لقد قمت بإنشاء رمز سريع يتبع رمز الهدف-C الذي تم إجراؤه بواسطة @Jason-Moore في الاعلىهذا الحل هو مكافئ سريع.

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
    }

ما كنت أبحث عنه هو وسيلة لقراءة JSON من ملف حزمة إلى NSDictionary ثم طباعته داخل أ UITextView. لم يتم تنسيق النتيجة بشكل جيد!

لقد استخدمت جزءًا من إجابة كريم من الأعلى لإنشاء طريقة ولدت 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);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top