Is there a way to get a flag image from a C# CultureInfo?

不羁的心 提交于 2019-12-08 10:02:58

问题


If I have a CultureInfo instance, is there any simple way — built-in, through a framework or via the OS — to obtain a matching "flag" icon matching that culture? This would avoid having to create a separate lookup.

I couldn't find a member function to do the job in the documentation for CultureInfo, but perhaps there is some other not-too-taxing approach that involves other .NET classes that I haven't found.


回答1:


No. There is no simple, built-in way, through the .NET framework or the Windows operating system, to obtain a matching "flag" icon for a given CultureInfo instance.




回答2:


As @Peter Duniho said, there is no built-in way in the .NET Framework. Furthermore, CultureInfo represent locales, while flags represent countries, and there are many situations where the two do not perfectly match. You may incur in diplomatic problems if you come across some users.

Anyway, if you want something simple, you may want to use famfamfam's flag icons, which have been conveniently named after the ISO 3166-alpha1 2 letter country code, and then convert your CultureInfo to a RegionInfo given the locale, and finally get the two letter ISO region name:

var r = new RegionInfo(yourCultureInfo.LCID);
var flagName = r.TwoLetterISORegionName + ".gif";

finally, if such a file exists, then display it. Please note that you may use the PNG versions, and that different languages may map to the same country (I'm thinking of Switzerland, where they speak 4 official languages).




回答3:


Extending Alex's answer. Once you have the ISO code, you can use this simple library to produce an ImageSource of the flag (from the famfamfam set) from the two character string:

https://github.com/drewnoakes/famfamfam-flags-wpf

It's not built in, but it is easy to use.

Images end up looking like this:



来源:https://stackoverflow.com/questions/27733705/is-there-a-way-to-get-a-flag-image-from-a-c-sharp-cultureinfo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!