简体   繁体   English

"如何使用cultureInfo 获取国家名称并进行翻译?"

[英]How can I get the name of a country with cultureInfo and translate it?

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)?如何获取保存在手机或平板电脑(Android 和 iOS 设备)设备设置中的国家名称? Is it possible to do that with cultureInfo?可以用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".例如,如果我有这个cultureInfo =“nl-BE”。

The result should look like this:结果应如下所示:

string English_name = "Belgium";
string French_name = "Belgique";
string German_name = "Belgien";

This is not perfect. 这不是完美的。 It gets the full name of the country/region in the language of the localized version of .NET Framework. 它使用.NET Framework本地化版本的语言获取国家/地区的全名。 It will only work, if the localized version of .NET Framework corresponds to the desired language. 仅当.NET Framework的本地化版本与所需的语言相对应时,它才有效。

Apparently, the .NET Framework only stores the country names in one language. 显然,.NET Framework仅以一种语言存储国家/地区名称。

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. 您可以使用新的ValueTuple (从C#7.0开始)来创建由国家代码和语言代码组成的复合字典键。

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 您可以使用以下方法获取当前的UI语言

string language = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;

You can use a generic method to get country name by alpha2 or alpha3.您可以使用通用方法通过 alpha2 或 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM