开发者

get cultureinfo with country code

I try to get CultureInfo with country code. I'm using hostip.info for getting country code.

For example, "http://api.hostip.info/?ip=207.46.197.32&position=true" returns me "US" for country code or http://api.hostip.info/?ip=82.1开发者_StackOverflow中文版51.131.196&position=true returns "TR".

If I could have "en-US", "tr-TR", it's easy, but I can't have these culture names. I have country codes.

So is there any way to get CultureInfo with country codes or do you have any suggestion for getting CultureInfo with IP Address?


If the request is made by a browser, the "Accept-Language" field in the HTTP request header will list desired culture(s). Granted, it's not from an IP, but it may be a solution if you have HTTP request headers.

E.g.,

Accept-Language: en-us,en


I am not sure you can do this in practice.

If you have Switzerland, Canada or India as country code which language would you want?


The official public standards is most better source to use. See "CLDR - Unicode Common Locale Data Repository" at http://cldr.unicode.org/.

The datafiles are available for download. Interesting documentation and recommendations are available.


I faced the same problem, because I got the country code from a third-party IP localization library. So, here is my solution.

First I try to get the culture info by the country code. If this fails I retrieved all cultrure Info and then I search for the one that contains the country name (also possible the way is the country code (US) is contained in the CultureInfo.Name field (en-US))

ci = CultureInfo.GetCultures(CultureTypes.AllCultures).Where(c => c.EnglishName.Contains(loc.countryName)).FirstOrDefault();

If the country have more than one culture then I followed the approach suggested by Doug Domeny

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜