Get language name from locale string using .NET? ex: en_us => english
Asked Answered
T

4

12

How can i find the language for a given locale?

Example: input: en_US output: English

Using the .NET libraries? I tried the CultureInfo class, but i can't find something usefull.

Thanks!

Tedda answered 20/4, 2011 at 12:38 Comment(1)
Dupe? #2433313Lorrianelorrie
T
19

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
Tonetic answered 20/4, 2011 at 12:42 Comment(1)
Thanks. But notice that like @ChrisSnowden said, this works only with en-US. Not with en_US.Tedda
G
10

You need to use en-US not en_US with code like:

System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
string name = culture.DisplayName;

output: English (United States)

Guitar answered 20/4, 2011 at 12:43 Comment(2)
Should i use the replace method? "_" => "-" Because I get the locale string from facebook..Tedda
Yes, that would be the best approach to format the locale string into the correct format.Guitar
C
2
System.Globalization.CultureInfo.GetCultureInfo("en-US").EnglishName;
Casteel answered 20/4, 2011 at 12:46 Comment(0)
T
0

You can use following code

Dim culture1 As CultureInfo = New CultureInfo("en-US")
Dim t As Thread = Thread.CurrentThread
Dim currentCulture As CultureInfo = t.CurrentCulture
Dim currentUICulture As CultureInfo = t.CurrentUICulture

'*** display cultures in console
Console.WriteLine("Current Culture: " & currentCulture.Name)
Console.WriteLine("Current UI Culture: " & currentUICulture.Name)
Thomasina answered 20/4, 2011 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.