using System.Globalization; using AIStudio.Settings.DataModel; namespace AIStudio.Tools.PluginSystem; public class I18N : ILang { public static readonly I18N I = new(); private static readonly ILogger LOG = Program.LOGGER_FACTORY.CreateLogger(); private ILanguagePlugin? language; private I18N() { } /// /// How the language in use writes its numbers, or the invariant culture while none is loaded. /// /// /// A number standing inside a translated sentence has to be written the way that language /// writes numbers. AI Studio's language is chosen in its own settings and never moves the /// thread's culture along with it, so a number formatted from the thread comes out with English /// separators inside a German sentence. It lives here because it is the same decision as the /// texts: whoever picked the language picked how its numbers look. /// /// Components which already hold the active plugin may keep deriving it themselves. This is for /// the code which has no plugin to ask -- a provider building an error message, say. /// public CultureInfo Culture { get; private set; } = CultureInfo.InvariantCulture; /// /// Asks where the form of address comes from, see AddressForm. /// private Func addressFormSource = () => AddressForm.FORMAL; /// /// How the texts of TA() address the person, in languages which tell a formal address from an informal one. /// /// /// 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. /// public AddressForm AddressForm => this.addressFormSource(); public static void Init(ILanguagePlugin language) { I.language = language; I.Culture = CommonTools.DeriveActiveCultureOrInvariant(language.IETFTag); } /// /// Sets where the form of address comes from. /// /// /// The source is asked for every text, so a changed setting shows as soon as a component renders again. /// /// Gets the form of address the person chose. public static void UseAddressFormOf(Func source) => I.addressFormSource = source; /// /// Gets a text which addresses the reader, in the form of address the person chose; see AddressForm. /// /// The text in English (US). /// The namespace of the type requesting the text, used as part of the key. /// The name of the type requesting the text, used as part of the key. /// The text from the language plugin or the English text. 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) { LOG.LogWarning("Using I18N.I.T without namespace and type is probably wrong, because the I18N key collection process of the build system will not find those keys."); if(this.language is not null) return this.GetText(this.language, fallbackEN); return fallbackEN; } public string T(string fallbackEN, string? typeNamespace, string? typeName) { if(this.language is not null) return this.GetText(this.language, fallbackEN, typeNamespace, typeName); return fallbackEN; } #endregion }