From 5f33e2d9a78ba51b87df370bb28e5719d7de4c51 Mon Sep 17 00:00:00 2001 From: Erik Date: Fri, 19 Nov 2021 16:10:30 -0500 Subject: [PATCH] added support to split i18n files into multiple json files to help structuring localization data into domains --- .../Modes/LocalizationI18NModeGenerator.cs | 9 ++++---- .../i18n/localization.en-US.json | 10 +------- .../i18n/localization.fr-FR.json | 10 +------- .../i18n/temperatures.en-US.json | 10 ++++++++ .../i18n/temperatures.fr-FR.json | 10 ++++++++ .../Localizer/I18nFallbackJsonFileTest.cs | 23 ++++++++++++++++++- .../i18nFallback/localizationFile2.en.json | 3 +++ .../i18nFallback/localizationFile2.fr.json | 3 +++ .../i18nFallback/localizationFile2.json | 3 +++ 9 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/temperatures.en-US.json create mode 100644 test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/temperatures.fr-FR.json create mode 100644 test/Askmethat.Aspnet.JsonLocalizer.Test/i18nFallback/localizationFile2.en.json create mode 100644 test/Askmethat.Aspnet.JsonLocalizer.Test/i18nFallback/localizationFile2.fr.json create mode 100644 test/Askmethat.Aspnet.JsonLocalizer.Test/i18nFallback/localizationFile2.json diff --git a/Askmethat.Aspnet.JsonLocalizer/Localizer/Modes/LocalizationI18NModeGenerator.cs b/Askmethat.Aspnet.JsonLocalizer/Localizer/Modes/LocalizationI18NModeGenerator.cs index ce99c92..c2c93dd 100644 --- a/Askmethat.Aspnet.JsonLocalizer/Localizer/Modes/LocalizationI18NModeGenerator.cs +++ b/Askmethat.Aspnet.JsonLocalizer/Localizer/Modes/LocalizationI18NModeGenerator.cs @@ -29,8 +29,8 @@ public ConcurrentDictionary ConstructLocalization(IEn _options = options; var enumerable = myFiles as string[] ?? myFiles.ToArray(); - var neutralFile = enumerable.FirstOrDefault(file => Path.GetFileName(file) - .Count(s => s.CompareTo('.') == 0) == 1); + var neutralFiles = enumerable.Where(file => Path.GetFileName(file) + .Count(s => s.CompareTo('.') == 0) == 1).ToList(); var isInvariantCulture = currentCulture.DisplayName == CultureInfo.InvariantCulture.ThreeLetterISOLanguageName; @@ -61,9 +61,10 @@ public ConcurrentDictionary ConstructLocalization(IEn } else { - if (neutralFile != null) + if (neutralFiles.Any()) { - AddValueToLocalization(options, neutralFile, true); + foreach (var neutralFile in neutralFiles) + AddValueToLocalization(options, neutralFile, true); } } diff --git a/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/localization.en-US.json b/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/localization.en-US.json index d980899..f5b13ae 100644 --- a/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/localization.en-US.json +++ b/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/localization.en-US.json @@ -8,16 +8,8 @@ "WeatherForecastDesc": "This component demonstrates fetching data from a service.", "Loading": "Loading...", "Date": "Date", - "TempC": "Temp. (C)", - "TempF": "Temp. (F)", "Summary": "Summary", "CurrentCount": "Current count", "ClickMe": "Click me", - "About": "About", - "Freezing": "Freezing", - "Bracing": "Bracing", - "Chilly": "Chilly", - "Cool": "Cool", - "Mild": "Mild", - "Warm": "Warm" + "About": "About" } \ No newline at end of file diff --git a/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/localization.fr-FR.json b/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/localization.fr-FR.json index 706c5c2..7780cfe 100644 --- a/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/localization.fr-FR.json +++ b/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/localization.fr-FR.json @@ -8,16 +8,8 @@ "WeatherForecastDesc": "Ce volet montre comment obtenir des données à partir d'un service.", "Loading": "Chargement...", "Date": "Date", - "TempC": "Temp. (C)", - "TempF": "Temp. (F)", "Summary": "Sommaire", "CurrentCount": "Valeur courante", "ClickMe": "Cliquer", - "About": "À propos de", - "Freezing": "Geler", - "Bracing": "Bracing", - "Chilly": "Froid", - "Cool": "Frais", - "Mild": "Doux", - "Warm": "Chaud" + "About": "À propos de" } \ No newline at end of file diff --git a/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/temperatures.en-US.json b/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/temperatures.en-US.json new file mode 100644 index 0000000..cbe882d --- /dev/null +++ b/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/temperatures.en-US.json @@ -0,0 +1,10 @@ +{ + "TempC": "Temp. (C)", + "TempF": "Temp. (F)", + "Freezing": "Freezing", + "Bracing": "Bracing", + "Chilly": "Chilly", + "Cool": "Cool", + "Mild": "Mild", + "Warm": "Warm" +} diff --git a/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/temperatures.fr-FR.json b/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/temperatures.fr-FR.json new file mode 100644 index 0000000..2bb914d --- /dev/null +++ b/test/Askmethat.Aspnet.JsonLocalizer.I18nTestSample/i18n/temperatures.fr-FR.json @@ -0,0 +1,10 @@ +{ + "TempC": "Temp. (C)", + "TempF": "Temp. (F)", + "Freezing": "Geler", + "Bracing": "Bracing", + "Chilly": "Froid", + "Cool": "Frais", + "Mild": "Doux", + "Warm": "Chaud" +} diff --git a/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/I18nFallbackJsonFileTest.cs b/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/I18nFallbackJsonFileTest.cs index 60ecba0..71c63f7 100644 --- a/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/I18nFallbackJsonFileTest.cs +++ b/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/I18nFallbackJsonFileTest.cs @@ -70,6 +70,26 @@ public void Should_Read_Color_FallbackToParent() } + [TestMethod] + public void Should_Read_Luminosity_FallbackToParent() + { + InitLocalizer("fr-FR"); + LocalizedString result = localizer.GetString("Luminosity"); + Assert.AreEqual("Luminosité", result); + Assert.IsFalse(result.ResourceNotFound); + + InitLocalizer("en-NZ"); + result = localizer.GetString("Luminosity"); + Assert.AreEqual("Luminosity", result); + Assert.IsFalse(result.ResourceNotFound); + + InitLocalizer("zh-CN"); + result = localizer.GetString("Luminosity"); + Assert.AreEqual("Luminosity", result); + Assert.IsFalse(result.ResourceNotFound); + + } + //[TestMethod] public void Should_Read_ResourceMissingCulture_FallbackToResourceName() { @@ -97,7 +117,8 @@ public void Should_Read_AllStringsWithParentFallback() LocalizedString[] results = localizer.GetAllStrings(includeParentCultures: true).ToArray(); LocalizedString[] expected = new[] { new LocalizedString("Color", "Colour (specific)", false), - new LocalizedString("Empty", "Empty", false) + new LocalizedString("Empty", "Empty", false), + new LocalizedString("Luminosity", "Luminosity", false) }; CollectionAssert.AreEqual(expected, results.OrderBy(s => s.Name).ToArray(), new LocalizedStringComparer()); } diff --git a/test/Askmethat.Aspnet.JsonLocalizer.Test/i18nFallback/localizationFile2.en.json b/test/Askmethat.Aspnet.JsonLocalizer.Test/i18nFallback/localizationFile2.en.json new file mode 100644 index 0000000..b659e6f --- /dev/null +++ b/test/Askmethat.Aspnet.JsonLocalizer.Test/i18nFallback/localizationFile2.en.json @@ -0,0 +1,3 @@ +{ + "Luminosity" : "Luminosity" +} \ No newline at end of file diff --git a/test/Askmethat.Aspnet.JsonLocalizer.Test/i18nFallback/localizationFile2.fr.json b/test/Askmethat.Aspnet.JsonLocalizer.Test/i18nFallback/localizationFile2.fr.json new file mode 100644 index 0000000..5fd12e6 --- /dev/null +++ b/test/Askmethat.Aspnet.JsonLocalizer.Test/i18nFallback/localizationFile2.fr.json @@ -0,0 +1,3 @@ +{ + "Luminosity": "Luminosité" +} \ No newline at end of file diff --git a/test/Askmethat.Aspnet.JsonLocalizer.Test/i18nFallback/localizationFile2.json b/test/Askmethat.Aspnet.JsonLocalizer.Test/i18nFallback/localizationFile2.json new file mode 100644 index 0000000..b659e6f --- /dev/null +++ b/test/Askmethat.Aspnet.JsonLocalizer.Test/i18nFallback/localizationFile2.json @@ -0,0 +1,3 @@ +{ + "Luminosity" : "Luminosity" +} \ No newline at end of file