Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.0.0</Version>
<Version>9.0.0-beta01</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override LocalizedString this[string name]
{
string? ret = null;
var localizer = Utility.GetStringLocalizerFromService(Assembly, typeName);
if (localizer != null)
if (localizer != null && localizer is not JsonStringLocalizer)
{
ret = GetLocalizerValueFromCache(localizer, name);
}
Expand Down Expand Up @@ -177,7 +177,7 @@ public override IEnumerable<LocalizedString> GetAllStrings(bool includeParentCul
{
IEnumerable<LocalizedString>? ret = null;
var localizer = Utility.GetStringLocalizerFromService(Assembly, typeName);
if (localizer != null)
if (localizer != null && localizer is not JsonStringLocalizer)
{
ret = localizer.GetAllStrings(includeParentCultures);
}
Expand Down
51 changes: 51 additions & 0 deletions test/UnitTest/Localization/JsonStringLocalizerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
Expand Down Expand Up @@ -306,6 +307,25 @@ public void Validate_ResourceManagerStringLocalizerType()
Assert.Equal("Test", result[0].ErrorMessage);
}

[Fact]
public void ValidateFixEndlessLoop()
{
var sc = new ServiceCollection();
sc.AddConfiguration();
sc.AddBootstrapBlazor();
sc.AddSingleton<IStringLocalizerFactory, MockLocalizerFactory>();
sc.AddSingleton<IStringLocalizerFactory, MockLocalizerFactory2>();

var provider = sc.BuildServiceProvider();
var localizer = provider.GetRequiredService<IStringLocalizer<Foo>>();

Assert.Equal("姓名", localizer["Name"]);

var items = localizer.GetAllStrings(false);
Assert.Equal("姓名", items.First(i => i.Name == "Name").Value);
Assert.DoesNotContain("Test-JsonName", items.Select(i => i.Name));
}

private class MockTypeInfo : TypeDelegator
{
public override string? FullName => null;
Expand All @@ -318,6 +338,37 @@ private class MockLocalizerFactory : IStringLocalizerFactory
public IStringLocalizer Create(string baseName, string location) => new MockStringLocalizer();
}

private class MockLocalizerFactory2 : IStringLocalizerFactory
{
private readonly IServiceProvider _serviceProvider;

public MockLocalizerFactory2(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public IStringLocalizer Create(Type resourceSource)
{
var stringLocalizerFactorys = _serviceProvider.GetServices<IStringLocalizerFactory>();
IStringLocalizerFactory stringLocalizerFactory;
if (resourceSource == typeof(Foo))
{
stringLocalizerFactory = stringLocalizerFactorys.Single(s => s.GetType().Name == "JsonStringLocalizerFactory");
}
else
{
stringLocalizerFactory = _serviceProvider.GetServices<IStringLocalizerFactory>().Single(s => s is MockLocalizerFactory);
}
return stringLocalizerFactory.Create(resourceSource);
}

public IStringLocalizer Create(string baseName, string location)
{
var stringLocalizerFactory = _serviceProvider.GetServices<IStringLocalizerFactory>().Single(s => s is MockLocalizerFactory);
return stringLocalizerFactory.Create(baseName, location);
}
}

private class MockStringLocalizer : IStringLocalizer
{
public LocalizedString this[string name] => GetAllStrings(true).FirstOrDefault(l => l.Name == name) ?? new LocalizedString(name, name, true);
Expand Down