Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #121 from ErikApption/collect-missing-tojson
Browse files Browse the repository at this point in the history
prevent empty dictionary from being serialized
  • Loading branch information
AlexTeixeira authored Jan 3, 2022
2 parents 26590d8 + e855b09 commit 674281a
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Askmethat.Aspnet.JsonLocalizer.Localizer
{
internal class JsonStringLocalizer : JsonStringLocalizerBase, IJsonStringLocalizer, IDisposable
{

#if NETCORE
private readonly IWebHostEnvironment _env;

Expand Down Expand Up @@ -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}";
Expand Down Expand Up @@ -236,7 +237,7 @@ public MarkupString GetHtmlBlazorString(string name, bool shouldTryDefaultCultur
{
return new MarkupString(GetString(name, shouldTryDefaultCulture));
}

private void InitJsonFromCulture(CultureInfo cultureInfo)
{
InitJsonStringLocalizer(cultureInfo);
Expand Down Expand Up @@ -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)}");
}
Expand Down

0 comments on commit 674281a

Please sign in to comment.