mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-05 00:49:40 +00:00
Configure language & start language plugins (#400)
Build and Release / Read metadata (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, deb) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Has been cancelled
Build and Release / Prepare & create release (push) Has been cancelled
Build and Release / Publish release (push) Has been cancelled
Build and Release / Read metadata (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, deb) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Has been cancelled
Build and Release / Prepare & create release (push) Has been cancelled
Build and Release / Publish release (push) Has been cancelled
This commit is contained in:
1 parent
171ed37c27
commit
3fc15d9789
31 files changed
+612
-156
No files matched your search
@@ -4,9 +4,12 @@ namespace AIStudio.Tools.PluginSystem;
|
||||
|
||||
public sealed class PluginLanguage : PluginBase, ILanguagePlugin
|
||||
{
|
||||
private static readonly ILogger<PluginLanguage> LOGGER = Program.LOGGER_FACTORY.CreateLogger<PluginLanguage>();
|
||||
|
||||
private readonly Dictionary<string, string> content = [];
|
||||
private readonly List<ILanguagePlugin> otherLanguagePlugins = [];
|
||||
private readonly string langCultureTag;
|
||||
private readonly string langName;
|
||||
|
||||
private ILanguagePlugin? baseLanguage;
|
||||
|
||||
@@ -15,6 +18,9 @@ public sealed class PluginLanguage : PluginBase, ILanguagePlugin
|
||||
if(!this.TryInitIETFTag(out var issue, out this.langCultureTag))
|
||||
this.pluginIssues.Add(issue);
|
||||
|
||||
if(!this.TryInitLangName(out issue, out this.langName))
|
||||
this.pluginIssues.Add(issue);
|
||||
|
||||
if (this.TryInitUITextContent(out issue, out var readContent))
|
||||
this.content = readContent;
|
||||
else
|
||||
@@ -36,39 +42,6 @@ public sealed class PluginLanguage : PluginBase, ILanguagePlugin
|
||||
/// </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>
|
||||
/// <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)
|
||||
{
|
||||
// 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;
|
||||
|
||||
value = string.Empty;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to initialize the IETF tag.
|
||||
@@ -127,4 +100,71 @@ public sealed class PluginLanguage : PluginBase, ILanguagePlugin
|
||||
message = string.Empty;
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryInitLangName(out string message, out string readLangName)
|
||||
{
|
||||
if (!this.state.Environment["LANG_NAME"].TryRead(out readLangName))
|
||||
{
|
||||
message = "The field LANG_NAME does not exist or is not a valid string.";
|
||||
readLangName = string.Empty;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(readLangName))
|
||||
{
|
||||
message = "The field LANG_NAME is empty. Use a valid language name.";
|
||||
readLangName = string.Empty;
|
||||
return false;
|
||||
}
|
||||
|
||||
message = string.Empty;
|
||||
return true;
|
||||
}
|
||||
|
||||
#region Implementation of ILanguagePlugin
|
||||
|
||||
/// <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>
|
||||
/// <param name="logWarning">When true, a warning will be logged if the key does not exist.</param>
|
||||
/// <returns>True if the key exists, false otherwise.</returns>
|
||||
public bool TryGetText(string key, out string value, bool logWarning = false)
|
||||
{
|
||||
// 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;
|
||||
|
||||
if(logWarning)
|
||||
LOGGER.LogWarning($"Missing translation key '{key}'.");
|
||||
|
||||
value = string.Empty;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string IETFTag => this.langCultureTag;
|
||||
|
||||
/// <inheritdoc />
|
||||
public string LangName => this.langName;
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in new issue
Block a user