Reputation: 127
i want localize stringWithFormat by this:
NSString *string = [NSString stringWithFormat:@"Login %d in",2013];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@string, nil)
message:@"Message"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"sure", nil];
[alertView show];
also i write this in Localizable.string:
"Login %d in" = "LOGIN %d IN";
and it doesn't work.can you help me?Thank...
Upvotes: 1
Views: 207
Reputation:
You have to localize the format string itself. Doing anything else doesn't make sense because the string can be practically anything when it's formatted (that's the purpose of format strings, after all). Just out of curiosity, haven't you seen things like
"%d seconds remaining" = "%d secondes restants";
"Hello, %@!" = "Bonjour, %@ !";
in the Localizable.strings
file of applications you used yet?
NSString *localizedFmt = NSLocalizedString(@"Login %d in", nil);
UIAlertView *av = [[UIAlertView alloc]
initWithTitle:[NSString stringWithFormat:localizedFmt, 2013],
// etc...
];
The Security Freak's Notice: although this is the most common and easiest approach to localize formatted strings, it's not entirely safe.
An attacker can change the localized format string in the aforementioned Localizable.strings
file of your app to something bogus, for example, to a string that contains more conversion specifiers than stringWithFormat:
has arguments (or even mismatching specifiers - treating integers as pointers, anyone?), and then a stack smashing-based attack can be carried out against your application - so beware of hackers.
Upvotes: 3