Reputation: 333
How can I get the name of the country which is saved in the device settings of a mobile phone or tablet(Android and iOS devices)? Is it possible to do that with cultureInfo?
I want to display the name of the country in more than one language. I need to display the name of the country in English, French and German. Is it possible to automatically display a translated version of the name of the country in English, French and German or is it necessary that I translate all the country names myself?
For example if I have this cultureInfo = "nl-BE".
The result should look like this:
string English_name = "Belgium";
string French_name = "Belgique";
string German_name = "Belgien";
How can I do that?
Upvotes: 1
Views: 2262
Reputation: 316
You can use a generic method to get country name by alpha2 or alpha3.
Eg:
public static string GetCountryName(string alpha2code = null, string alpha3code = null, bool nativeName = true)
{
RegionInfo result = null;
if (!String.IsNullOrEmpty(alpha2code))
result = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
?.Where(item => (new RegionInfo(item.LCID))
?.TwoLetterISORegionName.Equals(alpha2code, StringComparison.Ordinal) == true)
?.Select(item => new RegionInfo(item.LCID))
?.FirstOrDefault();
if (!String.IsNullOrEmpty(alpha3code))
result = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
?.Where(item => (new RegionInfo(item.LCID))?.ThreeLetterISORegionName.Equals(alpha3code, StringComparison.Ordinal) == true)
?.Select(item => new RegionInfo(item.LCID))
?.FirstOrDefault();
if (result == null) return String.Empty;
return nativeName ? result.NativeName : result.EnglishName;
}
Upvotes: 1
Reputation: 112682
This is not perfect. It gets the full name of the country/region in the language of the localized version of .NET Framework. It will only work, if the localized version of .NET Framework corresponds to the desired language.
Apparently, the .NET Framework only stores the country names in one language.
var culture = CultureInfo.CurrentUICulture; // Other get any desired culture info.
var regionInfo = new RegionInfo(culture.Name);
string countryName = regionInfo.DisplayName;
You must implement this translation functionality yourself. You could use the new ValueTuple
(since C# 7.0) to create a compound dictionary key consisting of a country code and a language code.
public static readonly Dictionary<(string language, string country), string> _countryNames =
new Dictionary<(string language, string country), string> {
[("en","BE")] = "Belgium",
[("fr", "BE")] = "Belgique",
[("de", "BE")] = "Belgien",
[("en", "CH")] = "Switzerland",
[("fr", "CH")] = "Suisse",
[("de", "CH")] = "Schweiz",
// ...
};
Example: get name of Belgium in English.
string name = _countryNames[("en", "BE")];
or, when you are not sure whether the entry exists:
if (_countryNames.TryGetValue(("en", "BE"), out string name)) {
//TODO: use name
}
You can get the current UI language with
string language = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
Upvotes: 0