using System.Globalization;
using System.Reflection;
using System.Text.Json;
namespace XFEWarframe.Desktop.Editing;
/// <summary>Interface language is separate from game and Public Export item languages.</summary>
public static class UiText
{
private static readonly Lazy<Dictionary<string, Dictionary<string, string>>> Catalog = new(Load);
private static string language = "zh";
public static string Language
{
get => language;
set => language = Languages.Any(x => x.Code == value) ? value : "en";
}
public static IReadOnlyList<UiLanguage> Languages { get; } =
[new("en", "English"), new("de", "Deutsch"), new("es", "Español"), new("fr", "Français"),
new("pl", "Polski"), new("ru", "Русский"), new("uk", "Українська"), new("zh", "简体中文")];
public static string Get(string key)
{
var data = Catalog.Value;
return data.GetValueOrDefault(Language)?.GetValueOrDefault(key)
?? data.GetValueOrDefault("en")?.GetValueOrDefault(key)
?? data.GetValueOrDefault("zh")?.GetValueOrDefault(key) ?? key;
}
public static string Translate(string source) => Get(source);
public static string Format(string source, params object?[] args) => string.Format(CultureInfo.CurrentCulture, Translate(source), args);
private static Dictionary<string, Dictionary<string, string>> Load()
{
using var stream = typeof(UiText).Assembly.GetManifestResourceStream("XFEWarframe.UiTranslations.json")
?? throw new InvalidOperationException("Interface translation resources are missing.");
return JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(stream)!;
}
}
public sealed record UiLanguage(string Code, string Label)
{
public static UiLanguage Find(string? code) => UiText.Languages.FirstOrDefault(x => x.Code == code) ?? UiText.Languages[0];
}
using System.Globalization;
using System.Reflection;
using System.Text.Json;
namespace XFEWarframe.Desktop.Editing;
/// <summary>Interface language is separate from game and Public Export item languages.</summary>
public static class UiText
{
private static readonly Lazy<Dictionary<string, Dictionary<string, string>>> Catalog = new(Load);
private static string language = "zh";
public static string Language
{
get => language;
set => language = Languages.Any(x => x.Code == value) ? value : "en";
}
public static IReadOnlyList<UiLanguage> Languages { get; } =
[new("en", "English"), new("de", "Deutsch"), new("es", "Español"), new("fr", "Français"),
new("pl", "Polski"), new("ru", "Русский"), new("uk", "Українська"), new("zh", "简体中文")];
public static string Get(string key)
{
var data = Catalog.Value;
return data.GetValueOrDefault(Language)?.GetValueOrDefault(key)
?? data.GetValueOrDefault("en")?.GetValueOrDefault(key)
?? data.GetValueOrDefault("zh")?.GetValueOrDefault(key) ?? key;
}
public static string Translate(string source) => Get(source);
public static string Format(string source, params object?[] args) => string.Format(CultureInfo.CurrentCulture, Translate(source), args);
private static Dictionary<string, Dictionary<string, string>> Load()
{
using var stream = typeof(UiText).Assembly.GetManifestResourceStream("XFEWarframe.UiTranslations.json")
?? throw new InvalidOperationException("Interface translation resources are missing.");
return JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(stream)!;
}
}
public sealed record UiLanguage(string Code, string Label)
{
public static UiLanguage Find(string? code) => UiText.Languages.FirstOrDefault(x => x.Code == code) ?? UiText.Languages[0];
}