TouchyTickets/TouchyTickets/Localization.cs

41 lines
1.6 KiB
C#
Raw Normal View History

2020-06-02 14:16:14 +02:00
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Microsoft.Xna.Framework;
2020-06-04 21:43:11 +02:00
using Microsoft.Xna.Framework.Content;
2020-06-02 14:16:14 +02:00
using MLEM.Startup;
using Newtonsoft.Json;
namespace TouchyTickets {
public static class Localization {
2020-06-25 14:18:57 +02:00
public static readonly List<string> NumberFormat = LoadLocalized<List<string>>("NumberFormat", "json5");
2020-06-04 21:43:11 +02:00
private static readonly Dictionary<string, string> Strings = LoadLocalized<Dictionary<string, string>>("Localization");
private static readonly List<string> News = LoadLocalized<List<string>>("News");
private static readonly Random Random = new Random();
2020-06-02 14:16:14 +02:00
2020-06-04 21:43:11 +02:00
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)];
}
2020-06-25 14:18:57 +02:00
private static T LoadLocalized<T>(string name, string extension = "json") {
2020-06-04 21:43:11 +02:00
var location = GameImpl.Instance.Content.RootDirectory + "/Localization/" + name;
2020-06-02 14:16:14 +02:00
var culture = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
2020-06-25 14:18:57 +02:00
foreach (var path in new[] {$"{location}.{culture}.{extension}", $"{location}.{extension}"}) {
2020-06-02 14:16:14 +02:00
try {
2020-06-04 21:43:11 +02:00
using (var reader = new JsonTextReader(new StreamReader(TitleContainer.OpenStream(path))))
return SaveHandler.Serializer.Deserialize<T>(reader);
2020-06-02 14:16:14 +02:00
} catch (Exception) {
// move on to the next path
}
}
2020-06-04 21:43:11 +02:00
throw new ContentLoadException();
2020-06-02 14:16:14 +02:00
}
}
}