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