Fix Localization Serialization

pull/3770/head
Qstick 2 years ago
parent 9f1ec2a2c3
commit 937a8518ad

@ -4,6 +4,7 @@ interface CssExports {
'deleteButton': string;
'hideMetadataProfile': string;
'labelIcon': string;
'message': string;
}
export const cssExports: CssExports;
export default cssExports;

@ -7,7 +7,7 @@ function getTranslations() {
dataType: 'json',
url: '/localization',
success: function(data) {
localization = data.Strings;
localization = data.strings;
}
};
@ -19,12 +19,13 @@ function getTranslations() {
const translations = getTranslations();
export default function translate(key, args = '') {
const translation = translations[key] || key;
if (args) {
const translatedKey = translate(key);
return translatedKey.replace(/\{(\d+)\}/g, (match, index) => {
return translation.replace(/\{(\d+)\}/g, (match, index) => {
return args[index];
});
}
return translations[key] || key;
return translation;
}

@ -1,7 +1,5 @@
using System.Text.Json;
using Lidarr.Http;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Localization;
namespace Lidarr.Api.V1.Localization
@ -10,21 +8,17 @@ namespace Lidarr.Api.V1.Localization
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]
[Produces("application/json")]
public string GetLocalizationDictionary()
public LocalizationResource GetLocalizationDictionary()
{
return JsonSerializer.Serialize(_localizationService.GetLocalizationDictionary().ToResource(), _serializerSettings);
return _localizationService.GetLocalizationDictionary().ToResource();
}
}
}

@ -1,10 +1,36 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Lidarr.Http.REST;
namespace Lidarr.Api.V1.Localization
{
public class LocalizationResourceSerializer : JsonConverter<Dictionary<string, string>>
{
public override Dictionary<string, string> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, Dictionary<string, string> dictionary, JsonSerializerOptions options)
{
writer.WriteStartObject();
foreach (var (key, value) in dictionary)
{
var propertyName = key;
writer.WritePropertyName(propertyName);
writer.WriteStringValue(value);
}
writer.WriteEndObject();
}
}
public class LocalizationResource : RestResource
{
[JsonConverter(typeof(LocalizationResourceSerializer))]
public Dictionary<string, string> Strings { get; set; }
}

Loading…
Cancel
Save