using System; using System.Collections.Generic; using System.Globalization; using System.Reflection; using System.Resources; using System.Threading; namespace MonoGameBlank2dStartKit.Core.Localization; /// /// Manages localization settings for the game, including retrieving supported cultures and setting the current culture for localization. /// internal class LocalizationManager { /// /// the culture code we default to /// public const string DEFAULT_CULTURE_CODE = "en-EN"; /// /// Retrieves a list of supported cultures based on available language resources in the game. /// This method checks the current culture settings and the satellite assemblies for available localized resources. /// /// A list of objects representing the cultures supported by the game. /// /// This method iterates through all specific cultures defined in the satellite assemblies and attempts to load the corresponding resource set. /// If a resource set is found for a particular culture, that culture is added to the list of supported cultures. The invariant culture /// is always included in the returned list as it represents the default (non-localized) resources. /// public static List GetSupportedCultures() { // Create a list to hold supported cultures List supportedCultures = new List(); // Get the current assembly Assembly assembly = Assembly.GetExecutingAssembly(); // Resource manager for your Resources.resx ResourceManager resourceManager = new ResourceManager("MonoGameBlank2dStartKit.Core.Localization.Resources", assembly); // Get all cultures defined in the satellite assemblies CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures); foreach (CultureInfo culture in cultures) { try { // Try to get the resource set for this culture var resourceSet = resourceManager.GetResourceSet(culture, true, false); if (resourceSet != null) { supportedCultures.Add(culture); } } catch (MissingManifestResourceException) { // This exception is thrown when there is no .resx for the culture, ignore it } } // Always add the default (invariant) culture - the base .resx file supportedCultures.Add(CultureInfo.InvariantCulture); return supportedCultures; } /// /// Sets the current culture of the game based on the specified culture code. /// This method updates both the current culture and UI culture for the current thread. /// /// The culture code (e.g., "en-US", "fr-FR") to set for the game. /// /// This method modifies the and properties, /// which affect how dates, numbers, and other culture-specific values are formatted, as well as how localized resources are loaded. /// public static void SetCulture(string cultureCode) { if (string.IsNullOrEmpty(cultureCode)) throw new ArgumentNullException(nameof(cultureCode), "A culture code must be provided."); // Create a CultureInfo object from the culture code CultureInfo culture = new CultureInfo(cultureCode); // Set the current culture and UI culture for the current thread Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture; } }