I decided this in the project a few weeks ago, creating my own simple template system using NSScanner . The method uses a template system that finds variables with the syntax ${name} . Variables are passed to the method via NSDictionary .
- (NSString *)localizedStringFromTemplateString:(NSString *)string variables:(NSDictionary *)variables { NSMutableString *result = [NSMutableString string]; // Create scanner with the localized string NSScanner *scanner = [[NSScanner alloc] initWithString:NSLocalizedString(string, nil)]; [scanner setCharactersToBeSkipped:nil]; NSString *output; while (![scanner isAtEnd]) { output = NULL; // Find ${variable} templates if ([scanner scanUpToString:@"${" intoString:&output]) { [result appendString:output]; // Skip syntax [scanner scanString:@"${" intoString:NULL]; output = NULL; if ([scanner scanUpToString:@"}" intoString:&output]) { id variable = nil; // Check for the variable if ((variable = [variables objectForKey:output])) { if ([variable isKindOfClass:[NSString class]]) { // NSString, append [result appendString:variable]; } else if ([variable respondsToSelector:@selector(description)]) { // Not a NSString, but can handle description, append [result appendString:[variable description]]; } } else { // Not found, localize the template key and append [result appendString:NSLocalizedString(output, nil)]; } // Skip syntax [scanner scanString:@"}" intoString:NULL]; } } } [scanner release]; return result; }
With a localized file that looks like this:
"born message" = "I was born in ${birthYear} on a ${birthWeekDay}. ${byebye}"; "byebye" = "Cheers!";
We can perform the following results ...
NSDictionary *variables = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1986], @"birthYear", @"monday", @"birthWeekDay", nil]; NSString *finalString [self localizedStringFromTemplateString:@"born message" variables:variables]; NSLog(@"%@", finalString);
As you can see, I have added some additional features. Firstly, any variables that are not found ( ${byebye} in my example) will be localized and added to the results. I did this because I load the HTML files from my application package and run them through the localize method (in this case, I do not localize the input line when creating the scanner). In addition, I added the ability to send things other than NSString objects for some extra flexibility.
This code may not be the most efficient or beautifully written, but it does the job without any noticeable impact :)
alleus
source share