Skip to content

Add support for additional language plugins #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/MindWork AI Studio/Tools/PluginSystem/PluginLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace AIStudio.Tools.PluginSystem;
public sealed class PluginLanguage : PluginBase, ILanguagePlugin
{
private readonly Dictionary<string, string> content = [];
private readonly List<ILanguagePlugin> otherLanguagePlugins = [];

private ILanguagePlugin? baseLanguage;

Expand All @@ -22,6 +23,16 @@ public PluginLanguage(bool isInternal, LuaState state, PluginType type) : base(i
/// <param name="baseLanguagePlugin">The base language plugin to use.</param>
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>
/// Tries to get a text from the language plugin.
/// </summary>
Expand All @@ -36,9 +47,18 @@ public PluginLanguage(bool isInternal, LuaState state, PluginType type) : base(i
/// <returns>True if the key exists, false otherwise.</returns>
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!))
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))
return true;

Expand Down