mirror of https://github.com/lidarr/Lidarr
Initial support to use named tokens for backend translations
Towards #4210 (cherry picked from commit 11f96c31048c2d1aafca0c91736d439f7f9a95a8)
This commit is contained in:
parent
f4c718554b
commit
a9bef2b4a1
|
@ -0,0 +1,81 @@
|
||||||
|
using System;
|
||||||
|
using FluentAssertions;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
|
using NzbDrone.Core.Configuration;
|
||||||
|
using NzbDrone.Core.Languages;
|
||||||
|
using NzbDrone.Core.Localization;
|
||||||
|
using NzbDrone.Core.Test.Framework;
|
||||||
|
using NzbDrone.Test.Common;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Test.Localization
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class LocalizationServiceFixture : CoreTest<LocalizationService>
|
||||||
|
{
|
||||||
|
[SetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
Mocker.GetMock<IConfigService>().Setup(m => m.UILanguage).Returns((int)Language.English);
|
||||||
|
|
||||||
|
Mocker.GetMock<IAppFolderInfo>().Setup(m => m.StartUpFolder).Returns(TestContext.CurrentContext.TestDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_get_string_in_dictionary_if_lang_exists_and_string_exists()
|
||||||
|
{
|
||||||
|
var localizedString = Subject.GetLocalizedString("UiLanguage");
|
||||||
|
|
||||||
|
localizedString.Should().Be("UI Language");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_get_string_in_french()
|
||||||
|
{
|
||||||
|
Mocker.GetMock<IConfigService>().Setup(m => m.UILanguage).Returns((int)Language.French);
|
||||||
|
|
||||||
|
var localizedString = Subject.GetLocalizedString("UiLanguage");
|
||||||
|
|
||||||
|
localizedString.Should().Be("Langue de l'IU");
|
||||||
|
|
||||||
|
ExceptionVerification.ExpectedErrors(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_get_string_in_default_dictionary_if_unknown_language_and_string_exists()
|
||||||
|
{
|
||||||
|
Mocker.GetMock<IConfigService>().Setup(m => m.UILanguage).Returns(0);
|
||||||
|
var localizedString = Subject.GetLocalizedString("UiLanguage");
|
||||||
|
|
||||||
|
localizedString.Should().Be("UI Language");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_return_argument_if_string_doesnt_exists()
|
||||||
|
{
|
||||||
|
var localizedString = Subject.GetLocalizedString("badString");
|
||||||
|
|
||||||
|
localizedString.Should().Be("badString");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_return_argument_if_string_doesnt_exists_default_lang()
|
||||||
|
{
|
||||||
|
var localizedString = Subject.GetLocalizedString("badString");
|
||||||
|
|
||||||
|
localizedString.Should().Be("badString");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_throw_if_empty_string_passed()
|
||||||
|
{
|
||||||
|
Assert.Throws<ArgumentNullException>(() => Subject.GetLocalizedString(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_throw_if_null_string_passed()
|
||||||
|
{
|
||||||
|
Assert.Throws<ArgumentNullException>(() => Subject.GetLocalizedString(null));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NzbDrone.Common.Cache;
|
using NzbDrone.Common.Cache;
|
||||||
|
@ -18,13 +19,16 @@ namespace NzbDrone.Core.Localization
|
||||||
public interface ILocalizationService
|
public interface ILocalizationService
|
||||||
{
|
{
|
||||||
Dictionary<string, string> GetLocalizationDictionary();
|
Dictionary<string, string> GetLocalizationDictionary();
|
||||||
|
|
||||||
string GetLocalizedString(string phrase);
|
string GetLocalizedString(string phrase);
|
||||||
string GetLocalizedString(string phrase, string language);
|
string GetLocalizedString(string phrase, Dictionary<string, object> tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class LocalizationService : ILocalizationService, IHandleAsync<ConfigSavedEvent>
|
public class LocalizationService : ILocalizationService, IHandleAsync<ConfigSavedEvent>
|
||||||
{
|
{
|
||||||
private const string DefaultCulture = "en";
|
private const string DefaultCulture = "en";
|
||||||
|
private static readonly Regex TokenRegex = new Regex(@"(?:\{)(?<token>[a-z0-9]+)(?:\})",
|
||||||
|
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
|
||||||
|
|
||||||
private readonly ICached<Dictionary<string, string>> _cache;
|
private readonly ICached<Dictionary<string, string>> _cache;
|
||||||
|
|
||||||
|
@ -52,22 +56,17 @@ namespace NzbDrone.Core.Localization
|
||||||
|
|
||||||
public string GetLocalizedString(string phrase)
|
public string GetLocalizedString(string phrase)
|
||||||
{
|
{
|
||||||
var language = GetSetLanguageFileName();
|
return GetLocalizedString(phrase, new Dictionary<string, object>());
|
||||||
|
|
||||||
return GetLocalizedString(phrase, language);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetLocalizedString(string phrase, string language)
|
public string GetLocalizedString(string phrase, Dictionary<string, object> tokens)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(phrase))
|
if (string.IsNullOrEmpty(phrase))
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(phrase));
|
throw new ArgumentNullException(nameof(phrase));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (language.IsNullOrWhiteSpace())
|
var language = GetSetLanguageFileName();
|
||||||
{
|
|
||||||
language = GetSetLanguageFileName();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (language == null)
|
if (language == null)
|
||||||
{
|
{
|
||||||
|
@ -78,12 +77,26 @@ namespace NzbDrone.Core.Localization
|
||||||
|
|
||||||
if (dictionary.TryGetValue(phrase, out var value))
|
if (dictionary.TryGetValue(phrase, out var value))
|
||||||
{
|
{
|
||||||
return value;
|
return ReplaceTokens(value, tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
return phrase;
|
return phrase;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string ReplaceTokens(string input, Dictionary<string, object> tokens)
|
||||||
|
{
|
||||||
|
tokens.TryAdd("appName", "Lidarr");
|
||||||
|
|
||||||
|
return TokenRegex.Replace(input, (match) =>
|
||||||
|
{
|
||||||
|
var tokenName = match.Groups["token"].Value;
|
||||||
|
|
||||||
|
tokens.TryGetValue(tokenName, out var token);
|
||||||
|
|
||||||
|
return token?.ToString() ?? $"{{{tokenName}}}";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private string GetSetLanguageFileName()
|
private string GetSetLanguageFileName()
|
||||||
{
|
{
|
||||||
var isoLanguage = IsoLanguages.Get((Language)_configService.UILanguage) ?? IsoLanguages.Get(Language.English);
|
var isoLanguage = IsoLanguages.Get((Language)_configService.UILanguage) ?? IsoLanguages.Get(Language.English);
|
||||||
|
|
Loading…
Reference in New Issue