Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get CultureInfo by culture English name

Tags:

c#

.net

Is it possible to get CultureInfo by culture English name?

Imagine we have got: Danish, English, Spanish and etc...

Thank you!

like image 446
Friend Avatar asked Nov 12 '12 11:11

Friend


People also ask

What is CultureInfo CurrentCulture?

The CultureInfo. CurrentCulture property is a per-thread setting; that is, each thread can have its own culture. You get the culture of the current thread by retrieving the value of the CultureInfo. CurrentCulture property, as the following example illustrates. C# Copy.

What does CultureInfo Invariantculture mean?

The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region. You specify the invariant culture by name by using an empty string ("") in the call to a CultureInfo instantiation method. CultureInfo.

What is culture in asp net?

In an ASP.NET Web page, you can set to two culture values, the Culture and UICulture properties. The Culture value determines the results of culture-dependent functions, such as the date, number, and currency formatting, and so on. The UICulture value determines which resources are loaded for the page.

What is default culture in C#?

C# current cultureDefaultThreadCurrentCulture property gets or sets the default culture for threads in the current application domain. using System. Globalization; double val = 1235.56; Console. WriteLine($"Current culture: {CultureInfo.CurrentCulture.Name}"); Console.


3 Answers

var names = CultureInfo.GetCultures(CultureTypes.AllCultures).ToLookup(x => x.EnglishName);
names["English"].FirstOrDefault();
like image 60
m0sa Avatar answered Oct 17 '22 03:10

m0sa


var EnglishCulture = CultureInfo.GetCultures(CultureTypes.AllCultures)
                                .Where(r => r.EnglishName == "English");

Or if you need FirstOrDefault

var EnglishCulture = CultureInfo.GetCultures(CultureTypes.AllCultures)
                                .FirstOrDefault(r=> r.EnglishName == "English");
like image 43
Habib Avatar answered Oct 17 '22 05:10

Habib


There's no builtin method to get a culture by it's english name, so you could write one:

public static CultureInfo getCultureByEnglishName(String englishName)
{
    // create an array of CultureInfo to hold all the cultures found, 
    // these include the users local culture, and all the
    // cultures installed with the .Net Framework
    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
    // get culture by it's english name 
    var culture = cultures.FirstOrDefault(c => 
        c.EnglishName.Equals(englishName, StringComparison.InvariantCultureIgnoreCase));
    return culture;
}

Here's a demo: http://ideone.com/KX4U8l

like image 29
Tim Schmelter Avatar answered Oct 17 '22 03:10

Tim Schmelter