简体   繁体   English

有没有办法从 C# CultureInfo 获取旗帜图像?

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

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?如果我有一个CultureInfo实例,是否有任何简单的方法——内置的、通过框架或通过操作系统——来获得匹配该文化的匹配“标志”图标? 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.我在CultureInfo的文档中找不到成员 function 来完成这项工作,但也许还有其他一些不太费力的方法涉及我尚未找到的其他 .NET 类。

不会。没有简单的内置方法通过.NET框架或Windows操作系统来获取给定CultureInfo实例的匹配“标志”图标。

As @Peter Duniho said, there is no built-in way in the .NET Framework. 正如@Peter Duniho所说,.NET Framework中没有内置方法。 Furthermore, CultureInfo represent locales, while flags represent countries, and there are many situations where the two do not perfectly match. 此外,CultureInfo代表语言环境,而标志代表国家,在许多情况下两者并不完全匹配。 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: 无论如何,如果您想简单一些,则可能要使用famfamfam的标志图标 (已按照ISO 3166-alpha1 2字母国家/地区代码方便地命名),然后将您的CultureInfo转换为具有区域设置的RegionInfo,最后得到两个字母ISO区域名称:

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). 请注意,您可能会使用PNG版本,并且不同的语言可能会映射到同一国家/地区(我想到的是瑞士,他们会说4种官方语言)。

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: 获得ISO代码后,您可以使用此简单的库从两个字符串生成标志的ImageSource (来自famfamfam集):

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

It's not built in, but it is easy to use. 它不是内置的,但易于使用。

Images end up looking like this: 图片最终看起来像这样:

Guess what!你猜怎么了! It's now possible to do this in C# and is supported by most platforms including Windows 11 devices!现在可以在 C# 中执行此操作,并且得到包括 Windows 11 设备在内的大多数平台的支持! You can just use the emojis and you don't need a flag icon pack!您可以只使用表情符号,不需要旗帜图标包!

On Windows 10 it only works on Firefox browsers for now.在 Windows 10 上,它目前仅适用于 Firefox 浏览器。 But it does work with iOS, MacOS & Android apps但它确实适用于 iOS、MacOS 和 Android 应用程序

public string GetFlag(string country)
{
  var regions = CultureInfo.GetCultures (CultureTypes. SpecificCultures).Select(x => new RegionInfo(x.LCID));
  var englishRegion = regions.FirstOrDefault(region => region.EnglishName.Contains(country));
  if (englishRegion == null) return "🏳";
  var countryAbbrev = englishRegion.TwoLetterISORegionName;
  return IsoCountryCodeToFlagEmoji(countryAbbrev);
}
public string IsoCountryCodeToFlagEmoji(string countryCode) => string.Concat(countryCode.ToUpper().Select(x => char.ConvertFromUtf32(x + 0x1F1A5)));

https://itnext.io/convert-country-name-to-flag-emoji-in-c-the.net-ecosystem-115f714d3ef9 https://itnext.io/convert-country-name-to-flag-emoji-in-c-the.net-ecosystem-115f714d3ef9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM