How to Translate Cultureinfo Language Names

How to translate CultureInfo language names

This functionality isn't built into the .NET Framework

Maybe look at Google Translate API

Obtain a language name in different languages

It is possible to retrieve translations for installed framework languages. The workings are undocumented but the internal implementations can be seen in the reference source (e.g. for CultureData).
For target cultures other than installed ones, the English fallback will be returned.

Building on top of that we can use the following (again, for installed framework languages only):

public static string GetDisplayName(this CultureInfo culture, CultureInfo locale)
{
var rm = new ResourceManager("mscorlib", typeof(object).Assembly);
var resourceKey = $"Globalization.ci_{culture.Name}";
return rm.GetString(resourceKey, locale);
}

For example with Swedish and English installed:

var culture = CultureInfo.GetCultureInfo("en");
var swedishName = culture.GetDisplayName(CultureInfo.GetCultureInfo("sv")); // Engelska
var englishName = culture.GetDisplayName(CultureInfo.GetCultureInfo("en")); // English
var germanName = culture.GetDisplayName(CultureInfo.GetCultureInfo("de")); // English <- German not installed

To cover all languages (or an arbitrary collection of languages), I would suggest a non-native approach as the built-in way is not really supporting your use case.

Cheers!

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

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;

Retrieve localized language names by CultureInfo

No, this is not possible. CultureInfo holds the native name for a language it represents (e.g. fr-Ca is Francais Canadienne), but CultureInfo("fr-CA") would not be aware that "German" in French is "Allemagne"

As indicated in this thread, you might be interested in the Google Translate APi:

ref: How to translate CultureInfo language names

Get Culture Display Name in its language

You need to display the NativeName instead of the DisplayName.

How to get translated list of all country names in dotnet

I'd recommend two Nuget packages:

  • ISO3166 by Jorn Schou-Rode
  • IsoNames by cmon

Get country name in selected language:

IsoNames.CountryNames.GetName(new CultureInfo(language), country);

Get list of two letter codes:

ISO3166.Country.List.Select(c => c.TwoLetterCode);

I looked for some MS stuff previously for similar task and ended up with these two packages.

Get language name from locale string using .NET? ex: en_us = english

Do not use the constructor of CultureInfo. It is faster to use the static GetCultureInfo method since this method is cached and returns an immutable (readonly) CultureInfo object.

According to the Facebook SDK documentation concerning localization, it is safe to assume that you can replace the underscore by a dash in order to allow .NET to understand the locale.

Facebook locales follow ISO language
and country codes respectively,
concatenated by an underscore.

The basic format is ''ll_CC'', where
''ll'' is a two-letter language code,
and ''CC'' is a two-letter country
code. For instance, 'en_US' represents
US English.

Depending if you need the name to appear in english regardless of the language of the OS, use

CultureInfo.GetCultureInfo("en-US").EnglishName

If you need the name in the language of the OS, use:

CultureInfo.GetCultureInfo("en-US").DisplayName

set CultureInfo to a language with country unknown

This var en = CultureInfo.GetCultures (CultureTypes.AllCultures).First(x=>x.Name=="en"); will give you a base CultureInfo for your language (en in the example). If you'll examine "en-GB", "en-US" than you can see both have Parent equal to "en".



Related Topics



Leave a reply



Submit