Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To get Language by Country in c#

What's the best way to solve it?

I have tried :

using System;
using System.Globalization;
using System.Linq;
using System.Collections.Generic;

class HelloWorld {
    static void Main() {
        List<CultureInfo> CultureInfos = CultureInfo
                            .GetCultures(CultureTypes.SpecificCultures)
                            .ToList();

        var a = CultureInfos.Where(x => x.DisplayName.Contains("Israel"))
                            .FirstOrDefault();

        Console.WriteLine(a?.DisplayName.Split(' ')[0]);
    }
}

Funny thing, It works on onlinegdb.com but doesn't work on my local machine .Net core 3.1. It returns Null in "a" Tried to find any clue to their .Net version - no success

Do you know why? / What I'm doing wrong?

Thanks

like image 599
Leon Nicetomeetya Avatar asked Jan 28 '26 20:01

Leon Nicetomeetya


1 Answers

Ok it seems like RegionInfo contains the plain country name. Using EnglishNameso you have a baseline. If you use DisplayName it depends on the CurrentCulture if your code works.

For example on a german system "Austria" would'nt work, but "Österreich"

var cultureInfo = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                             .FirstOrDefault(c => new RegionInfo(c.Name).EnglishName == "Israel"));

See working example here.

like image 160
CSharpie Avatar answered Jan 31 '26 11:01

CSharpie