Add support for additional language plugins in PluginLanguage

This commit is contained in:
Thorsten Sommer 2025-04-03 10:33:06 +02:00
parent 158252dc34
commit 926f4e1a90
No known key found for this signature in database
GPG Key ID: B0B7E2FC074BF1F5

View File

@ -5,6 +5,7 @@ namespace AIStudio.Tools.PluginSystem;
public sealed class PluginLanguage : PluginBase, ILanguagePlugin public sealed class PluginLanguage : PluginBase, ILanguagePlugin
{ {
private readonly Dictionary<string, string> content = []; private readonly Dictionary<string, string> content = [];
private readonly List<ILanguagePlugin> otherLanguagePlugins = [];
private ILanguagePlugin? baseLanguage; private ILanguagePlugin? baseLanguage;
@ -22,6 +23,16 @@ public sealed class PluginLanguage : PluginBase, ILanguagePlugin
/// <param name="baseLanguagePlugin">The base language plugin to use.</param> /// <param name="baseLanguagePlugin">The base language plugin to use.</param>
public void SetBaseLanguage(ILanguagePlugin baseLanguagePlugin) => this.baseLanguage = baseLanguagePlugin; public void SetBaseLanguage(ILanguagePlugin baseLanguagePlugin) => this.baseLanguage = baseLanguagePlugin;
/// <summary>
/// Add another language plugin. This plugin will be used to fill in missing keys.
/// </summary>
/// <remarks>
/// Use this method to add (i.e., register) an assistant plugin as a language plugin.
/// This is necessary because the assistant plugins need to serve their own texts.
/// </remarks>
/// <param name="languagePlugin">The language plugin to add.</param>
public void AddOtherLanguagePlugin(ILanguagePlugin languagePlugin) => this.otherLanguagePlugins.Add(languagePlugin);
/// <summary> /// <summary>
/// Tries to get a text from the language plugin. /// Tries to get a text from the language plugin.
/// </summary> /// </summary>
@ -36,9 +47,18 @@ public sealed class PluginLanguage : PluginBase, ILanguagePlugin
/// <returns>True if the key exists, false otherwise.</returns> /// <returns>True if the key exists, false otherwise.</returns>
public bool TryGetText(string key, out string value) public bool TryGetText(string key, out string value)
{ {
// First, we check if the key is part of the main language pack:
if (this.content.TryGetValue(key, out value!)) if (this.content.TryGetValue(key, out value!))
return true; return true;
// Second, we check if the key is part of the other language packs, such as the assistant plugins:
foreach (var otherLanguagePlugin in this.otherLanguagePlugins)
if(otherLanguagePlugin.TryGetText(key, out value))
return true;
// Finally, we check if the key is part of the base language pack. This is the case,
// when a language plugin does not cover all keys. In this case, the base language plugin
// will be used to fill in the missing keys:
if(this.baseLanguage is not null && this.baseLanguage.TryGetText(key, out value)) if(this.baseLanguage is not null && this.baseLanguage.TryGetText(key, out value))
return true; return true;