From 4cb63834b23e3bfb3440ed60446506a6540ca9f9 Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 14 Dec 2021 16:47:29 -0500 Subject: [PATCH] prevent empty dictionary from being serialized --- .../Localizer/JsonStringLocalizer.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizer.cs b/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizer.cs index 7a5c142..f502523 100644 --- a/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizer.cs +++ b/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizer.cs @@ -17,7 +17,7 @@ namespace Askmethat.Aspnet.JsonLocalizer.Localizer { internal class JsonStringLocalizer : JsonStringLocalizerBase, IJsonStringLocalizer, IDisposable { - + #if NETCORE private readonly IWebHostEnvironment _env; @@ -120,13 +120,14 @@ public LocalizedString GetPlural(string name, double count, params object[] argu string format = name; - if(localization != null) { + if (localization != null) + { // try get the localization for the specified rule if (localization.TryGetValue(nameWithRule, out LocalizatedFormat localizedValue)) { format = localizedValue.Value; } - else + else { // if no translation was found for that rule, try with the "Other" rule. var nameWithOtherRule = $"{name}.{PluralizationConstants.Other}"; @@ -236,7 +237,7 @@ public MarkupString GetHtmlBlazorString(string name, bool shouldTryDefaultCultur { return new MarkupString(GetString(name, shouldTryDefaultCulture)); } - + private void InitJsonFromCulture(CultureInfo cultureInfo) { InitJsonStringLocalizer(cultureInfo); @@ -302,15 +303,16 @@ protected virtual void Dispose(bool disposing) { if (disposing) { - if (!string.IsNullOrWhiteSpace(_missingTranslations)) + if (!string.IsNullOrWhiteSpace(_missingTranslations) && (_missingJsonValues?.Count ?? 0) > 0) { try { // save missing values var json = JsonConvert.SerializeObject(_missingJsonValues); - Console.Error.WriteLine($"Writing missing translations to {Path.GetFullPath(_missingTranslations)}"); + Console.Error.WriteLine($"Writing {_missingJsonValues?.Count} missing translations to {Path.GetFullPath(_missingTranslations)}"); File.WriteAllText(_missingTranslations, json); - } catch (Exception) + } + catch (Exception) { Console.Error.WriteLine($"Cannot write missing translations to {Path.GetFullPath(_missingTranslations)}"); }