Browse Source

Support JSON Array.

pull/23701/head
maliming 5 months ago
parent
commit
1f0358311d
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 85
      framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs
  2. 17
      framework/test/Volo.Abp.Localization.Tests/Volo/Abp/Localization/AbpLocalization_Tests.cs
  3. 17
      framework/test/Volo.Abp.Localization.Tests/Volo/Abp/Localization/TestResources/SourceExt/en.json

85
framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using Microsoft.Extensions.Localization;
@ -61,10 +62,7 @@ public static class JsonLocalizationDictionaryBuilder
var dictionary = new Dictionary<string, LocalizedString>();
var dublicateNames = new List<string>();
// Flache Struktur in Dictionary umwandeln
var flatTexts = FlattenTexts(jsonFile.Texts);
foreach (var item in flatTexts)
foreach (var item in FlattenTexts(jsonFile.Texts))
{
if (string.IsNullOrEmpty(item.Key))
{
@ -90,44 +88,63 @@ public static class JsonLocalizationDictionaryBuilder
private static Dictionary<string, string> FlattenTexts(Dictionary<string, object> texts, string prefix = "")
{
var result = new Dictionary<string, string>();
foreach (var item in texts)
foreach (var text in texts)
{
var currentKey = string.IsNullOrEmpty(prefix) ? item.Key : $"{prefix}__{item.Key}";
if (item.Value is JsonElement jsonElement)
var currentKey = string.IsNullOrEmpty(prefix) ? text.Key : $"{prefix}__{text.Key}";
switch (text.Value)
{
if (jsonElement.ValueKind == JsonValueKind.String)
{
result[currentKey] = jsonElement.GetString() ?? "";
}
else if (jsonElement.ValueKind == JsonValueKind.Object)
case JsonElement jsonElement:
foreach (var item in FlattenJsonElement(jsonElement, currentKey))
{
result[item.Key] = item.Value;
}
break;
case string str:
result[currentKey] = str;
break;
case null:
result[currentKey] = "";
break;
default:
result[currentKey] = text.Value.ToString() ?? "";
break;
}
}
return result;
}
private static IEnumerable<KeyValuePair<string, string>> FlattenJsonElement(JsonElement element, string prefix)
{
switch (element.ValueKind)
{
case JsonValueKind.String:
yield return new KeyValuePair<string, string>(prefix, element.GetString() ?? "");
break;
case JsonValueKind.Object:
foreach (var prop in element.EnumerateObject())
{
var nestedDict = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonElement.GetRawText());
if (nestedDict != null)
var newKey = $"{prefix}__{prop.Name}";
foreach (var item in FlattenJsonElement(prop.Value, newKey))
{
var flattenedNested = FlattenTexts(nestedDict, currentKey);
foreach (var nested in flattenedNested)
{
result[nested.Key] = nested.Value;
}
yield return item;
}
}
}
else if (item.Value is string stringValue)
{
result[currentKey] = stringValue;
}
else if (item.Value is Dictionary<string, object> nestedDict)
{
var flattenedNested = FlattenTexts(nestedDict, currentKey);
foreach (var nested in flattenedNested)
break;
case JsonValueKind.Array:
var i = 0;
foreach (var prop in element.EnumerateArray())
{
result[nested.Key] = nested.Value;
var newKey = $"{prefix}__{i}";
foreach (var item in FlattenJsonElement(prop, newKey))
{
yield return item;
}
i++;
}
}
break;
default:
yield return new KeyValuePair<string, string>(prefix, element.ToString());
break;
}
return result;
}
}

17
framework/test/Volo.Abp.Localization.Tests/Volo/Abp/Localization/AbpLocalization_Tests.cs

@ -196,7 +196,7 @@ public class AbpLocalization_Tests : AbpIntegratedTest<AbpLocalizationTestModule
{
_localizer["CarPlural"].Value.ShouldBe("汽车");
}
using (CultureHelper.Use(CultureInfo.GetCultureInfo("zh-Hans-CN")))
{
_localizer["Car"].Value.ShouldBe("汽车");
@ -214,7 +214,7 @@ public class AbpLocalization_Tests : AbpIntegratedTest<AbpLocalizationTestModule
{
_localizer["CarPlural"].Value.ShouldBe("汽車");
}
using (CultureHelper.Use(CultureInfo.GetCultureInfo("zh-TW")))
{
_localizer["Car"].Value.ShouldBe("汽車");
@ -223,7 +223,7 @@ public class AbpLocalization_Tests : AbpIntegratedTest<AbpLocalizationTestModule
{
_localizer["CarPlural"].Value.ShouldBe("汽車");
}
using (CultureHelper.Use(CultureInfo.GetCultureInfo("zh-Hant-TW")))
{
_localizer["Car"].Value.ShouldBe("汽車");
@ -376,9 +376,6 @@ public class AbpLocalization_Tests : AbpIntegratedTest<AbpLocalizationTestModule
externalLocalizer["Car"].Value.ShouldBe("Car");
}
/// <summary>
/// <see href="https://github.com/abpframework/abp/issues/18208"/>
/// </summary>
[Fact]
public void Should_Get_Nested_Translations()
{
@ -387,6 +384,12 @@ public class AbpLocalization_Tests : AbpIntegratedTest<AbpLocalizationTestModule
_localizer["MyNestedTranslation__SomeKey"].Value.ShouldBe("Some nested value");
_localizer["MyNestedTranslation__SomeOtherKey"].Value.ShouldBe("Some other nested value");
_localizer["MyNestedTranslation__DeeplyNested__DeepKey"].Value.ShouldBe("A deeply nested value");
_localizer["MyNestedTranslation__DeeplyNestedArray__0"].Value.ShouldBe("First value in array");
_localizer["MyNestedTranslation__DeeplyNestedArray__1"].Value.ShouldBe("Second value in array");
_localizer["MyNestedTranslation__DeeplyNestedArray__2__InnerDeepKey"].Value.ShouldBe("Inner deeply nested value");
_localizer["MyNestedTranslation__DeeplyNestedArray__3__InnerDeepArray__0"].Value.ShouldBe("First inner deep array value");
_localizer["MyNestedTranslation__DeeplyNestedArray__3__InnerDeepArray__1"].Value.ShouldBe("Second inner deep array value");
}
}
}
}

17
framework/test/Volo.Abp.Localization.Tests/Volo/Abp/Localization/TestResources/SourceExt/en.json

@ -7,7 +7,20 @@
"SomeOtherKey": "Some other nested value",
"DeeplyNested": {
"DeepKey": "A deeply nested value"
}
},
"DeeplyNestedArray": [
"First value in array",
"Second value in array",
{
"InnerDeepKey": "Inner deeply nested value"
},
{
"InnerDeepArray": [
"First inner deep array value",
"Second inner deep array value"
]
}
]
}
}
}
}

Loading…
Cancel
Save