using System; using System.Collections.Generic; using System.Globalization; using System.IO; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Newtonsoft.Json; namespace TouchyTickets; public static class Localization { public static readonly List NumberFormat = Localization.LoadLocalized>("NumberFormat", "json5"); private static readonly Dictionary Strings = Localization.LoadLocalized>("Localization"); private static readonly Dictionary FallbackStrings = Localization.Load>("Localization"); private static readonly List News = Localization.LoadLocalized>("News"); private static readonly Random Random = new(); public static string Get(string key) { if (Localization.Strings.TryGetValue(key, out var val)) return val; if (Localization.FallbackStrings.TryGetValue(key, out var fallback)) return fallback; return $"?{key}?"; } public static string GetRandomNews() { return Localization.News[Localization.Random.Next(Localization.News.Count)]; } private static T LoadLocalized(string name, string extension = "json") { var culture = CultureInfo.CurrentCulture.TwoLetterISOLanguageName; foreach (var path in new[] {$"{name}.{culture}", name}) { try { return Localization.Load(path, extension); } catch (Exception) { // move on to the next path } } throw new ContentLoadException(); } private static T Load(string name, string extension = "json") { var path = $"{GameImpl.Instance.Content.RootDirectory}/Localization/{name}.{extension}"; using var reader = new JsonTextReader(new StreamReader(TitleContainer.OpenStream(path))); return SaveHandler.Serializer.Deserialize(reader); } }