I have a list of alpha-2 country codes.
List<String> countryCodes = ["DE", "CH", "AT"];
How to get the localized country names in Flutter/Dart?
If the device language is set to english, i want this output..
Germany, Switzerland, Austria
If the device language is set to german, i want this output..
Deutschland, Schweiz, Österreich
Is this possible without any 3rd party package in Flutter/Dart?
In Java you could do..
Locale loc = new Locale("","DE");
loc.getDisplayCountry();
To get the localized country names from a country code one can use the Country Codes package from pub.dev.
Its easy to use. I recommend.
https://pub.dev/packages/country_codes
Related
I am using NumberFormat.compactCurrency() with HI locale as I want to compact the number in the Indian currency way - into lakhs, crores..etc.
But, if I use HI as the locale in it, it seems to add the lakh/crore string in Hindi. I want to compact the currency in the Indian way but show the lakh/crore in English. Do I need to use a different locale string for that? any other ideas?
The most simple way to get closest to your requirements would be to use en_IN:
NumberFormat.compactCurrency(locale: 'en_IN').format(price)
Output :
I have a question about currencies. How can I get device currency based on region, location or something like this? There are cases when (for example) device language (and locale) set to English, but currency is Euro or Peso. Currently I'm using NumberFormat which is based on localeCode, but it generates errors for some regions.
For example
country: Ukraine,
language: Russian,
currency: Russian ruble <- wrong. Should be Ukrainian hryvna.
I don't know how, but Google Play deals with this problem :) So I think it should be also possible for other apps.
Have you tried the following, it should work with Ukrainian hryvna:
import 'dart:io';
import 'package:intl/intl.dart';
String currency = NumberFormat.simpleCurrency(locale: Platform.localeName).currencySymbol;
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)
This is not a question but rather a note for other developers who might be using CTCarrier.
I have been developing an app which requires reading the iso of the SIM card to determine the country the app is being used in (at least the SIM country). The doc mentions the following about the isoCountryCode property:
This property uses the ISO 3166-1 country code representation.
From my tests and deployment however it appears that the returned iso is not the standard ISO 3166-1 for several countries (not sure if this is Apple's mistake of the carrier who stored the info in the SIM)! So i had to do a translational phase to resolve the incorrect ISO's. The following is the list i have got to so far (will keep updating it once i get new incorrect iso's):
Country: ISO I get Expected ISO
---------- ---------- -------------
UK uk gb
Japan ja jp
Cheers
AF
UK was reserved by the the United Kingdom (and is an an appendix to the standard) so that's a grey area ;)
JA should be Jamacia - why do you expect this to be Japan?
Info from here : http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
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.