From aab98b55396bca0298b1c6839d1ae3c837f9601a Mon Sep 17 00:00:00 2001 From: Alexandre Teixeira Date: Sat, 4 May 2019 09:02:25 +0200 Subject: [PATCH 1/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ceefdab..aa2a732 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ services.AddJsonLocalization(options => { ### Current Options -- **SupportedCultureInfos** : _Default value : _List containing only default culture_. Array of cultures that you should provide to plugin. _(Like RequestLocalizationOptions) +- **SupportedCultureInfos** : _Default value : _List containing only default culture_ and CurrentUICulture. Optionnal array of cultures that you should provide to plugin. _(Like RequestLocalizationOptions) - **ResourcesPath** : _Default value : `$"{_env.WebRootPath}/Resources/"`_. Base path of your resources. The plugin will browse the folder and sub-folders and load all present JSON files. - **CacheDuration** : _Default value : 30 minutes_. Cache all values to memory to avoid loading files for each request, - **FileEncoding** : _default value : UTF8_. Specify the file encoding. From 53712eab0a2fcf19b433ac33945d22fe14e02792 Mon Sep 17 00:00:00 2001 From: Alexandre Teixeira Date: Sat, 4 May 2019 09:06:19 +0200 Subject: [PATCH 2/8] Update README.md --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index aa2a732..0a1f08b 100644 --- a/README.md +++ b/README.md @@ -168,7 +168,13 @@ Intel Core i7-5557U CPU 3.10GHz (Broadwell), 1 CPU, 4 logical and 2 physical cor # Contributors -[@lethek](https://github.com/lethek) : PRs : [#20](https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/pull/20), [#17](https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/pull/17) +[@lethek](https://github.com/lethek) : +- [#20](https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/pull/20) +- [#17](https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/pull/17) + +[@lugospod](https://github.com/lugospod) : +- [#43](https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/pull/43) +- [#44](https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/pull/44) # License From d6dee17f81e4c25dd23b53be6ea7813e2eba5eaa Mon Sep 17 00:00:00 2001 From: Alexandre Teixeira Date: Tue, 14 May 2019 12:24:41 +0200 Subject: [PATCH 3/8] merge only code from fork #52 --- .../Askmethat.Aspnet.JsonLocalizer.csproj | 3 +- .../Localizer/JsonStringLocalizer.cs | 6 +- .../Localizer/JsonStringLocalizerBase.cs | 9 +- .../Localizer/JsonStringLocalizerFactory.cs | 6 +- README.md | 78 +++------------ ...that.Aspnet.JsonLocalizer.Benchmark.csproj | 3 +- .../Program.cs | 5 +- ...Askmethat.Aspnet.JsonLocalizer.Test.csproj | 7 +- .../Localizer/CustomLocalizerJsonFileTest.cs | 53 ---------- .../Localizer/JsonStringLocalizerTest.cs | 51 +--------- .../Localizer/PluralizationJsonTest.cs | 98 +++++++++++++++++++ .../Resources/localization.json | 18 ---- .../pluralization/localization.json | 20 ++++ ...hat.Aspnet.JsonLocalizer.TestSample.csproj | 14 +-- 14 files changed, 154 insertions(+), 217 deletions(-) delete mode 100644 test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/CustomLocalizerJsonFileTest.cs create mode 100644 test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/PluralizationJsonTest.cs create mode 100644 test/Askmethat.Aspnet.JsonLocalizer.Test/pluralization/localization.json diff --git a/Askmethat.Aspnet.JsonLocalizer/Askmethat.Aspnet.JsonLocalizer.csproj b/Askmethat.Aspnet.JsonLocalizer/Askmethat.Aspnet.JsonLocalizer.csproj index 4ea8d17..4c54b98 100644 --- a/Askmethat.Aspnet.JsonLocalizer/Askmethat.Aspnet.JsonLocalizer.csproj +++ b/Askmethat.Aspnet.JsonLocalizer/Askmethat.Aspnet.JsonLocalizer.csproj @@ -18,10 +18,11 @@ Git - + + diff --git a/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizer.cs b/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizer.cs index 7219f1c..c8c84a6 100644 --- a/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizer.cs +++ b/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizer.cs @@ -20,7 +20,7 @@ internal class JsonStringLocalizer : JsonStringLocalizerBase, IStringLocalizer public JsonStringLocalizer(IOptions localizationOptions, IHostingEnvironment env, string baseName = null) : base(localizationOptions, baseName) { _env = env; - _resourcesRelativePath = GetJsonRelativePath(_localizationOptions.Value.ResourcesPath); + resourcesRelativePath = GetJsonRelativePath(_localizationOptions.Value.ResourcesPath); InitJsonStringLocalizer(); } @@ -54,7 +54,7 @@ private string GetPluralLocalization(string name, string format, object[] argume { bool isPlural = (bool)last; value = GetString(name); - if (value.Contains(_localizationOptions.Value.PluralSeparator)) + if (!string.IsNullOrEmpty(value) && value.Contains(_localizationOptions.Value.PluralSeparator)) { int index = (isPlural ? 1 : 0); value = value.Split(_localizationOptions.Value.PluralSeparator)[index]; @@ -128,7 +128,7 @@ string GetString(string name, bool shouldTryDefaultCulture = true) //advert user that current name string does not //contains any translation - Console.Error.WriteLine($"{name} does not contains any translation"); + Console.Error.WriteLine($"{name} does not contain any translation"); return null; } diff --git a/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerBase.cs b/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerBase.cs index ff382c2..2d11b63 100644 --- a/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerBase.cs +++ b/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerBase.cs @@ -13,15 +13,16 @@ namespace Askmethat.Aspnet.JsonLocalizer.Localizer { internal class JsonStringLocalizerBase { - protected Dictionary localization; protected readonly IMemoryCache _memCache; protected readonly IOptions _localizationOptions; - protected string _resourcesRelativePath; protected readonly string _baseName; - protected readonly TimeSpan _memCacheDuration; protected const string CACHE_KEY = "LocalizationBlob"; + + protected string resourcesRelativePath; protected string currentCulture = string.Empty; + protected Dictionary localization; + public JsonStringLocalizerBase(IOptions localizationOptions, string baseName = null) { _baseName = TransformBaseNameToPath(baseName); @@ -80,7 +81,7 @@ protected void InitJsonStringLocalizer(CultureInfo currentCulture) //Look for cache key. if (!_memCache.TryGetValue(GetCacheKey(currentCulture), out localization)) { - ConstructLocalizationObject(_resourcesRelativePath, currentCulture); + ConstructLocalizationObject(resourcesRelativePath, currentCulture); // Set cache options. MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions() // Keep in cache for this time, reset time if accessed. diff --git a/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerFactory.cs b/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerFactory.cs index 94cf088..8be6287 100644 --- a/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerFactory.cs +++ b/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerFactory.cs @@ -19,12 +19,8 @@ public JsonStringLocalizerFactory( IHostingEnvironment env, IOptions localizationOptions = null) { - if (localizationOptions == null) - { - throw new ArgumentNullException(nameof(localizationOptions)); - } _env = env; - _localizationOptions = localizationOptions; + _localizationOptions = localizationOptions ?? throw new ArgumentNullException(nameof(localizationOptions)); } diff --git a/README.md b/README.md index 0a1f08b..5305ad7 100644 --- a/README.md +++ b/README.md @@ -10,46 +10,14 @@ Json Localizer library for .NetStandard and .NetCore Asp.net projects # Project -This library allow user to use JSON files instead of RESX in Asp.net application. -The code try to be most compliante with Microsoft guidelines. -The library is compatible with NetStandard & NetCore +This library allows users to use JSON files instead of RESX in an ASP.NET application. +The code tries to be most compliant with Microsoft guidelines. +The library is compatible with NetStandard & NetCore. # Configuration An extension method is available for `IServiceCollection`. -You can have a look to this method [here](https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/blob/development/Askmethat.Aspnet.JsonLocalizer/Extensions/JsonLocalizerServiceExtension.cs) - -# Breaking Changes - -For performance purpose, JSON structure was changes since 2.0.0 from List to Dictionnary. -Here the detail for version before 1.1.7 and version after 2.0.0 - -## 1.1.7- - -``` json -[ - { - "Key": "Name3", - "Values": { - "en-US": "My Name 3", - "fr-FR": "Mon Nom 3" - } - } -] -``` - -## 2.0.0+ - -``` json -{ - "Name3": { - "Values": { - "en-US": "My Name 3", - "fr-FR": "Mon Nom 3" - } - } -} -``` +You can have a look at the method [here](https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/blob/development/Askmethat.Aspnet.JsonLocalizer/Extensions/JsonLocalizerServiceExtension.cs) ## Options @@ -61,7 +29,7 @@ services.AddJsonLocalization(options => { options.CacheDuration = TimeSpan.FromMinutes(15); options.ResourcesPath = "mypath"; options.FileEncoding = Encoding.GetEncoding("ISO-8859-1"); - options.SupportedCultureInfos = new[] + options.SupportedCultureInfos = new HashSet() { new CultureInfo("en-US"), new CultureInfo("fr-FR") @@ -83,7 +51,7 @@ services.AddJsonLocalization(options => { #Pluralization In version 2.0.0, Pluralization was introduced. -You are now able to manage a singular (left) and plural (rigth) version for the same Key. +You are now able to manage a singular (left) and plural (right) version for the same Key. *PluralSeparator* is used as separator between the two strings. For example : User|Users for key Users @@ -99,12 +67,6 @@ Pluralization is available with IStringLocalizer, IViewLocalizer and HtmlStringL **Platform Support** -## 1.1.7 - -|Platform|Version| -| ------------------- | :------------------: | -|NetStandard|1.1.6+| -|NetCore|2.0.0+| ## 2.0.0+ @@ -115,36 +77,15 @@ Pluralization is available with IStringLocalizer, IViewLocalizer and HtmlStringL **WithCulture method** -**WhithCulture** method is not implemented and will be not implemented. ASP.NET Team, start to set this method **Obsolete** fr version 3 and will be removed in version 4 of asp.net core. +**WhithCulture** method is not implemented and will not be implemented. ASP.NET Team, start to set this method **Obsolete** for version 3 and will be removed in version 4 of asp.net core. For more information : https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/issues/46 # Performances -After talking with others Devs about my package, they ask my about performance. +After talking with others Devs about my package, they asked my about performance. -So I added a benchmark project and here the last results with some modification, that will be available with 1.1.7 - -## 1.1.7 - -``` ini - -BenchmarkDotNet=v0.11.3, OS=macOS Mojave 10.14 (18A391) [Darwin 18.0.0] -Intel Core i7-5557U CPU 3.10GHz (Broadwell), 1 CPU, 4 logical and 2 physical cores -.NET Core SDK=2.2.100 - [Host] : .NET Core 2.2.0 (CoreCLR 4.6.27110.04, CoreFX 4.6.27110.04), 64bit RyuJIT [AttachedDebugger] - DefaultJob : .NET Core 2.2.0 (CoreCLR 4.6.27110.04, CoreFX 4.6.27110.04), 64bit RyuJIT - - -``` -| Method | Mean | Error | StdDev | Min | Max | Ratio | Gen 0/1k Op | Gen 1/1k Op | Gen 2/1k Op | Allocated Memory/Op | -|-------------- |---------:|----------:|----------:|---------:|---------:|------:|------------:|------------:|------------:|--------------------:| -| JsonLocalizer | 255.1 ns | 0.7950 ns | 0.7048 ns | 253.3 ns | 256.4 ns | 2.18 | 0.0648 | - | - | 136 B | -| Localizer | 117.2 ns | 0.2544 ns | 0.2255 ns | 116.8 ns | 117.5 ns | 1.00 | - | - | - | - | - - -## 2.0.0+ ``` ini @@ -176,6 +117,9 @@ Intel Core i7-5557U CPU 3.10GHz (Broadwell), 1 CPU, 4 logical and 2 physical cor - [#43](https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/pull/43) - [#44](https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/pull/44) +[@Compufreak345](https://github.com/Compufreak345) : +- [#52](https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/issues/52) + # License [MIT Licence](https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/blob/master/LICENSE) diff --git a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark.csproj b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark.csproj index 8a9506a..66a1db4 100644 --- a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark.csproj +++ b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark.csproj @@ -1,8 +1,9 @@ - + Exe netcoreapp2.2 + false diff --git a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/Program.cs b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/Program.cs index 5a33d74..a4ab2d7 100644 --- a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/Program.cs +++ b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/Program.cs @@ -39,7 +39,6 @@ public class BenchmarkJSONLocalizer IMemoryCache _cach = new MemoryCache(Options.Create(new MemoryCacheOptions() {})); IMemoryCache _cach2 = new MemoryCache(Options.Create(new MemoryCacheOptions() { })); - private const int N = 10000; IStringLocalizer _jsonLocalizer; public BenchmarkJSONLocalizer() @@ -109,9 +108,9 @@ public string LocalizerDefaultCultureValue() } - class Program + internal class Program { - static void Main(string[] args) + private static void Main(string[] args) { BenchmarkRunner.Run(); } diff --git a/test/Askmethat.Aspnet.JsonLocalizer.Test/Askmethat.Aspnet.JsonLocalizer.Test.csproj b/test/Askmethat.Aspnet.JsonLocalizer.Test/Askmethat.Aspnet.JsonLocalizer.Test.csproj index 31120bc..691420e 100644 --- a/test/Askmethat.Aspnet.JsonLocalizer.Test/Askmethat.Aspnet.JsonLocalizer.Test.csproj +++ b/test/Askmethat.Aspnet.JsonLocalizer.Test/Askmethat.Aspnet.JsonLocalizer.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp2.2; + netcoreapp2.2 false @@ -9,7 +9,7 @@ - + @@ -38,6 +38,9 @@ Always + + Always + Always diff --git a/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/CustomLocalizerJsonFileTest.cs b/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/CustomLocalizerJsonFileTest.cs deleted file mode 100644 index a0609ef..0000000 --- a/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/CustomLocalizerJsonFileTest.cs +++ /dev/null @@ -1,53 +0,0 @@ -using Askmethat.Aspnet.JsonLocalizer.Extensions; -using Askmethat.Aspnet.JsonLocalizer.Test.Helpers; -using Microsoft.Extensions.Localization; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Globalization; - -namespace Askmethat.Aspnet.JsonLocalizer.Test.Localizer -{ - [TestClass] - public class CustomLocalizerJsonFileTest - { - - [TestMethod] - public void Should_Be_Singular_Users_Custom() - { - // Arrange - CultureInfo.CurrentUICulture = new CultureInfo("fr-FR"); - var localizer = JsonStringLocalizerHelperFactory.Create(new JsonLocalizationOptions() - { - DefaultCulture = new CultureInfo("en-US"), - SupportedCultureInfos = new System.Collections.Generic.HashSet() - { - new CultureInfo("fr-FR") - }, - PluralSeparator = '#' - }); - - var result = localizer.GetString("CustomPluralUser", false); - - Assert.AreEqual("Utilisateur", result); - } - - [TestMethod] - public void Should_Be_Plural_Users_Custom() - { - // Arrange - CultureInfo.CurrentUICulture = new CultureInfo("fr-FR"); - var localizer = JsonStringLocalizerHelperFactory.Create(new JsonLocalizationOptions() - { - DefaultCulture = new CultureInfo("en-US"), - SupportedCultureInfos = new System.Collections.Generic.HashSet() - { - new CultureInfo("fr-FR") - }, - PluralSeparator = '#' - }); - - var result = localizer.GetString("CustomPluralUser", true); - - Assert.AreEqual("Utilisateurs", result); - } - } -} \ No newline at end of file diff --git a/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/JsonStringLocalizerTest.cs b/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/JsonStringLocalizerTest.cs index d8fe559..c911173 100644 --- a/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/JsonStringLocalizerTest.cs +++ b/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/JsonStringLocalizerTest.cs @@ -126,52 +126,6 @@ public void Should_Read_CaseInsensitive_UseDefault() Assert.AreEqual("US English", result); } - [TestMethod] - public void Should_Be_Singular_Users() - { - CultureInfo.CurrentUICulture = new CultureInfo("fr-FR"); - // Arrange - var localizer = JsonStringLocalizerHelperFactory.Create(new JsonLocalizationOptions() - { - DefaultCulture = new CultureInfo("fr-FR") - }); - - var result = localizer.GetString("PluralUser", false); - - Assert.AreEqual("Utilisateur", result); - } - - [TestMethod] - public void Should_Be_Plural_Users() - { - CultureInfo.CurrentUICulture = new CultureInfo("fr-FR"); - // Arrange - var localizer = JsonStringLocalizerHelperFactory.Create(new JsonLocalizationOptions() - { - DefaultCulture = new CultureInfo("fr-FR") - }); - - var result = localizer.GetString("PluralUser", true); - - Assert.AreEqual("Utilisateurs", result); - } - - [TestMethod] - public void Should_Be_PluralWithNoSeperator_ShowDefault() - { - CultureInfo.CurrentUICulture = new CultureInfo("fr-FR"); - - // Arrange - var localizer = JsonStringLocalizerHelperFactory.Create(new JsonLocalizationOptions() - { - DefaultCulture = new CultureInfo("fr-FR") - }); - - var result = localizer.GetString("PluralUserFailed", true); - - Assert.AreEqual("Utilisateurs", result); - } - [TestMethod] public void Should_GetAllStrings_ByCaseInsensitiveCultureName() { @@ -185,10 +139,7 @@ public void Should_GetAllStrings_ByCaseInsensitiveCultureName() var expected = new[] { "Mon Nom de Base 1", "Mon Nom de Base 2", - "French", - "Utilisateur|Utilisateurs", - "Utilisateurs", - "Utilisateur#Utilisateurs" + "French" }; var results = localizer.GetAllStrings().Select(x => x.Value).ToArray(); CollectionAssert.AreEquivalent(expected, results); diff --git a/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/PluralizationJsonTest.cs b/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/PluralizationJsonTest.cs new file mode 100644 index 0000000..24fa8e7 --- /dev/null +++ b/test/Askmethat.Aspnet.JsonLocalizer.Test/Localizer/PluralizationJsonTest.cs @@ -0,0 +1,98 @@ +using Askmethat.Aspnet.JsonLocalizer.Extensions; +using Askmethat.Aspnet.JsonLocalizer.Localizer; +using Askmethat.Aspnet.JsonLocalizer.Test.Helpers; +using Microsoft.Extensions.Localization; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Globalization; + +namespace Askmethat.Aspnet.JsonLocalizer.Test.Localizer +{ + + + [TestClass] + public class PluralizationJsonTest + { + JsonStringLocalizer localizer = null; + public void InitLocalizer(char seperator = '|') + { + CultureInfo.CurrentUICulture = new CultureInfo("fr-FR"); + + localizer = JsonStringLocalizerHelperFactory.Create(new JsonLocalizationOptions() + { + DefaultCulture = new CultureInfo("en-US"), + SupportedCultureInfos = new System.Collections.Generic.HashSet() + { + new CultureInfo("fr-FR"), + }, + ResourcesPath = "pluralization", + PluralSeparator = seperator + }); + } + + + [TestMethod] + public void Should_Be_Singular_Users() + { + // Arrange + InitLocalizer(); + + var result = localizer.GetString("PluralUser", false); + + Assert.AreEqual("Utilisateur", result); + } + + [TestMethod] + public void Should_Be_Plural_Users() + { + InitLocalizer(); + + var result = localizer.GetString("PluralUser", true); + + Assert.AreEqual("Utilisateurs", result); + } + + [TestMethod] + public void Should_Be_PluralWithNoSeperator_ShowDefault() + { + InitLocalizer(); + + var result = localizer.GetString("PluralUserFailed", true); + + Assert.AreEqual("Utilisateurs", result); + } + + [TestMethod] + public void Should_Be_Singular_Users_Custom() + { + // Arrange + InitLocalizer('#'); + + var result = localizer.GetString("CustomPluralUser", false); + + Assert.AreEqual("Utilisateur", result); + } + + [TestMethod] + public void Should_Be_Plural_Users_Custom() + { + // Arrange + InitLocalizer('#'); + + var result = localizer.GetString("CustomPluralUser", true); + + Assert.AreEqual("Utilisateurs", result); + } + + [TestMethod] + public void Should_Be_Plural_NotFound() + { + // Arrange + InitLocalizer(); + + var result = localizer.GetString("NotFound", true); + + Assert.AreEqual("NotFound", result); + } + + } +} diff --git a/test/Askmethat.Aspnet.JsonLocalizer.Test/Resources/localization.json b/test/Askmethat.Aspnet.JsonLocalizer.Test/Resources/localization.json index f870dba..a1f1d0c 100644 --- a/test/Askmethat.Aspnet.JsonLocalizer.Test/Resources/localization.json +++ b/test/Askmethat.Aspnet.JsonLocalizer.Test/Resources/localization.json @@ -21,23 +21,5 @@ "en-us": "US English", "FR-FR": "French" } - }, - "PluralUser": { - "Values": { - "en-US": "User|Users", - "fr-FR": "Utilisateur|Utilisateurs" - } - }, - "PluralUserFailed": { - "Values": { - "en-US": "Users", - "fr-FR": "Utilisateurs" - } - }, - "CustomPluralUser": { - "Values": { - "en-US": "User#Users", - "fr-FR": "Utilisateur#Utilisateurs" - } } } \ No newline at end of file diff --git a/test/Askmethat.Aspnet.JsonLocalizer.Test/pluralization/localization.json b/test/Askmethat.Aspnet.JsonLocalizer.Test/pluralization/localization.json new file mode 100644 index 0000000..140e45b --- /dev/null +++ b/test/Askmethat.Aspnet.JsonLocalizer.Test/pluralization/localization.json @@ -0,0 +1,20 @@ +{ + "PluralUser": { + "Values": { + "en-US": "User|Users", + "fr-FR": "Utilisateur|Utilisateurs" + } + }, + "PluralUserFailed": { + "Values": { + "en-US": "Users", + "fr-FR": "Utilisateurs" + } + }, + "CustomPluralUser": { + "Values": { + "en-US": "User#Users", + "fr-FR": "Utilisateur#Utilisateurs" + } + } +} \ No newline at end of file diff --git a/test/Askmethat.Aspnet.JsonLocalizer.TestSample/Askmethat.Aspnet.JsonLocalizer.TestSample.csproj b/test/Askmethat.Aspnet.JsonLocalizer.TestSample/Askmethat.Aspnet.JsonLocalizer.TestSample.csproj index 9928bf5..6f65e53 100644 --- a/test/Askmethat.Aspnet.JsonLocalizer.TestSample/Askmethat.Aspnet.JsonLocalizer.TestSample.csproj +++ b/test/Askmethat.Aspnet.JsonLocalizer.TestSample/Askmethat.Aspnet.JsonLocalizer.TestSample.csproj @@ -1,7 +1,7 @@  - netcoreapp2.0;netcoreapp2.1; + netcoreapp2.2 2.1.0 @@ -16,15 +16,9 @@ - - - - - - - - - + + + From 85ba2f1b02222db079abd80782ee1ca7962b6c4a Mon Sep 17 00:00:00 2001 From: Alexandre Teixeira Date: Tue, 14 May 2019 12:31:07 +0200 Subject: [PATCH 4/8] merge code salve 2 #52 need to run benchmark --- .../Localizer/JsonStringLocalizerBase.cs | 18 ++++++++-- ...that.Aspnet.JsonLocalizer.Benchmark.csproj | 1 - ...k.BenchmarkJSONLocalizer-report-default.md | 34 ++++++++++++------- ...rk.BenchmarkJSONLocalizer-report-github.md | 34 ++++++++++++------- ...enchmark.BenchmarkJSONLocalizer-report.csv | 14 ++++---- ...nchmark.BenchmarkJSONLocalizer-report.html | 26 +++++++------- .../Program.cs | 25 ++------------ .../Startup.cs | 2 -- .../json/Views/Home/Index/localization.json | 8 ++--- 9 files changed, 83 insertions(+), 79 deletions(-) diff --git a/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerBase.cs b/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerBase.cs index 2d11b63..4ba3094 100644 --- a/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerBase.cs +++ b/Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerBase.cs @@ -17,8 +17,8 @@ internal class JsonStringLocalizerBase protected readonly IOptions _localizationOptions; protected readonly string _baseName; protected readonly TimeSpan _memCacheDuration; - protected const string CACHE_KEY = "LocalizationBlob"; + protected const string CACHE_KEY = "LocalizationBlob"; protected string resourcesRelativePath; protected string currentCulture = string.Empty; protected Dictionary localization; @@ -32,6 +32,15 @@ public JsonStringLocalizerBase(IOptions localizationOpt } string GetCacheKey(CultureInfo ci) => $"{CACHE_KEY}_{ci.DisplayName}"; + + //string GetCacheKey(CultureInfo ci) + //{ + // if (_localizationOptions.Value.UseBaseName) + // { + // return $"{CACHE_KEY}_{ci.DisplayName}_{_baseName}"; + // } + // return $"{CACHE_KEY}_{ci.DisplayName}"; + //} void SetCurrentCultureToCache(CultureInfo ci) => currentCulture = ci.Name; protected bool IsUICultureCurrentCulture(CultureInfo ci) { return string.Equals(currentCulture, ci.Name, StringComparison.InvariantCultureIgnoreCase); @@ -104,9 +113,12 @@ void ConstructLocalizationObject(string jsonPath, CultureInfo currentCulture) localization = new Dictionary(); } - string pattern = string.IsNullOrWhiteSpace(_baseName) ? "*.json" : $"{_baseName}/*.json"; + string basePath = string.IsNullOrWhiteSpace(_baseName) ? jsonPath : Path.Combine(jsonPath, _baseName); + if (!Directory.Exists(basePath)) return; + string pattern = "*.json"; + //get all files ending by json extension - string[] myFiles = Directory.GetFiles(jsonPath, pattern, SearchOption.AllDirectories); + string[] myFiles = Directory.GetFiles(basePath, pattern, SearchOption.AllDirectories); foreach (string file in myFiles) { diff --git a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark.csproj b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark.csproj index 66a1db4..2e1dd77 100644 --- a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark.csproj +++ b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark.csproj @@ -8,7 +8,6 @@ - diff --git a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report-default.md b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report-default.md index a3ba897..79454b4 100644 --- a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report-default.md +++ b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report-default.md @@ -1,16 +1,24 @@ -BenchmarkDotNet=v0.11.5, OS=macOS Mojave 10.14.4 (18E226) [Darwin 18.5.0] -Intel Core i7-5557U CPU 3.10GHz (Broadwell), 1 CPU, 4 logical and 2 physical cores -.NET Core SDK=2.2.106 - [Host] : .NET Core 2.2.4 (CoreCLR 4.6.27521.02, CoreFX 4.6.27521.01), 64bit RyuJIT - DefaultJob : .NET Core 2.2.4 (CoreCLR 4.6.27521.02, CoreFX 4.6.27521.01), 64bit RyuJIT +BenchmarkDotNet=v0.11.5, OS=Windows 10.0.17134.706 (1803/April2018Update/Redstone4) +Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores +Frequency=3328128 Hz, Resolution=300.4692 ns, Timer=TSC +.NET Core SDK=2.2.103 + [Host] : .NET Core 2.2.1 (CoreCLR 4.6.27207.03, CoreFX 4.6.27207.03), 64bit RyuJIT - Method | Mean | Error | StdDev | Min | Max | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated | ------------------------------------------------- |--------------:|--------------:|--------------:|--------------:|--------------:|---------:|--------:|--------:|--------:|-------:|----------:| - Localizer | 109.70 ns | 0.1010 ns | 0.0789 ns | 109.60 ns | 109.89 ns | 1.00 | 0.00 | - | - | - | - | - JsonLocalizer | 80.34 ns | 0.2115 ns | 0.1875 ns | 79.99 ns | 80.70 ns | 0.73 | 0.00 | 0.0228 | - | - | 48 B | - JsonLocalizerWithCreation | 510,920.91 ns | 2,157.3470 ns | 1,912.4319 ns | 507,912.26 ns | 514,646.87 ns | 4,659.68 | 16.18 | 83.0078 | 27.3438 | 4.8828 | 175576 B | - JsonLocalizerWithCreationAndExternalMemoryCache | 4,581.21 ns | 12.6355 ns | 11.2011 ns | 4,562.24 ns | 4,605.28 ns | 41.75 | 0.11 | 1.6174 | 0.8087 | - | 3408 B | - JsonLocalizerDefaultCultureValue | 322.22 ns | 0.9659 ns | 0.8563 ns | 320.33 ns | 323.33 ns | 2.94 | 0.01 | 0.1793 | - | - | 376 B | - LocalizerDefaultCultureValue | 362.96 ns | 1.8632 ns | 1.5558 ns | 361.14 ns | 365.80 ns | 3.31 | 0.01 | 0.1559 | - | - | 328 B | + Method | Mean | Error | Min | Max | Ratio | RatioSD | +------------------------------------------------ |-----:|------:|----:|----:|------:|--------:| + Localizer | NA | NA | NA | NA | ? | ? | + JsonLocalizer | NA | NA | NA | NA | ? | ? | + JsonLocalizerWithCreation | NA | NA | NA | NA | ? | ? | + JsonLocalizerWithCreationAndExternalMemoryCache | NA | NA | NA | NA | ? | ? | + JsonLocalizerDefaultCultureValue | NA | NA | NA | NA | ? | ? | + LocalizerDefaultCultureValue | NA | NA | NA | NA | ? | ? | + +Benchmarks with issues: + BenchmarkJSONLocalizer.Localizer: DefaultJob + BenchmarkJSONLocalizer.JsonLocalizer: DefaultJob + BenchmarkJSONLocalizer.JsonLocalizerWithCreation: DefaultJob + BenchmarkJSONLocalizer.JsonLocalizerWithCreationAndExternalMemoryCache: DefaultJob + BenchmarkJSONLocalizer.JsonLocalizerDefaultCultureValue: DefaultJob + BenchmarkJSONLocalizer.LocalizerDefaultCultureValue: DefaultJob diff --git a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report-github.md b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report-github.md index 65a778f..2ff68f7 100644 --- a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report-github.md +++ b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report-github.md @@ -1,18 +1,26 @@ ``` ini -BenchmarkDotNet=v0.11.5, OS=macOS Mojave 10.14.4 (18E226) [Darwin 18.5.0] -Intel Core i7-5557U CPU 3.10GHz (Broadwell), 1 CPU, 4 logical and 2 physical cores -.NET Core SDK=2.2.106 - [Host] : .NET Core 2.2.4 (CoreCLR 4.6.27521.02, CoreFX 4.6.27521.01), 64bit RyuJIT - DefaultJob : .NET Core 2.2.4 (CoreCLR 4.6.27521.02, CoreFX 4.6.27521.01), 64bit RyuJIT +BenchmarkDotNet=v0.11.5, OS=Windows 10.0.17134.706 (1803/April2018Update/Redstone4) +Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores +Frequency=3328128 Hz, Resolution=300.4692 ns, Timer=TSC +.NET Core SDK=2.2.103 + [Host] : .NET Core 2.2.1 (CoreCLR 4.6.27207.03, CoreFX 4.6.27207.03), 64bit RyuJIT ``` -| Method | Mean | Error | StdDev | Min | Max | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated | -|------------------------------------------------ |--------------:|--------------:|--------------:|--------------:|--------------:|---------:|--------:|--------:|--------:|-------:|----------:| -| Localizer | 109.70 ns | 0.1010 ns | 0.0789 ns | 109.60 ns | 109.89 ns | 1.00 | 0.00 | - | - | - | - | -| JsonLocalizer | 80.34 ns | 0.2115 ns | 0.1875 ns | 79.99 ns | 80.70 ns | 0.73 | 0.00 | 0.0228 | - | - | 48 B | -| JsonLocalizerWithCreation | 510,920.91 ns | 2,157.3470 ns | 1,912.4319 ns | 507,912.26 ns | 514,646.87 ns | 4,659.68 | 16.18 | 83.0078 | 27.3438 | 4.8828 | 175576 B | -| JsonLocalizerWithCreationAndExternalMemoryCache | 4,581.21 ns | 12.6355 ns | 11.2011 ns | 4,562.24 ns | 4,605.28 ns | 41.75 | 0.11 | 1.6174 | 0.8087 | - | 3408 B | -| JsonLocalizerDefaultCultureValue | 322.22 ns | 0.9659 ns | 0.8563 ns | 320.33 ns | 323.33 ns | 2.94 | 0.01 | 0.1793 | - | - | 376 B | -| LocalizerDefaultCultureValue | 362.96 ns | 1.8632 ns | 1.5558 ns | 361.14 ns | 365.80 ns | 3.31 | 0.01 | 0.1559 | - | - | 328 B | +| Method | Mean | Error | Min | Max | Ratio | RatioSD | +|------------------------------------------------ |-----:|------:|----:|----:|------:|--------:| +| Localizer | NA | NA | NA | NA | ? | ? | +| JsonLocalizer | NA | NA | NA | NA | ? | ? | +| JsonLocalizerWithCreation | NA | NA | NA | NA | ? | ? | +| JsonLocalizerWithCreationAndExternalMemoryCache | NA | NA | NA | NA | ? | ? | +| JsonLocalizerDefaultCultureValue | NA | NA | NA | NA | ? | ? | +| LocalizerDefaultCultureValue | NA | NA | NA | NA | ? | ? | + +Benchmarks with issues: + BenchmarkJSONLocalizer.Localizer: DefaultJob + BenchmarkJSONLocalizer.JsonLocalizer: DefaultJob + BenchmarkJSONLocalizer.JsonLocalizerWithCreation: DefaultJob + BenchmarkJSONLocalizer.JsonLocalizerWithCreationAndExternalMemoryCache: DefaultJob + BenchmarkJSONLocalizer.JsonLocalizerDefaultCultureValue: DefaultJob + BenchmarkJSONLocalizer.LocalizerDefaultCultureValue: DefaultJob diff --git a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report.csv b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report.csv index 8563c64..4e3f2de 100644 --- a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report.csv +++ b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report.csv @@ -1,7 +1,7 @@ -Method Job AnalyzeLaunchVariance EvaluateOverhead MaxAbsoluteError MaxRelativeError MinInvokeCount MinIterationTime OutlierMode Affinity EnvironmentVariables Jit Platform Runtime AllowVeryLargeObjects Concurrent CpuGroups Force HeapAffinitizeMask HeapCount NoAffinitize RetainVm Server PowerPlan Arguments BuildConfiguration Clock EngineFactory NuGetReferences Toolchain IsMutator InvocationCount IterationCount IterationTime LaunchCount MaxIterationCount MaxWarmupIterationCount MinIterationCount MinWarmupIterationCount RunStrategy UnrollFactor WarmupCount Mean Error StdDev Min Max Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated -Localizer Default False Default Default Default Default Default Default 0000 Empty RyuJit X64 Core False True False True Default Default False False False Default Default Default Default Default Default Default Default 1 Default Default Default Default Default Default Default Default 16 Default 109.70 ns 0.1010 ns 0.0789 ns 109.60 ns 109.89 ns 1.00 0.00 - - - - -JsonLocalizer Default False Default Default Default Default Default Default 0000 Empty RyuJit X64 Core False True False True Default Default False False False Default Default Default Default Default Default Default Default 1 Default Default Default Default Default Default Default Default 16 Default 80.34 ns 0.2115 ns 0.1875 ns 79.99 ns 80.70 ns 0.73 0.00 0.0228 - - 48 B -JsonLocalizerWithCreation Default False Default Default Default Default Default Default 0000 Empty RyuJit X64 Core False True False True Default Default False False False Default Default Default Default Default Default Default Default 1 Default Default Default Default Default Default Default Default 16 Default "510,920.91 ns" "2,157.3470 ns" "1,912.4319 ns" "507,912.26 ns" "514,646.87 ns" "4,659.68" 16.18 83.0078 27.3438 4.8828 175576 B -JsonLocalizerWithCreationAndExternalMemoryCache Default False Default Default Default Default Default Default 0000 Empty RyuJit X64 Core False True False True Default Default False False False Default Default Default Default Default Default Default Default 1 Default Default Default Default Default Default Default Default 16 Default "4,581.21 ns" 12.6355 ns 11.2011 ns "4,562.24 ns" "4,605.28 ns" 41.75 0.11 1.6174 0.8087 - 3408 B -JsonLocalizerDefaultCultureValue Default False Default Default Default Default Default Default 0000 Empty RyuJit X64 Core False True False True Default Default False False False Default Default Default Default Default Default Default Default 1 Default Default Default Default Default Default Default Default 16 Default 322.22 ns 0.9659 ns 0.8563 ns 320.33 ns 323.33 ns 2.94 0.01 0.1793 - - 376 B -LocalizerDefaultCultureValue Default False Default Default Default Default Default Default 0000 Empty RyuJit X64 Core False True False True Default Default False False False Default Default Default Default Default Default Default Default 1 Default Default Default Default Default Default Default Default 16 Default 362.96 ns 1.8632 ns 1.5558 ns 361.14 ns 365.80 ns 3.31 0.01 0.1559 - - 328 B +Method;Job;AnalyzeLaunchVariance;EvaluateOverhead;MaxAbsoluteError;MaxRelativeError;MinInvokeCount;MinIterationTime;OutlierMode;Affinity;EnvironmentVariables;Jit;Platform;Runtime;AllowVeryLargeObjects;Concurrent;CpuGroups;Force;HeapAffinitizeMask;HeapCount;NoAffinitize;RetainVm;Server;PowerPlan;Arguments;BuildConfiguration;Clock;EngineFactory;NuGetReferences;Toolchain;IsMutator;InvocationCount;IterationCount;IterationTime;LaunchCount;MaxIterationCount;MaxWarmupIterationCount;MinIterationCount;MinWarmupIterationCount;RunStrategy;UnrollFactor;WarmupCount;Mean;Error;Min;Max;Ratio;RatioSD +Localizer;Default;False;Default;Default;Default;Default;Default;Default;11111111;Empty;RyuJit;X64;Core;False;True;False;True;Default;Default;False;False;False;Default;Default;Default;Default;Default;Default;Default;Default;1;Default;Default;Default;Default;Default;Default;Default;Default;16;Default;NA;NA;NA;NA;?;? +JsonLocalizer;Default;False;Default;Default;Default;Default;Default;Default;11111111;Empty;RyuJit;X64;Core;False;True;False;True;Default;Default;False;False;False;Default;Default;Default;Default;Default;Default;Default;Default;1;Default;Default;Default;Default;Default;Default;Default;Default;16;Default;NA;NA;NA;NA;?;? +JsonLocalizerWithCreation;Default;False;Default;Default;Default;Default;Default;Default;11111111;Empty;RyuJit;X64;Core;False;True;False;True;Default;Default;False;False;False;Default;Default;Default;Default;Default;Default;Default;Default;1;Default;Default;Default;Default;Default;Default;Default;Default;16;Default;NA;NA;NA;NA;?;? +JsonLocalizerWithCreationAndExternalMemoryCache;Default;False;Default;Default;Default;Default;Default;Default;11111111;Empty;RyuJit;X64;Core;False;True;False;True;Default;Default;False;False;False;Default;Default;Default;Default;Default;Default;Default;Default;1;Default;Default;Default;Default;Default;Default;Default;Default;16;Default;NA;NA;NA;NA;?;? +JsonLocalizerDefaultCultureValue;Default;False;Default;Default;Default;Default;Default;Default;11111111;Empty;RyuJit;X64;Core;False;True;False;True;Default;Default;False;False;False;Default;Default;Default;Default;Default;Default;Default;Default;1;Default;Default;Default;Default;Default;Default;Default;Default;16;Default;NA;NA;NA;NA;?;? +LocalizerDefaultCultureValue;Default;False;Default;Default;Default;Default;Default;Default;11111111;Empty;RyuJit;X64;Core;False;True;False;True;Default;Default;False;False;False;Default;Default;Default;Default;Default;Default;Default;Default;1;Default;Default;Default;Default;Default;Default;Default;Default;16;Default;NA;NA;NA;NA;?;? diff --git a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report.html b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report.html index 95f8a29..3a12fc9 100644 --- a/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report.html +++ b/benchmark/Askmethat.Aspnet.JsonLocalizer.Benchmark/BenchmarkDotNet.Artifacts/results/Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-report.html @@ -2,7 +2,7 @@ -Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-20190502-084305 +Askmethat.Aspnet.JsonLocalizer.Benchmark.BenchmarkJSONLocalizer-20190514-122941