2010-06-30 4 views
13

मैं स्थानीयizable.strings फ़ाइल से पाठ को पढ़ना चाहता हूं। मैं एक .strings फ़ाइल में कई निर्देशिकाओं और फ़ाइल से अनुवाद के लिए स्ट्रिंग एकत्र कर रहा हूं। लेकिन फिर मेरे पास एक ही अनुवाद स्ट्रिंग की कई प्रतियां हैं। मैं इस प्रोग्रामेटिक रूप से हटाना चाहता हूं। तो मुझे .strings फ़ाइल से केवल स्ट्रिंग्स (टिप्पणियां नहीं) पढ़ने की आवश्यकता है, और - फिर उन्हें सॉर्ट करें, - बार-बार तार हटाएं, फिर एक नई .strings फ़ाइल बनाएं।आईफोन - स्थानीयकरण योग्य.स्ट्रिंग्स फ़ाइल से एक शब्दकोश में कुंजी-मान के रूप में

क्या स्ट्रिंग्स फ़ाइल को पढ़ना संभव है और कुंजी स्ट्रिंग और अनुवादित मान को एक शब्दकोश में रखना संभव है। मेरा मतलब है .text फ़ाइल को पढ़ने के लिए कोई भी अंतर्निहित विधि, केवल "कुंजी" = "मान" भाग,/* ... */या # टिप्पणियां भाग से परहेज करें। एक विन्यास फाइल पढ़ने की तरह।

+0

mxg - आप शायद इस सवाल का जवाब मतलब है। – Jonny

उत्तर

7

मुझे एनएसएसटींग कक्षा में एक उत्कृष्ट एपीआई खोजने में खुशी है। कोड नीचे है।

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

मैंने पाया कि .strings फ़ाइल को "बाइनरी प्रॉपर्टी लिस्ट" बनने के लिए संकलित किया गया है। इसे लोड किया जा सकता है: [NSDictionary dictionaryWithContentsOfFile: पथ]; –

52
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

बस शानदार! – mxg

+1

असल में, यह नियमित रूप से (utf-8 और utf-16) .strings फ़ाइलों के साथ भी काम करता है। –

0

क्या मैं खोज रहा था एक NSDictionary में एक बंडल फ़ाइल से एक json पढ़ सकते हैं और फिर एक 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); 
} 
संबंधित मुद्दे