views:

145

answers:

2

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 have several copies of the same translation strings. I want to remove this programmatically. So I need to read the strings only (not the comments) from the .strings file, and - then sort them, - remove repeated strings then create a new .strings file.

Is it possible to read the strings file and keep the key string and translated value in a dictionary. I mean any built-in method to read a .text file, only the "key " = "value" part, avoiding /* ... */ or # comments part. Like reading a config file.

A: 

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]];
}
}
karim
I found that the .strings file is compiled to become a "binary property list". This can be loaded with: [NSDictionary dictionaryWithContentsOfFile:path];
Jason Moore
A: 
  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"];
Jason Moore