From 8ff66f8622a16c77f239fc59973753e6010a42ee Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Thu, 17 Apr 2025 12:38:45 +0200 Subject: [PATCH] Refactor translation logic into ILangExtensions utility. --- .../Components/MSGComponentBase.cs | 21 +------------- .../Tools/PluginSystem/ILangExtensions.cs | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 20 deletions(-) create mode 100644 app/MindWork AI Studio/Tools/PluginSystem/ILangExtensions.cs diff --git a/app/MindWork AI Studio/Components/MSGComponentBase.cs b/app/MindWork AI Studio/Components/MSGComponentBase.cs index fa0c456d..c4c3a49a 100644 --- a/app/MindWork AI Studio/Components/MSGComponentBase.cs +++ b/app/MindWork AI Studio/Components/MSGComponentBase.cs @@ -35,26 +35,7 @@ public abstract class MSGComponentBase : ComponentBase, IDisposable, IMessageBus #region Implementation of ILang /// - public string T(string fallbackEN) - { - var type = this.GetType(); - var ns = $"{type.Namespace!}::{type.Name}".ToUpperInvariant().Replace(".", "::"); - var key = $"root::{ns}::T{fallbackEN.ToFNV32()}"; - - if(this.Lang is NoPluginLanguage) - return fallbackEN; - - if(this.Lang.TryGetText(key, out var text, logWarning: false)) - { - if(string.IsNullOrWhiteSpace(text)) - return fallbackEN; - - return text; - } - - this.Logger.LogWarning($"Missing translation key '{key}' for content '{fallbackEN}'."); - return fallbackEN; - } + public string T(string fallbackEN) => this.GetText(this.Lang, fallbackEN); #endregion diff --git a/app/MindWork AI Studio/Tools/PluginSystem/ILangExtensions.cs b/app/MindWork AI Studio/Tools/PluginSystem/ILangExtensions.cs new file mode 100644 index 00000000..8b457b04 --- /dev/null +++ b/app/MindWork AI Studio/Tools/PluginSystem/ILangExtensions.cs @@ -0,0 +1,29 @@ +using SharedTools; + +namespace AIStudio.Tools.PluginSystem; + +public static class ILangExtensions +{ + private static readonly ILogger LOGGER = Program.LOGGER_FACTORY.CreateLogger(); + + public static string GetText(this ILang lang, ILanguagePlugin plugin, string fallbackEN) + { + var type = lang.GetType(); + var ns = $"{type.Namespace!}::{type.Name}".ToUpperInvariant().Replace(".", "::"); + var key = $"root::{ns}::T{fallbackEN.ToFNV32()}"; + + if(plugin is NoPluginLanguage) + return fallbackEN; + + if(plugin.TryGetText(key, out var text, logWarning: false)) + { + if(string.IsNullOrWhiteSpace(text)) + return fallbackEN; + + return text; + } + + LOGGER.LogWarning($"Missing translation key '{key}' for content '{fallbackEN}'."); + return fallbackEN; + } +} \ No newline at end of file