From 1f0358311d1f1ddf7a5ed74cf0b28f64852049e7 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 11 Sep 2025 17:13:40 +0800 Subject: [PATCH] Support JSON Array. --- .../Json/JsonLocalizationDictionaryBuilder.cs | 85 +++++++++++-------- .../Abp/Localization/AbpLocalization_Tests.cs | 17 ++-- .../TestResources/SourceExt/en.json | 17 +++- 3 files changed, 76 insertions(+), 43 deletions(-) diff --git a/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs b/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs index c0af6a7af6..f065f0e695 100644 --- a/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs +++ b/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(); var dublicateNames = new List(); - // 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 FlattenTexts(Dictionary texts, string prefix = "") { var result = new Dictionary(); - - 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> FlattenJsonElement(JsonElement element, string prefix) + { + switch (element.ValueKind) + { + case JsonValueKind.String: + yield return new KeyValuePair(prefix, element.GetString() ?? ""); + break; + case JsonValueKind.Object: + foreach (var prop in element.EnumerateObject()) { - var nestedDict = JsonSerializer.Deserialize>(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 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(prefix, element.ToString()); + break; } - - return result; } } diff --git a/framework/test/Volo.Abp.Localization.Tests/Volo/Abp/Localization/AbpLocalization_Tests.cs b/framework/test/Volo.Abp.Localization.Tests/Volo/Abp/Localization/AbpLocalization_Tests.cs index b827c9b8e6..189c1b8614 100644 --- a/framework/test/Volo.Abp.Localization.Tests/Volo/Abp/Localization/AbpLocalization_Tests.cs +++ b/framework/test/Volo.Abp.Localization.Tests/Volo/Abp/Localization/AbpLocalization_Tests.cs @@ -196,7 +196,7 @@ public class AbpLocalization_Tests : AbpIntegratedTest - /// - /// [Fact] public void Should_Get_Nested_Translations() { @@ -387,6 +384,12 @@ public class AbpLocalization_Tests : AbpIntegratedTest