Localization of numbers in iOS

I just have a positive integer [9, 393, 3, 993], and I would like to localize it in a certain language [9, 393, 3, 939].

If I use NSNumberFormatter , it will localize the number according to the user's language. However, I want to override this and choose any language to translate the number.

I tried the following: the job is not :

 // user locale is @"en" NSNumberFormatter* formatter = [NSNumberFormatter new]; [formatter setNumberStyle:NSNumberFormatterNoStyle]; [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"ar"]]; [formatter setMinimumIntegerDigits:padding]; return [formatter stringFromNumber:@(num)]; 

The returned string is in English.


Note that I have a very similar code snippet for NSDateFormatter , but it works as expected. The NSDateFormatter object corresponds to the set language.

+7
source share
1 answer

It seems that I came across a very special case when the application locale just goes crazy.

I am changing the standard application locale using something like this:

 [[NSUserDefaults standardUserDefaults] setObject:@[@"ar"] forKey:@"AppleLanguages"]; 

Then I tried to get the preferred language and create a local object from it using:

 NSString* langPrefix = [NSLocale preferredLanguages][0]; 

Finally, create a new NSLocale object from the returned object. When testing the code, I would change the language from the application, and then close the application through Xcode. I assume that NSUserDefaults will not synchronize, but even if I called the synchronize method, it would still get messed up.

Bottom line: localization testing should be performed by deploying the application, and after the device has been disconnected from Xcode, so the application will work properly at all stages of the life cycle.

+3
source

All Articles