| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="CultureExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.Linq; |
| | | 10 | | |
| | | 11 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 12 | | namespace MyNet.Geography; |
| | | 13 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 14 | | |
| | | 15 | | public static class CultureExtensions |
| | | 16 | | { |
| | | 17 | | extension(CultureInfo culture) |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the country associated with the specified culture, if available. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <returns>The country associated with the specified culture, or <c>null</c> if not available.</returns> |
| | | 23 | | public Country? GetCountry() |
| | | 24 | | { |
| | 6 | 25 | | ArgumentNullException.ThrowIfNull(culture); |
| | | 26 | | |
| | 6 | 27 | | if (string.IsNullOrEmpty(culture.Name)) |
| | 2 | 28 | | return null; |
| | | 29 | | |
| | | 30 | | try |
| | | 31 | | { |
| | 4 | 32 | | var specificCulture = culture.IsNeutralCulture ? CultureInfo.CreateSpecificCulture(culture.Name) : cultu |
| | | 33 | | |
| | 4 | 34 | | var alpha2 = new RegionInfo(specificCulture.Name).TwoLetterISORegionName; |
| | | 35 | | |
| | 4 | 36 | | return Country.All.FirstOrDefault(c => string.Equals(c.Alpha2, alpha2, StringComparison.OrdinalIgnoreCas |
| | | 37 | | } |
| | 0 | 38 | | catch (ArgumentException) |
| | | 39 | | { |
| | 0 | 40 | | return null; |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | |