I need to give the facility to user that user can use different currency symbol instead of device local currency symbol. For example: If my device language is 'English', then my currency symbol will be '$' but I need to do that my device language will remain 'English' but currency symbol changed to '€'(or any other currency symbol but not '$').
Please do not mark it as duplicate because I need to use different currency symbol instead of device default local currency symbol.
Please give your suggestion regarding this. Thanks for your time.
I use this:
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setCurrencyCode:currencyCode];
where currency code is something like "USD", "CAD", "EUR", "GBP", etc.
Related
I have my app translated to different languages (https://pub.dev/packages/flutter_translate) and I also provide the option to select a specific currency (https://pub.dev/packages/country_currency_pickers):
first 2 options
When I select the currency I get this:
Now that everything is selected I need to update my currency and for that I can do:
NumberFormat.simpleCurrency(
locale: "en_GB",
).format(money);
Notice that I have en_GB which is what I'd need here to display pounds. For this example I get es from the language and GB from the currency but es_GB gets euros instead of pounds, which is what I'd need.
How can I get the country code from any of the previous parameters?
Can I pass something else to get the symbol like currencyCode for example?
You should use provide name in the constructor. https://api.flutter.dev/flutter/intl/NumberFormat/NumberFormat.simpleCurrency.html
If name is specified, the currency with that ISO 4217 name will be used. Otherwise we will use the default currency name for the current locale.
NumberFormat.simpleCurrency(
name: "GBP", //currencyCode
).format(money)
i'm be able to retrieve the region format of the user, ad example it_IT with this code
[NSLocale currentLocale] objectForKey:NSLocaleIdentifier];
is it possible have the complete list of these identifier?
(it_IT, de_DE, de_AT,....)
Thanks
[NSLocale availableLocaleIdentifiers] is yet another way.
For testing, specifically to insure all date formats are handled properly in my UI, I rip through that array, set a NSDateFormatter to use that and test for every possible combination of date formatting in an automated way.
Interesting what you learn along the way as well when you see some date representations that break your assumptions about how some dates, day names, etc might be formatted.
Try with this:
Returns an array of NSString objects that represents all known legal ISO currency codes.
[NSLocale ISOCurrencyCodes];
Returns an array of NSString objects that represents all known legal country codes.
[NSLocal ISOCountryCodes];
Returns an array of NSString objects that represents all known legal ISO language codes.
[NSLocale ISOLanguageCodes];
All the info from:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSLocale_Class/Reference/Reference.html
i am getting product price in multiple currency. here is a sample data
CurrencyName CurrencySymbol Pirce
"USD", "$" 1234.5
"EUR", "€" 12340.5
"GBP", "£" 123.4
"CHF", "CHF" 12345.0
so i want to format available currency based on type not based iPad/iPhone local format.
for example
$1234.50
1.234,50€
and also i want to display all at a same time.
thanks.
That means you want to show it in different localizations. Like the Euros in French or German (with decimal comma) and the CHF in Swiss (with decimal point and probably and apostrophe for separating the thousands).
I would map the currency to a specific localization and the feed it to the nsnumberformatter.
I want to know and print the locale names which I am changing on my device. Is there any way to print them. Printing a NSLocale object does not help.
Create an NSLocale for the language in which you want the words to appear, then use -displayNameForKey:value: on the NSLocale object to get the display name for the NSLocaleIdentifier key.
The documentation for this method has an example that shows you exactly what you're looking for.
I am localizing my app for Denmark. I have the Localizable.Strings and the XIBs translated into Danish.
On iPhone a user can
Set the 'Region Format' to 'Denmark'
but keep the language 'English'
Set the 'Region Format' to any English-speaking country but keep
the language 'Dansk'
Set the 'Region Format' to 'Denmark' and language 'Dansk'
My question is: for any of the above settings should the app show the 'Danish' version (which for my App will mean all text in UI and the database to be in Danish.) or should the 'Danish' version only come when user sets the language to 'Dansk'?
This is a great question.
The real issue it comes down to is, in Apple's opinion, as they did it, does NSLocalizedString tend to favour the 'Region Format' or the 'language' setting?
I have never been able to find a clear answer on this, I just let NSLocalizedString decide.
(Purely FWIW, I think follow the "language" setting.)
It's possible the following code snippets could help you.
// to ("usually") get the preferred language FROM THE SET WHICH we supplied in bundle
// [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0]
// to ("often") get the preferred language REGARDLESS OF what we supplied
// [ [NSBundle preferredLocalizationsFromArray:[NSLocale ISOCountryCodes]] objectAtIndex:0]
// to ("fairly reliably") get the user's chosen language setting...
// [ [NSLocale preferredLanguages] objectAtIndex:0]
It may not help, but I hope it helps.
Languages and locales are treated differently by Cocoa, because they are independent concepts. NSLocalizedString() will obey the current language setting of the OS, where things like dates, times, and numbers are affected by the locale setting.
Why locales are handled separately from languages can be found in the Locales Programming Guide:
When you display data to a user it
should be formatted according to the
conventions of the user’s native
country, region, or culture.
Conversely, when users enter data,
they may do so according to their own
customs or preferences. Locale objects
are used to provide information
required to localize the presentation
or interpretation of data. This
information can include decimal
separators, date formats, and units of
measurement, as well as language and
region information.
For example, by convention in the
United States “7/4/76” represents the
Bicentennial of the Declaration of
Independence. However, in Great
Britain, it represents the “7th of
April, 1976”; in Thailand using the
Thai Traditional Calendar it might
represent “April 7th, 2519”; and in
France it represents “7 avril 1976”.
To take a more subtle example, in the
United States“12.125” represents the
decimal number twelve and one eighth,
whereas in Germany it represents
twelve thousand one hundred and
twenty-five.
In the example given there, you might have a user who prefers to use English as their primary language, yet who lives in Germany and is used to a comma as a decimal separator, not a period.
The Internationalization Programming Topics guide has a lengthy discussion of all the issues involved and how to deal with them.