From 2662d0f3fb8031f93136768bd398bc392494d402 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Fri, 28 Oct 2022 11:07:27 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E7=BB=99=E9=94=99=E8=AE=B0=E5=BD=95=E6=97=A5=E5=BF=97=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Localization/Json/JsonStringLocalizer.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs b/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs index b295839ed..0e7588e98 100644 --- a/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs +++ b/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs @@ -74,8 +74,22 @@ internal class JsonStringLocalizer : ResourceManagerStringLocalizer get { var format = GetStringSafely(name); - var value = string.Format(CultureInfo.CurrentCulture, format ?? name, arguments); + var value = SafeFormat() ?? name; return new LocalizedString(name, value, resourceNotFound: format == null, searchedLocation: TypeName); + + string? SafeFormat() + { + string? ret = null; + try + { + ret = string.Format(CultureInfo.CurrentCulture, format ?? name, arguments); + } + catch (Exception ex) + { + Logger.LogError(ex, "{JsonStringLocalizerName} searched for '{Name}' in '{TypeName}' with culture '{CultureName}' throw exception.", nameof(JsonStringLocalizer), name, TypeName, CultureInfo.CurrentUICulture.Name); + } + return ret; + } } } @@ -161,7 +175,7 @@ internal class JsonStringLocalizer : ResourceManagerStringLocalizer { if (!IgnoreLocalizerMissing) { - Logger.LogInformation($"{nameof(JsonStringLocalizer)} searched for '{name}' in '{TypeName}' with culture '{CultureInfo.CurrentUICulture.Name}' not found."); + Logger.LogInformation("{JsonStringLocalizerName} searched for '{Name}' in '{TypeName}' with culture '{CultureName}' not found.", nameof(JsonStringLocalizer), name, TypeName, CultureInfo.CurrentUICulture.Name); } } -- Gitee From 789b8e9a048f64b1472ac7757f7b31fce8a7f3b2 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Fri, 28 Oct 2022 11:15:02 +0800 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0=E5=B8=A6?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E7=9A=84=E8=B5=84=E6=BA=90=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Localization/Json/JsonStringLocalizer.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs b/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs index 0e7588e98..55cf509ea 100644 --- a/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs +++ b/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs @@ -73,15 +73,15 @@ internal class JsonStringLocalizer : ResourceManagerStringLocalizer { get { - var format = GetStringSafely(name); - var value = SafeFormat() ?? name; - return new LocalizedString(name, value, resourceNotFound: format == null, searchedLocation: TypeName); + var value = SafeFormat(); + return new LocalizedString(name, value ?? name, resourceNotFound: value == null, searchedLocation: TypeName); string? SafeFormat() { string? ret = null; try { + var format = GetStringSafely(name); ret = string.Format(CultureInfo.CurrentCulture, format ?? name, arguments); } catch (Exception ex) -- Gitee From 9ebe9f2a8873ad2842ba8b0ab378ea7c7373d07c Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Fri, 28 Oct 2022 11:15:10 +0800 Subject: [PATCH 3/4] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Localization/JsonStringLocalizerTest.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/test/UnitTest/Localization/JsonStringLocalizerTest.cs b/test/UnitTest/Localization/JsonStringLocalizerTest.cs index d5526982f..5e3a1ba06 100644 --- a/test/UnitTest/Localization/JsonStringLocalizerTest.cs +++ b/test/UnitTest/Localization/JsonStringLocalizerTest.cs @@ -212,6 +212,22 @@ public class JsonStringLocalizerTest : BootstrapBlazorTestBase Assert.Equal("test-name", localizer["test-name"]); } + [Fact] + public void FormatException_Ok() + { + var sc = new ServiceCollection(); + sc.AddConfiguration(); + sc.AddSingleton(); + sc.AddTransient(); + sc.AddBootstrapBlazor(); + + var provider = sc.BuildServiceProvider(); + var localizer = provider.GetRequiredService>(); + var v = localizer["Mock-FakeAddress", "Test"]; + Assert.True(v.ResourceNotFound); + Assert.Equal("Mock-FakeAddress", v); + } + [Fact] public void GetResourcePrefix_Ok() { @@ -260,7 +276,8 @@ public class JsonStringLocalizerTest : BootstrapBlazorTestBase public IEnumerable GetAllStrings(bool includeParentCultures) => new List() { new LocalizedString("Mock-Name", "Mock-Test-Name"), - new LocalizedString("Mock-Address", "Mock-Test-Address-{0}") + new LocalizedString("Mock-Address", "Mock-Test-Address-{0}"), + new LocalizedString("Mock-FakeAddress", "Mock-Test-Address-{ 0}") }; } -- Gitee From 65e98e216003300a7702f546960672ae3ac4aad1 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Fri, 28 Oct 2022 11:17:57 +0800 Subject: [PATCH 4/4] chore: bump version 6.11.30-beta02 --- src/BootstrapBlazor/BootstrapBlazor.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index 96fb981e6..c3f0d24d4 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 6.11.29 + 6.11.30-beta02 -- Gitee