mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-10 17:27:21 +00:00
Added the informal address to the BeyondFacts Lab (#1176)
This commit is contained in:
1 parent
b6c3fbb5db
commit
e77645f44c
48 files changed
+1254
-160
No files matched your search
@@ -1,5 +1,7 @@
|
||||
using System.Globalization;
|
||||
|
||||
using AIStudio.Settings.DataModel;
|
||||
|
||||
namespace AIStudio.Tools.PluginSystem;
|
||||
|
||||
public class I18N : ILang
|
||||
@@ -28,12 +30,51 @@ public class I18N : ILang
|
||||
/// </remarks>
|
||||
public CultureInfo Culture { get; private set; } = CultureInfo.InvariantCulture;
|
||||
|
||||
/// <summary>
|
||||
/// Asks where the form of address comes from, see AddressForm.
|
||||
/// </summary>
|
||||
private Func<AddressForm> addressFormSource = () => AddressForm.FORMAL;
|
||||
|
||||
/// <summary>
|
||||
/// How the texts of TA() address the person, in languages which tell a formal address from an informal one.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Like the language, it holds for every text, so that a static class, such as one with the labels of an enum,
|
||||
/// addresses the person the same way as a component does. Only texts of TA() have an informal variant; all
|
||||
/// other texts stay formal. Program.cs decides where the choice comes from, see UseAddressFormOf.
|
||||
/// </remarks>
|
||||
public AddressForm AddressForm => this.addressFormSource();
|
||||
|
||||
public static void Init(ILanguagePlugin language)
|
||||
{
|
||||
I.language = language;
|
||||
I.Culture = CommonTools.DeriveActiveCultureOrInvariant(language.IETFTag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets where the form of address comes from.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The source is asked for every text, so a changed setting shows as soon as a component renders again.
|
||||
/// </remarks>
|
||||
/// <param name="source">Gets the form of address the person chose.</param>
|
||||
public static void UseAddressFormOf(Func<AddressForm> source) => I.addressFormSource = source;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a text which addresses the reader, in the form of address the person chose; see AddressForm.
|
||||
/// </summary>
|
||||
/// <param name="fallbackEN">The text in English (US).</param>
|
||||
/// <param name="typeNamespace">The namespace of the type requesting the text, used as part of the key.</param>
|
||||
/// <param name="typeName">The name of the type requesting the text, used as part of the key.</param>
|
||||
/// <returns>The text from the language plugin or the English text.</returns>
|
||||
public string TA(string fallbackEN, string? typeNamespace, string? typeName)
|
||||
{
|
||||
if(this.language is not null)
|
||||
return this.GetText(this.language, fallbackEN, typeNamespace, typeName, this.AddressForm);
|
||||
|
||||
return fallbackEN;
|
||||
}
|
||||
|
||||
#region Implementation of ILang
|
||||
|
||||
public string T(string fallbackEN)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using AIStudio.Settings.DataModel;
|
||||
|
||||
using SharedTools;
|
||||
|
||||
namespace AIStudio.Tools.PluginSystem;
|
||||
@@ -5,30 +7,47 @@ namespace AIStudio.Tools.PluginSystem;
|
||||
public static class ILangExtensions
|
||||
{
|
||||
private static readonly ILogger<ILang> LOGGER = Program.LOGGER_FACTORY.CreateLogger<ILang>();
|
||||
|
||||
|
||||
extension(ILang lang)
|
||||
{
|
||||
public string GetText(ILanguagePlugin plugin, string fallbackEN, string? typeNamespace = null, string? typeName = null)
|
||||
/// <summary>
|
||||
/// Gets a text from a language plugin.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A text of TA() asks for the form of address the person chose. Its informal variant counts only when the
|
||||
/// language in use has it: the base language would answer with English, while the formal variant of the
|
||||
/// language in use is the better fallback. Missing both, the text stays English.
|
||||
/// </remarks>
|
||||
/// <param name="plugin">The language plugin in use.</param>
|
||||
/// <param name="fallbackEN">The text in English (US), which also makes its key.</param>
|
||||
/// <param name="typeNamespace">The namespace of the type requesting the text, or null for the type of the receiver.</param>
|
||||
/// <param name="typeName">The name of the type requesting the text, or null for the type of the receiver.</param>
|
||||
/// <param name="addressForm">How the text addresses the reader; only texts of TA() have an informal variant.</param>
|
||||
/// <returns>The text in the language of the plugin, or the English text.</returns>
|
||||
public string GetText(ILanguagePlugin plugin, string fallbackEN, string? typeNamespace = null, string? typeName = null, AddressForm addressForm = AddressForm.FORMAL)
|
||||
{
|
||||
var type = lang.GetType();
|
||||
typeName ??= type.Name;
|
||||
typeNamespace ??= type.Namespace!;
|
||||
|
||||
|
||||
// When the type's name ends with `1 or `2, etc. (i.e., generic classes), remove it:
|
||||
if(typeName.Contains('`'))
|
||||
typeName = typeName[..typeName.IndexOf('`')];
|
||||
|
||||
|
||||
var ns = $"{typeNamespace}::{typeName}".ToUpperInvariant().Replace(".", "::");
|
||||
var key = $"root::{ns}::T{I18NKeyHash.Of(fallbackEN)}";
|
||||
|
||||
|
||||
if(plugin is NoPluginLanguage)
|
||||
return fallbackEN;
|
||||
|
||||
|
||||
if(addressForm is AddressForm.INFORMAL && plugin.Content.TryGetValue(I18NAddressVariant.InformalKeyOf(key), out var informalText) && !string.IsNullOrWhiteSpace(informalText))
|
||||
return informalText;
|
||||
|
||||
if(plugin.TryGetText(key, out var text, logWarning: false))
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(text))
|
||||
return fallbackEN;
|
||||
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
using SharedTools;
|
||||
|
||||
namespace AIStudio.Tools.PluginSystem;
|
||||
|
||||
public static class ILanguagePluginExtensions
|
||||
{
|
||||
/// <param name="plugin">The language plugin.</param>
|
||||
extension(ILanguagePlugin plugin)
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether the language tells a formal address from an informal one, so that a choice between both
|
||||
/// makes sense.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The plugin decides by its texts, not by a list of languages: it needs an informal variant of a text of
|
||||
/// TA() which reads differently than the formal one. English addresses everybody alike, so its plugin holds
|
||||
/// the informal variants in the same words. A language translated before it got informal variants offers
|
||||
/// no choice either, since it would change nothing.
|
||||
/// </remarks>
|
||||
/// <returns>True, when at least one informal variant differs from its formal one.</returns>
|
||||
public bool HasInformalAddress()
|
||||
{
|
||||
var content = plugin.Content;
|
||||
foreach (var (key, text) in content)
|
||||
{
|
||||
if (I18NAddressVariant.TryGetFormalKey(key, out var formalKey) && content.TryGetValue(formalKey, out var formalText) && !string.Equals(text, formalText, StringComparison.Ordinal))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user