parent
99ccaab6a6
commit
729a876fc7
@ -0,0 +1,48 @@
|
||||
import createFetchHandler from 'Store/Actions/Creators/createFetchHandler';
|
||||
import { createThunk } from 'Store/thunks';
|
||||
|
||||
//
|
||||
// Variables
|
||||
|
||||
const section = 'settings.languages';
|
||||
|
||||
//
|
||||
// Actions Types
|
||||
|
||||
export const FETCH_LANGUAGES = 'settings/languages/fetchLanguages';
|
||||
|
||||
//
|
||||
// Action Creators
|
||||
|
||||
export const fetchLanguages = createThunk(FETCH_LANGUAGES);
|
||||
|
||||
//
|
||||
// Details
|
||||
|
||||
export default {
|
||||
|
||||
//
|
||||
// State
|
||||
|
||||
defaultState: {
|
||||
isFetching: false,
|
||||
isPopulated: false,
|
||||
error: null,
|
||||
items: []
|
||||
},
|
||||
|
||||
//
|
||||
// Action Handlers
|
||||
|
||||
actionHandlers: {
|
||||
[FETCH_LANGUAGES]: createFetchHandler(section, '/language')
|
||||
},
|
||||
|
||||
//
|
||||
// Reducers
|
||||
|
||||
reducers: {
|
||||
|
||||
}
|
||||
|
||||
};
|
@ -0,0 +1,34 @@
|
||||
import $ from 'jquery';
|
||||
|
||||
function getTranslations() {
|
||||
let localization = null;
|
||||
const ajaxOptions = {
|
||||
async: false,
|
||||
type: 'GET',
|
||||
global: false,
|
||||
dataType: 'json',
|
||||
url: `${window.Lidarr.apiRoot}/localization`,
|
||||
success: function(data) {
|
||||
localization = data.Strings;
|
||||
}
|
||||
};
|
||||
|
||||
ajaxOptions.headers = ajaxOptions.headers || {};
|
||||
ajaxOptions.headers['X-Api-Key'] = window.Lidarr.apiKey;
|
||||
|
||||
$.ajax(ajaxOptions);
|
||||
return localization;
|
||||
}
|
||||
|
||||
const translations = getTranslations();
|
||||
|
||||
export default function translate(key, args = '') {
|
||||
if (args) {
|
||||
const translatedKey = translate(key);
|
||||
return translatedKey.replace(/\{(\d+)\}/g, (match, index) => {
|
||||
return args[index];
|
||||
});
|
||||
}
|
||||
|
||||
return translations[key] || key;
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Lidarr.Http;
|
||||
using Lidarr.Http.REST;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NzbDrone.Core.Languages;
|
||||
|
||||
namespace Lidarr.Api.V1.Languages
|
||||
{
|
||||
[V1ApiController]
|
||||
public class LanguageController : RestController<LanguageResource>
|
||||
{
|
||||
public override LanguageResource GetResourceById(int id)
|
||||
{
|
||||
var language = (Language)id;
|
||||
|
||||
return new LanguageResource
|
||||
{
|
||||
Id = (int)language,
|
||||
Name = language.ToString()
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public List<LanguageResource> GetAll()
|
||||
{
|
||||
return Language.All.Select(l => new LanguageResource
|
||||
{
|
||||
Id = (int)l,
|
||||
Name = l.ToString()
|
||||
})
|
||||
.OrderBy(l => l.Name)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using Lidarr.Http.REST;
|
||||
|
||||
namespace Lidarr.Api.V1.Languages
|
||||
{
|
||||
public class LanguageResource : RestResource
|
||||
{
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
|
||||
public new int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string NameLower => Name.ToLowerInvariant();
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
using System.Text.Json;
|
||||
using Lidarr.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Localization;
|
||||
|
||||
namespace Lidarr.Api.V1.Localization
|
||||
{
|
||||
[V1ApiController]
|
||||
public class LocalizationController : Controller
|
||||
{
|
||||
private readonly ILocalizationService _localizationService;
|
||||
private readonly JsonSerializerOptions _serializerSettings;
|
||||
|
||||
public LocalizationController(ILocalizationService localizationService)
|
||||
{
|
||||
_localizationService = localizationService;
|
||||
_serializerSettings = STJson.GetSerializerSettings();
|
||||
_serializerSettings.DictionaryKeyPolicy = null;
|
||||
_serializerSettings.PropertyNamingPolicy = null;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public string GetLocalizationDictionary()
|
||||
{
|
||||
return JsonSerializer.Serialize(_localizationService.GetLocalizationDictionary().ToResource(), _serializerSettings);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
using System.Collections.Generic;
|
||||
using Lidarr.Http.REST;
|
||||
|
||||
namespace Lidarr.Api.V1.Localization
|
||||
{
|
||||
public class LocalizationResource : RestResource
|
||||
{
|
||||
public Dictionary<string, string> Strings { get; set; }
|
||||
}
|
||||
|
||||
public static class LocalizationResourceMapper
|
||||
{
|
||||
public static LocalizationResource ToResource(this Dictionary<string, string> localization)
|
||||
{
|
||||
if (localization == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new LocalizationResource
|
||||
{
|
||||
Strings = localization,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,191 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NzbDrone.Core.Datastore;
|
||||
|
||||
namespace NzbDrone.Core.Languages
|
||||
{
|
||||
public class Language : IEmbeddedDocument, IEquatable<Language>
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
|
||||
public Language()
|
||||
{
|
||||
}
|
||||
|
||||
private Language(int id, string name)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Id.GetHashCode();
|
||||
}
|
||||
|
||||
public bool Equals(Language other)
|
||||
{
|
||||
if (ReferenceEquals(null, other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(this, other))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return Id.Equals(other.Id);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(this, obj))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return Equals(obj as Language);
|
||||
}
|
||||
|
||||
public static bool operator ==(Language left, Language right)
|
||||
{
|
||||
return Equals(left, right);
|
||||
}
|
||||
|
||||
public static bool operator !=(Language left, Language right)
|
||||
{
|
||||
return !Equals(left, right);
|
||||
}
|
||||
|
||||
public static Language Unknown => new Language(0, "Unknown");
|
||||
public static Language English => new Language(1, "English");
|
||||
public static Language French => new Language(2, "French");
|
||||
public static Language Spanish => new Language(3, "Spanish");
|
||||
public static Language German => new Language(4, "German");
|
||||
public static Language Italian => new Language(5, "Italian");
|
||||
public static Language Danish => new Language(6, "Danish");
|
||||
public static Language Dutch => new Language(7, "Dutch");
|
||||
public static Language Japanese => new Language(8, "Japanese");
|
||||
public static Language Icelandic => new Language(9, "Icelandic");
|
||||
public static Language Chinese => new Language(10, "Chinese");
|
||||
public static Language Russian => new Language(11, "Russian");
|
||||
public static Language Polish => new Language(12, "Polish");
|
||||
public static Language Vietnamese => new Language(13, "Vietnamese");
|
||||
public static Language Swedish => new Language(14, "Swedish");
|
||||
public static Language Norwegian => new Language(15, "Norwegian");
|
||||
public static Language Finnish => new Language(16, "Finnish");
|
||||
public static Language Turkish => new Language(17, "Turkish");
|
||||
public static Language Portuguese => new Language(18, "Portuguese");
|
||||
public static Language Flemish => new Language(19, "Flemish");
|
||||
public static Language Greek => new Language(20, "Greek");
|
||||
public static Language Korean => new Language(21, "Korean");
|
||||
public static Language Hungarian => new Language(22, "Hungarian");
|
||||
public static Language Hebrew => new Language(23, "Hebrew");
|
||||
public static Language Lithuanian => new Language(24, "Lithuanian");
|
||||
public static Language Czech => new Language(25, "Czech");
|
||||
public static Language Hindi => new Language(26, "Hindi");
|
||||
public static Language Romanian => new Language(27, "Romanian");
|
||||
public static Language Thai => new Language(28, "Thai");
|
||||
public static Language Bulgarian => new Language(29, "Bulgarian");
|
||||
public static Language PortugueseBR => new Language(30, "Portuguese (Brazil)");
|
||||
public static Language Arabic => new Language(31, "Arabic");
|
||||
public static Language Any => new Language(-1, "Any");
|
||||
public static Language Original => new Language(-2, "Original");
|
||||
|
||||
public static List<Language> All
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<Language>
|
||||
{
|
||||
Unknown,
|
||||
English,
|
||||
French,
|
||||
Spanish,
|
||||
German,
|
||||
Italian,
|
||||
Danish,
|
||||
Dutch,
|
||||
Japanese,
|
||||
Icelandic,
|
||||
Chinese,
|
||||
Russian,
|
||||
Polish,
|
||||
Vietnamese,
|
||||
Swedish,
|
||||
Norwegian,
|
||||
Finnish,
|
||||
Turkish,
|
||||
Portuguese,
|
||||
Flemish,
|
||||
Greek,
|
||||
Korean,
|
||||
Hungarian,
|
||||
Hebrew,
|
||||
Lithuanian,
|
||||
Czech,
|
||||
Romanian,
|
||||
Hindi,
|
||||
Thai,
|
||||
Bulgarian,
|
||||
PortugueseBR,
|
||||
Arabic,
|
||||
Any,
|
||||
Original
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static Language FindById(int id)
|
||||
{
|
||||
if (id == 0)
|
||||
{
|
||||
return Unknown;
|
||||
}
|
||||
|
||||
Language language = All.FirstOrDefault(v => v.Id == id);
|
||||
|
||||
if (language == null)
|
||||
{
|
||||
throw new ArgumentException("ID does not match a known language", nameof(id));
|
||||
}
|
||||
|
||||
return language;
|
||||
}
|
||||
|
||||
public static explicit operator Language(int id)
|
||||
{
|
||||
return FindById(id);
|
||||
}
|
||||
|
||||
public static explicit operator int(Language language)
|
||||
{
|
||||
return language.Id;
|
||||
}
|
||||
|
||||
public static explicit operator Language(string lang)
|
||||
{
|
||||
var language = All.FirstOrDefault(v => v.Name.Equals(lang, StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
if (language == null)
|
||||
{
|
||||
throw new ArgumentException("Language does not match a known language", nameof(lang));
|
||||
}
|
||||
|
||||
return language;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace NzbDrone.Core.Languages
|
||||
{
|
||||
public static class LanguageExtensions
|
||||
{
|
||||
public static string ToExtendedString(this IEnumerable<Language> languages)
|
||||
{
|
||||
return string.Join(", ", languages.Select(l => l.ToString()));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
using NzbDrone.Core.Annotations;
|
||||
|
||||
namespace NzbDrone.Core.Languages
|
||||
{
|
||||
public class LanguageFieldConverter
|
||||
{
|
||||
public List<FieldSelectOption> GetSelectOptions()
|
||||
{
|
||||
return Language.All.ConvertAll(v => new FieldSelectOption { Value = v.Id, Name = v.Name });
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace NzbDrone.Core.Languages
|
||||
{
|
||||
public class LanguagesComparer : IComparer<List<Language>>
|
||||
{
|
||||
public int Compare(List<Language> x, List<Language> y)
|
||||
{
|
||||
if (!x.Any() && !y.Any())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!x.Any() && y.Any())
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (x.Any() && !y.Any())
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (x.Count > 1 && y.Count > 1 && x.Count > y.Count)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (x.Count > 1 && y.Count > 1 && x.Count < y.Count)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (x.Count > 1 && y.Count == 1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (x.Count == 1 && y.Count > 1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (x.Count == 1 && y.Count == 1)
|
||||
{
|
||||
return x.First().Name.CompareTo(y.First().Name);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NzbDrone.Core.Annotations;
|
||||
|
||||
namespace NzbDrone.Core.Languages
|
||||
{
|
||||
public class RealLanguageFieldConverter
|
||||
{
|
||||
public List<FieldSelectOption> GetSelectOptions()
|
||||
{
|
||||
return Language.All
|
||||
.Where(l => l != Language.Unknown && l != Language.Any)
|
||||
.ToList()
|
||||
.ConvertAll(v => new FieldSelectOption { Value = v.Id, Name = v.Name });
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
{
|
||||
"Language": "Language",
|
||||
"UILanguage": "UI Language"
|
||||
}
|
@ -0,0 +1,188 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Cache;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Configuration;
|
||||
using NzbDrone.Core.Configuration.Events;
|
||||
using NzbDrone.Core.Languages;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
using NzbDrone.Core.Parser;
|
||||
|
||||
namespace NzbDrone.Core.Localization
|
||||
{
|
||||
public interface ILocalizationService
|
||||
{
|
||||
Dictionary<string, string> GetLocalizationDictionary();
|
||||
string GetLocalizedString(string phrase);
|
||||
string GetLocalizedString(string phrase, string language);
|
||||
}
|
||||
|
||||
public class LocalizationService : ILocalizationService, IHandleAsync<ConfigSavedEvent>
|
||||
{
|
||||
private const string DefaultCulture = "en";
|
||||
|
||||
private readonly ICached<Dictionary<string, string>> _cache;
|
||||
|
||||
private readonly IConfigService _configService;
|
||||
private readonly IAppFolderInfo _appFolderInfo;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public LocalizationService(IConfigService configService,
|
||||
IAppFolderInfo appFolderInfo,
|
||||
ICacheManager cacheManager,
|
||||
Logger logger)
|
||||
{
|
||||
_configService = configService;
|
||||
_appFolderInfo = appFolderInfo;
|
||||
_cache = cacheManager.GetCache<Dictionary<string, string>>(typeof(Dictionary<string, string>), "localization");
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Dictionary<string, string> GetLocalizationDictionary()
|
||||
{
|
||||
var language = GetSetLanguageFileName();
|
||||
|
||||
return GetLocalizationDictionary(language);
|
||||
}
|
||||
|
||||
public string GetLocalizedString(string phrase)
|
||||
{
|
||||
var language = GetSetLanguageFileName();
|
||||
|
||||
return GetLocalizedString(phrase, language);
|
||||
}
|
||||
|
||||
public string GetLocalizedString(string phrase, string language)
|
||||
{
|
||||
if (string.IsNullOrEmpty(phrase))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(phrase));
|
||||
}
|
||||
|
||||
if (language.IsNullOrWhiteSpace())
|
||||
{
|
||||
language = GetSetLanguageFileName();
|
||||
}
|
||||
|
||||
if (language == null)
|
||||
{
|
||||
language = DefaultCulture;
|
||||
}
|
||||
|
||||
var dictionary = GetLocalizationDictionary(language);
|
||||
|
||||
if (dictionary.TryGetValue(phrase, out var value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
return phrase;
|
||||
}
|
||||
|
||||
private string GetSetLanguageFileName()
|
||||
{
|
||||
var isoLanguage = IsoLanguages.Get((Language)_configService.UILanguage);
|
||||
var language = isoLanguage.TwoLetterCode;
|
||||
|
||||
if (isoLanguage.CountryCode.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
language = string.Format("{0}_{1}", language, isoLanguage.CountryCode);
|
||||
}
|
||||
|
||||
return language;
|
||||
}
|
||||
|
||||
private Dictionary<string, string> GetLocalizationDictionary(string language)
|
||||
{
|
||||
if (string.IsNullOrEmpty(language))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(language));
|
||||
}
|
||||
|
||||
var startupFolder = _appFolderInfo.StartUpFolder;
|
||||
|
||||
var prefix = Path.Combine(startupFolder, "Localization", "Core");
|
||||
var key = prefix + language;
|
||||
|
||||
return _cache.Get("localization", () => GetDictionary(prefix, language, DefaultCulture + ".json").GetAwaiter().GetResult());
|
||||
}
|
||||
|
||||
private async Task<Dictionary<string, string>> GetDictionary(string prefix, string culture, string baseFilename)
|
||||
{
|
||||
if (string.IsNullOrEmpty(culture))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(culture));
|
||||
}
|
||||
|
||||
var dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var baseFilenamePath = Path.Combine(prefix, baseFilename);
|
||||
|
||||
var alternativeFilenamePath = Path.Combine(prefix, GetResourceFilename(culture));
|
||||
|
||||
await CopyInto(dictionary, baseFilenamePath).ConfigureAwait(false);
|
||||
|
||||
if (culture.Contains("_"))
|
||||
{
|
||||
var languageBaseFilenamePath = Path.Combine(prefix, GetResourceFilename(culture.Split('_')[0]));
|
||||
await CopyInto(dictionary, languageBaseFilenamePath).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await CopyInto(dictionary, alternativeFilenamePath).ConfigureAwait(false);
|
||||
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
private async Task CopyInto(IDictionary<string, string> dictionary, string resourcePath)
|
||||
{
|
||||
if (!File.Exists(resourcePath))
|
||||
{
|
||||
_logger.Error("Missing translation/culture resource: {0}", resourcePath);
|
||||
return;
|
||||
}
|
||||
|
||||
using (var fs = File.OpenRead(resourcePath))
|
||||
{
|
||||
if (fs != null)
|
||||
{
|
||||
var dict = await JsonSerializer.DeserializeAsync<Dictionary<string, string>>(fs);
|
||||
|
||||
foreach (var key in dict.Keys)
|
||||
{
|
||||
dictionary[key] = dict[key];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Error("Missing translation/culture resource: {0}", resourcePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetResourceFilename(string culture)
|
||||
{
|
||||
var parts = culture.Split('_');
|
||||
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
culture = parts[0].ToLowerInvariant() + "_" + parts[1].ToUpperInvariant();
|
||||
}
|
||||
else
|
||||
{
|
||||
culture = culture.ToLowerInvariant();
|
||||
}
|
||||
|
||||
return culture + ".json";
|
||||
}
|
||||
|
||||
public void HandleAsync(ConfigSavedEvent message)
|
||||
{
|
||||
_cache.Clear();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,14 +1,22 @@
|
||||
using NzbDrone.Core.Languages;
|
||||
|
||||
namespace NzbDrone.Core.Parser
|
||||
{
|
||||
public class IsoLanguage
|
||||
{
|
||||
public string TwoLetterCode { get; set; }
|
||||
public string ThreeLetterCode { get; set; }
|
||||
public string CountryCode { get; set; }
|
||||
public string EnglishName { get; set; }
|
||||
public Language Language { get; set; }
|
||||
|
||||
public IsoLanguage(string twoLetterCode, string threeLetterCode)
|
||||
public IsoLanguage(string twoLetterCode, string countryCode, string threeLetterCode, string englishName, Language language)
|
||||
{
|
||||
TwoLetterCode = twoLetterCode;
|
||||
ThreeLetterCode = threeLetterCode;
|
||||
CountryCode = countryCode;
|
||||
EnglishName = englishName;
|
||||
Language = language;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue