// // Licensed under the MIT License. See LICENSE file in the project root for full license information. // namespace MUnique.OpenMU.DataModel; using System.Globalization; using System.Resources; using Nito.Disposables; using Nito.Disposables.Internals; /// /// Helper class for culture related operations. /// public static class CultureHelper { /// /// Sets the temporary culture for the current thread. /// Should be disposed after usage to revert to the previous culture. /// Should not be used between async calls. /// /// The culture information. /// A disposable to revert to the previous culture. public static IDisposable SetTemporaryCulture(CultureInfo cultureInfo) { var oldUiCulture = CultureInfo.CurrentUICulture; var oldCulture = CultureInfo.CurrentCulture; CultureInfo.CurrentUICulture = cultureInfo; CultureInfo.CurrentCulture = cultureInfo; return Disposable.Create(() => { CultureInfo.CurrentUICulture = oldUiCulture; CultureInfo.CurrentCulture = oldCulture; }); } /// /// Gets the available cultures for a specific resource. /// /// The type of the resources. /// An enumeration of of available cultures of the given resource type. public static IEnumerable GetAvailableCultures() { var resourceManager = new ResourceManager(typeof(TResources)); var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures); var result = cultures .Except([CultureInfo.InvariantCulture]) .Where(culture => culture is { IsNeutralCulture: true, TwoLetterISOLanguageName: "en" } || !object.Equals(resourceManager.GetResourceSet(culture, true, false), null)) .WhereNotNull() .ToList(); return result; } }