// // LanguageService.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Security.Cryptography; using NosSmooth.Data.Abstractions.Language; using Remora.Results; namespace NosSmooth.Data.NOSFiles; /// internal class LanguageService : ILanguageService { private readonly IReadOnlyDictionary>> _translations; private readonly LanguageServiceOptions _options; /// /// Initializes a new instance of the class. /// /// The translations. /// The options. public LanguageService(IReadOnlyDictionary>> translations, LanguageServiceOptions options) { CurrentLanguage = options.Language; _translations = translations; _options = options; } /// public Language CurrentLanguage { get; set; } /// public Result GetTranslation(TranslationRoot root, string key, Language? language = default) { if (!_translations.ContainsKey(language ?? CurrentLanguage)) { return new NotFoundError($"The requested language {language ?? CurrentLanguage} is not parsed."); } var translations = _translations[language ?? CurrentLanguage]; if (!translations.ContainsKey(root)) { return key; } var keyTranslations = translations[root]; if (!keyTranslations.ContainsKey(key)) { return key; } return keyTranslations[key]; } /// public Result GetTranslation(TranslatableString translatableString, Language? language = default) { var translation = GetTranslation(translatableString.Root, translatableString.Key, language); if (!translation.IsSuccess) { return translation; } if (_options.FillTranslatableStrings) { translatableString.Fill(translation.Entity); } return translation; } }