49 lines
No EOL
1.9 KiB
C#
49 lines
No EOL
1.9 KiB
C#
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<string> NumberFormat = Localization.LoadLocalized<List<string>>("NumberFormat", "json5");
|
|
private static readonly Dictionary<string, string> Strings = Localization.LoadLocalized<Dictionary<string, string>>("Localization");
|
|
private static readonly Dictionary<string, string> FallbackStrings = Localization.Load<Dictionary<string, string>>("Localization");
|
|
private static readonly List<string> News = Localization.LoadLocalized<List<string>>("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<T>(string name, string extension = "json") {
|
|
var culture = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
|
|
foreach (var path in new[] {$"{name}.{culture}", name}) {
|
|
try {
|
|
return Localization.Load<T>(path, extension);
|
|
} catch (Exception) {
|
|
// move on to the next path
|
|
}
|
|
}
|
|
throw new ContentLoadException();
|
|
}
|
|
|
|
private static T Load<T>(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<T>(reader);
|
|
}
|
|
|
|
} |