PKHeX/PKHeX.Core/Editing/BattleTemplate/BattleTemplateLocalization.cs
Kurt f730f7d19a
Feature: Localization of Battle Templates (Showdown Set) (#4482)
* Localization capability for each language, import & export
* Lines with stat names (IVs/EVs) can be configured to many representations (X/X/X/X/X/X, HABCDS, etc).
* Add nonstandard localizations
* Add token types for Showdown's new set format
* Add new program settings for hover & export styles. Allows users to select which presentation format they want for the hover previews, as well as the set export format.
* Revises preview hover GUI to use new settings
* Revises export events to use new settings
* Moves no longer indicate end of set
* Enhance robustness of stat parsing
* Expand all settings in settings editor on form load
* Extract clipboard -> sets operation to api for maintainability & reusability
2025-05-01 23:16:36 -05:00

79 lines
2.7 KiB
C#

using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace PKHeX.Core;
/// <summary>
/// Provides information for localizing <see cref="IBattleTemplate"/> sets.
/// </summary>
/// <param name="Strings">In-game strings</param>
/// <param name="Config">Grammar and prefix/suffix tokens</param>
public sealed record BattleTemplateLocalization(GameStrings Strings, BattleTemplateConfig Config)
{
public const string DefaultLanguage = GameLanguage.DefaultLanguage; // English
private static readonly Dictionary<string, BattleTemplateLocalization> Cache = new();
public static readonly BattleTemplateLocalization Default = GetLocalization(DefaultLanguage);
/// <param name="language"><see cref="LanguageID"/> index</param>
/// <inheritdoc cref="GetLocalization(string)"/>
public static BattleTemplateLocalization GetLocalization(LanguageID language) =>
GetLocalization(language.GetLanguageCode());
/// <summary>
/// Gets the localization for the requested language.
/// </summary>
/// <param name="language">Language code</param>
public static BattleTemplateLocalization GetLocalization(string language)
{
if (Cache.TryGetValue(language, out var result))
return result;
var strings = GameInfo.GetStrings(language);
var cfg = GetConfig(language);
result = new BattleTemplateLocalization(strings, cfg);
Cache[language] = result;
return result;
}
private static string GetJson(string language) => Util.GetStringResource($"battle_{language}.json");
private static BattleTemplateConfigContext GetContext() => new();
private static BattleTemplateConfig GetConfig(string language)
{
var text = GetJson(language);
var result = JsonSerializer.Deserialize(text, GetContext().BattleTemplateConfig)
?? throw new JsonException($"Failed to deserialize {nameof(BattleTemplateConfig)} for {language}");
return result;
}
/// <summary>
/// Force loads all localizations.
/// </summary>
public static bool ForceLoadAll()
{
bool anyLoaded = false;
foreach (var lang in GameLanguage.AllSupportedLanguages)
{
if (Cache.ContainsKey(lang))
continue;
_ = GetLocalization(lang);
anyLoaded = true;
}
return anyLoaded;
}
/// <summary>
/// Gets all localizations.
/// </summary>
public static IReadOnlyDictionary<string, BattleTemplateLocalization> GetAll()
{
_ = ForceLoadAll();
return Cache;
}
}
[JsonSerializable(typeof(BattleTemplateConfig))]
public sealed partial class BattleTemplateConfigContext : JsonSerializerContext;