diff --git a/src/Lepo.i18n/Translator.cs b/src/Lepo.i18n/Translator.cs new file mode 100644 index 0000000..6c633b0 --- /dev/null +++ b/src/Lepo.i18n/Translator.cs @@ -0,0 +1,64 @@ +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. +// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors. +// All Rights Reserved. + +namespace Lepo.i18n; + +/// +/// Provides functionality to translate strings. This class is obsolete and should be used instead. +/// +[Obsolete($"{nameof(Translator)} is obsolete, use {nameof(LocalizationProvider)} instead.")] +public static class Translator +{ + /// + /// Translates a string to the current culture. + /// + /// The string to translate. + /// The translated string if a translation exists, otherwise the original string. + [Obsolete("This method is obsolete and should not be used.")] + public static string String(string value) + { + if (value is null) + { + return string.Empty; + } + + LocalizationSet? localizationSet = LocalizationProvider + .GetInstance() + ?.Get(LocalizationProvider.GetInstance()?.GetCulture() ?? CultureInfo.CurrentUICulture); + ; + + if (localizationSet is null) + { + return value; + } + + return localizationSet.Strings.FirstOrDefault(s => s.Key == value).Value ?? value; + } + + /// + /// Prepares a string for translation. This method is obsolete and should not be used. + /// + /// The text or key to prepare. + /// The parameters to use in the prepared string. + /// Throws an exception indicating that this method is obsolete. + [Obsolete("This method is obsolete and should not be used.")] + public static string Prepare(string value, params object[] parameters) + { + throw new Exception("This method is obsolete and should not be used."); + } + + /// + /// Translates a string to the correct plural form for the current culture. This method is obsolete and should not be used. + /// + /// The singular form of the string. + /// The plural form of the string. + /// The number to determine the correct form. + /// Throws an exception indicating that this method is obsolete. + [Obsolete("This method is obsolete and should not be used.")] + public static string Plural(string single, string plural, object number) + { + throw new Exception("This method is obsolete and should not be used."); + } +} diff --git a/tests/Lepo.i18n.UnitTests/Yaml/FromYamlTests.cs b/tests/Lepo.i18n.UnitTests/Yaml/FromYamlTests.cs index 3912661..cdc27b1 100644 --- a/tests/Lepo.i18n.UnitTests/Yaml/FromYamlTests.cs +++ b/tests/Lepo.i18n.UnitTests/Yaml/FromYamlTests.cs @@ -13,6 +13,7 @@ public sealed class FromYamlTests public void FromYaml_ShouldProperlyAddLocalizations() { LocalizationBuilder builder = new(); + _ = builder.FromYaml( "Lepo.i18n.UnitTests.Resources.Translations.pl_PL.yaml", new CultureInfo("pl-PL")