123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using System;
- using System.Linq;
- using System.IO;
- using System.Collections.Generic;
- using Newtonsoft.Json.Linq;
- using BepInEx.Configuration;
- namespace COM3D2.MeidoPhotoStudio.Plugin
- {
- internal static class Translation
- {
- private static readonly string[] props = { "ui", "props", "bg", "face" };
- private static Dictionary<string, Dictionary<string, string>> Translations;
- private static readonly ConfigEntry<string> currentLanguage;
- public static string CurrentLanguage
- {
- get => currentLanguage.Value;
- private set => currentLanguage.Value = value;
- }
- public static event EventHandler ReloadTranslationEvent;
- static Translation()
- {
- currentLanguage = Configuration.Config.Bind<string>(
- "Translation", "Language",
- "en",
- "Directory to pull translations from"
- + "\nTranslations are found in the 'Translations' folder"
- );
- Configuration.Config.ConfigReloaded += OnSettingChange;
- }
- static void OnSettingChange(object sender, EventArgs args) => ReloadTranslation();
- public static void Initialize(string language)
- {
- string rootTranslationPath = Path.Combine(Constants.configPath, Constants.translationDirectory);
- string currentTranslationPath = Path.Combine(rootTranslationPath, language);
- if (!Directory.Exists(currentTranslationPath))
- {
- Utility.LogWarning(
- $"No translations found for '{language}' in '{currentTranslationPath}'"
- );
- return;
- }
- Translations = new Dictionary<string, Dictionary<string, string>>(
- StringComparer.InvariantCultureIgnoreCase
- );
- foreach (string prop in props)
- {
- string translationFile = $"translation.{prop}.json";
- try
- {
- string translationPath = Path.Combine(currentTranslationPath, translationFile);
- string translationJson = File.ReadAllText(translationPath);
- JObject translation = JObject.Parse(translationJson);
- foreach (JProperty translationProp in translation.AsJEnumerable())
- {
- JToken token = translationProp.Value;
- Translations[translationProp.Path] = new Dictionary<string, string>(
- token.ToObject<Dictionary<string, string>>(), StringComparer.InvariantCultureIgnoreCase
- );
- }
- }
- catch
- {
- Utility.LogError($"Could not find translation file '{translationFile}'");
- }
- }
- }
- public static void ReinitializeTranslation() => Configuration.Config.Reload();
- public static void ReloadTranslation()
- {
- Initialize(CurrentLanguage);
- OnReloadTranslation();
- }
- public static void OnReloadTranslation()
- {
- ReloadTranslationEvent?.Invoke(null, EventArgs.Empty);
- }
- public static bool Has(string category, string text, bool warn = false)
- {
- if (!Translations.ContainsKey(category))
- {
- if (warn)
- {
- Utility.LogWarning($"Could not translate '{text}': category '{category}' was not found");
- }
- return false;
- }
- if (!Translations[category].ContainsKey(text))
- {
- if (warn)
- {
- Utility.LogWarning(
- $"Could not translate '{text}': '{text}' was not found in category '{category}'"
- );
- }
- return false;
- }
- return true;
- }
- public static string Get(string category, string text, bool warn = true)
- {
- return Has(category, text, warn) ? Translations[category][text] : text;
- }
- public static string[] GetArray(string category, IEnumerable<string> list)
- {
- return GetList(category, list).ToArray();
- }
- public static IEnumerable<string> GetList(string category, IEnumerable<string> list)
- {
- return list.Select(uiName => Get(category, uiName));
- }
- }
- }
|