Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  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"];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...