[NSString propertyListFromStringsFileFormat]
Returns a dictionary object initialized with the keys and values found in the receiver.
- (NSDictionary *)propertyListFromStringsFileFormat
Return Value
A dictionary object initialized with the keys and values found in the receiver
Discussion of [NSString propertyListFromStringsFileFormat]
The receiver must contain text in the format used for
.strings
files. In this format, keys and values are separated by an equal sign, and each key-value pair is terminated with a semicolon. The value is optional—if not present, the equal sign is also omitted. The keys and values themselves are always strings enclosed in straight quotation marks. Comments may be included, delimited by /*
and */
as for ANSI C comments. Here’s a short example of a strings file:[NSString propertyListFromStringsFileFormat]/* Question in confirmation panel for quitting. */
|
"Confirm Quit" = "Are you sure you want to quit?";
|
|
/* Message when user tries to close unsaved document */
|
"Close or Save" = "Save changes before closing?";
|
|
/* Word for Cancel */
|
"Cancel";
|
Example of [NSString propertyListFromStringsFileFormat]
- (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]];
}
}