Implemented the language plugin logic

This commit is contained in:
Thorsten Sommer 2025-03-22 14:25:14 +01:00
parent 568a4270ae
commit bcd0138a96
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,21 @@
namespace AIStudio.Tools.PluginSystem;
/// <summary>
/// Represents a contract for a language plugin.
/// </summary>
public interface ILanguagePlugin
{
/// <summary>
/// Tries to get a text from the language plugin.
/// </summary>
/// <remarks>
/// When the key does not exist, the value will be an empty string.
/// Please note that the key is case-sensitive. Furthermore, the keys
/// are in the format "root::key". That means that the keys are
/// hierarchical and separated by "::".
/// </remarks>
/// <param name="key">The key to use to get the text.</param>
/// <param name="value">The desired text.</param>
/// <returns>True if the key exists, false otherwise.</returns>
public bool TryGetText(string key, out string value);
}

View File

@ -0,0 +1,48 @@
using Lua;
namespace AIStudio.Tools.PluginSystem;
public sealed class PluginLanguage : PluginBase, ILanguagePlugin
{
private readonly Dictionary<string, string> content = [];
private ILanguagePlugin? baseLanguage;
public PluginLanguage(string path, LuaState state, PluginType type) : base(path, state, type)
{
if (this.TryInitUITextContent(out var issue, out var readContent))
this.content = readContent;
else
this.pluginIssues.Add(issue);
}
/// <summary>
/// Sets the base language plugin. This plugin will be used to fill in missing keys.
/// </summary>
/// <param name="baseLanguagePlugin">The base language plugin to use.</param>
public void SetBaseLanguage(ILanguagePlugin baseLanguagePlugin) => this.baseLanguage = baseLanguagePlugin;
/// <summary>
/// Tries to get a text from the language plugin.
/// </summary>
/// <remarks>
/// When the key neither in the base language nor in this language exist,
/// the value will be an empty string. Please note that the key is case-sensitive.
/// Furthermore, the keys are in the format "root::key". That means that
/// the keys are hierarchical and separated by "::".
/// </remarks>
/// <param name="key">The key to use to get the text.</param>
/// <param name="value">The desired text.</param>
/// <returns>True if the key exists, false otherwise.</returns>
public bool TryGetText(string key, out string value)
{
if (this.content.TryGetValue(key, out value!))
return true;
if(this.baseLanguage is not null && this.baseLanguage.TryGetText(key, out value))
return true;
value = string.Empty;
return false;
}
}