Δευτέρα, 13 Αυγούστου 2012

Localizing Resources

copy paste from here


Don't localize numbers this way

Postby eaganj » Fri Jun 22, 2012 5:26 am
I just finished chapter 17 of the wonderful book, but this chapter feels weak, thin, and out of place. While the goal is certainly not to provide a complete overview of localization, it does provide one blaring example of what not to do when localizing numbers. In particular, the code that recommends showing the currency as :
CODE: SELECT ALL
NSString *currencySymbol = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol];
[[cell valueLabel] setText:[NSString stringWithFormat:@"%@%d", currencySymbol, [p valueInDollars]]];


will run and might appear correct, but is not. Not all regions display currencies in the same way. For example, while one might write $100 in the US, we write 100 (euro) in France. (Note: I had to write (euro) instead of the symbol in this message because of a text encoding bug.) Furthermore, some euro-zone regions write that as (euro) 100 (note the space). While completely understandable, writing (euro)100 just looks wrong. Fortunately, the NSNumberFormatter (which is just like the NSDateFormatter but for numbers) handles all these subtleties for us. So the above code should have been written as:
CODE: SELECT ALL
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setMaximumFractionDigits:0];
[[cell valueLabel] setText:[formatter stringFromNumber:[NSNumber numberWithInt:[p valueInDollars]]];



Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου