|
| 1 | +namespace ServiceLib.Common; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Extension methods for country code utilities |
| 5 | +/// </summary> |
| 6 | +public static class CountryExtension |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Country code to emoji flag mapping for common countries |
| 10 | + /// </summary> |
| 11 | + private static readonly Dictionary<string, string> CountryEmojiMap = new(StringComparer.OrdinalIgnoreCase) |
| 12 | + { |
| 13 | + // Asia |
| 14 | + { "CN", "🇨🇳" }, // China |
| 15 | + { "HK", "🇭🇰" }, // Hong Kong |
| 16 | + { "TW", "🇹🇼" }, // Taiwan |
| 17 | + { "JP", "🇯🇵" }, // Japan |
| 18 | + { "SG", "🇸🇬" }, // Singapore |
| 19 | + { "KR", "🇰🇷" }, // South Korea |
| 20 | + { "TH", "🇹🇭" }, // Thailand |
| 21 | + { "VN", "🇻🇳" }, // Vietnam |
| 22 | + { "ID", "🇮🇩" }, // Indonesia |
| 23 | + { "PH", "🇵🇭" }, // Philippines |
| 24 | + { "MY", "🇲🇾" }, // Malaysia |
| 25 | + { "IN", "🇮🇳" }, // India |
| 26 | + { "PK", "🇵🇰" }, // Pakistan |
| 27 | + { "BD", "🇧🇩" }, // Bangladesh |
| 28 | + { "LK", "🇱🇰" }, // Sri Lanka |
| 29 | + { "KH", "🇰🇭" }, // Cambodia |
| 30 | + { "LA", "🇱🇦" }, // Laos |
| 31 | + { "MM", "🇲🇲" }, // Myanmar |
| 32 | + |
| 33 | + // Americas |
| 34 | + { "US", "🇺🇸" }, // United States |
| 35 | + { "CA", "🇨🇦" }, // Canada |
| 36 | + { "MX", "🇲🇽" }, // Mexico |
| 37 | + { "BR", "🇧🇷" }, // Brazil |
| 38 | + { "AR", "🇦🇷" }, // Argentina |
| 39 | + { "CL", "🇨🇱" }, // Chile |
| 40 | + { "CO", "🇨🇴" }, // Colombia |
| 41 | + |
| 42 | + // Europe |
| 43 | + { "GB", "🇬🇧" }, // United Kingdom |
| 44 | + { "DE", "🇩🇪" }, // Germany |
| 45 | + { "FR", "🇫🇷" }, // France |
| 46 | + { "IT", "🇮🇹" }, // Italy |
| 47 | + { "ES", "🇪🇸" }, // Spain |
| 48 | + { "RU", "🇷🇺" }, // Russia |
| 49 | + { "NL", "🇳🇱" }, // Netherlands |
| 50 | + { "CH", "🇨🇭" }, // Switzerland |
| 51 | + { "SE", "🇸🇪" }, // Sweden |
| 52 | + { "NO", "🇳🇴" }, // Norway |
| 53 | + { "DK", "🇩🇰" }, // Denmark |
| 54 | + { "FI", "🇫🇮" }, // Finland |
| 55 | + { "PL", "🇵🇱" }, // Poland |
| 56 | + { "CZ", "🇨🇿" }, // Czech Republic |
| 57 | + { "AT", "🇦🇹" }, // Austria |
| 58 | + { "GR", "🇬🇷" }, // Greece |
| 59 | + { "PT", "🇵🇹" }, // Portugal |
| 60 | + { "TR", "🇹🇷" }, // Turkey |
| 61 | + { "UA", "🇺🇦" }, // Ukraine |
| 62 | + { "RO", "🇷🇴" }, // Romania |
| 63 | + |
| 64 | + // Middle East & Central Asia |
| 65 | + { "AE", "🇦🇪" }, // United Arab Emirates |
| 66 | + { "SA", "🇸🇦" }, // Saudi Arabia |
| 67 | + { "IL", "🇮🇱" }, // Israel |
| 68 | + { "KZ", "🇰🇿" }, // Kazakhstan |
| 69 | + |
| 70 | + // Oceania |
| 71 | + { "AU", "🇦🇺" }, // Australia |
| 72 | + { "NZ", "🇳🇿" }, // New Zealand |
| 73 | + |
| 74 | + // Africa |
| 75 | + { "ZA", "🇿🇦" }, // South Africa |
| 76 | + { "EG", "🇪🇬" }, // Egypt |
| 77 | + }; |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Converts country code to flag emoji using predefined mapping |
| 81 | + /// Example: "US" -> "🇺🇸", "CN" -> "🇨🇳" |
| 82 | + /// </summary> |
| 83 | + public static string? CountryToEmoji(this string? countryCode) |
| 84 | + { |
| 85 | + if (countryCode.IsNullOrEmpty()) |
| 86 | + { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + return CountryEmojiMap.TryGetValue(countryCode, out var emoji) ? emoji : null; |
| 91 | + } |
| 92 | +} |
0 commit comments