Added indexer to language plugins

This commit is contained in:
Thorsten Sommer 2025-04-12 17:45:35 +02:00
parent 6e8f9e5224
commit e2fc7619eb
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 47 additions and 33 deletions

View File

@ -19,6 +19,15 @@ public interface ILanguagePlugin
/// <returns>True if the key exists, false otherwise.</returns>
public bool TryGetText(string key, out string value);
/// <summary>
/// Gets the text from the language plugin.
/// </summary>
/// <remarks>
/// When the key does not exist, the value will be an empty string.
/// </remarks>
/// <param name="key">The key to use to get the text.</param>
public string this[string key] { get; }
/// <summary>
/// Gets the IETF tag of the language plugin.
/// </summary>

View File

@ -40,39 +40,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.
@ -152,6 +119,44 @@ public sealed class PluginLanguage : PluginBase, ILanguagePlugin
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>
/// <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;
}
/// <inheritdoc />
public string this[string key] => this.TryGetText(key, out var value) ? value : "string.Empty";
/// <inheritdoc />
public string IETFTag => this.langCultureTag;