Added an option for logging of unknown translation keys

This commit is contained in:
Thorsten Sommer 2025-04-12 20:41:05 +02:00
parent dc427afdbd
commit 022cc9d68d
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 10 additions and 3 deletions

View File

@ -16,8 +16,9 @@ public interface ILanguagePlugin
/// </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);
public bool TryGetText(string key, out string value, bool logWarning = false);
/// <summary>
/// Gets the text from the language plugin.

View File

@ -12,7 +12,7 @@ public sealed class NoPluginLanguage : PluginBase, ILanguagePlugin
#region Implementation of ILanguagePlugin
public bool TryGetText(string key, out string value)
public bool TryGetText(string key, out string value, bool logWarning = false)
{
value = string.Empty;
return true;

View File

@ -4,6 +4,8 @@ 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;
@ -132,8 +134,9 @@ public sealed class PluginLanguage : PluginBase, ILanguagePlugin
/// </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)
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!))
@ -150,6 +153,9 @@ public sealed class PluginLanguage : PluginBase, ILanguagePlugin
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;
}