33// See the LICENSE file in the project root for more information.
44// Maintainer: Argo Zhang([email protected] ) Website: https://www.blazor.zone 55
6+ using Microsoft . Extensions . DependencyInjection ;
67using Microsoft . Extensions . Localization ;
78using System . ComponentModel . DataAnnotations ;
89using System . Reflection ;
@@ -306,6 +307,25 @@ public void Validate_ResourceManagerStringLocalizerType()
306307 Assert . Equal ( "Test" , result [ 0 ] . ErrorMessage ) ;
307308 }
308309
310+ [ Fact ]
311+ public void ValidateFixEndlessLoop ( )
312+ {
313+ var sc = new ServiceCollection ( ) ;
314+ sc . AddConfiguration ( ) ;
315+ sc . AddBootstrapBlazor ( ) ;
316+ sc . AddSingleton < IStringLocalizerFactory , MockLocalizerFactory > ( ) ;
317+ sc . AddSingleton < IStringLocalizerFactory , MockLocalizerFactory2 > ( ) ;
318+
319+ var provider = sc . BuildServiceProvider ( ) ;
320+ var localizer = provider . GetRequiredService < IStringLocalizer < Foo > > ( ) ;
321+
322+ Assert . Equal ( "姓名" , localizer [ "Name" ] ) ;
323+
324+ var items = localizer . GetAllStrings ( false ) ;
325+ Assert . Equal ( "姓名" , items . First ( i => i . Name == "Name" ) . Value ) ;
326+ Assert . DoesNotContain ( "Test-JsonName" , items . Select ( i => i . Name ) ) ;
327+ }
328+
309329 private class MockTypeInfo : TypeDelegator
310330 {
311331 public override string ? FullName => null ;
@@ -318,6 +338,37 @@ private class MockLocalizerFactory : IStringLocalizerFactory
318338 public IStringLocalizer Create ( string baseName , string location ) => new MockStringLocalizer ( ) ;
319339 }
320340
341+ private class MockLocalizerFactory2 : IStringLocalizerFactory
342+ {
343+ private readonly IServiceProvider _serviceProvider ;
344+
345+ public MockLocalizerFactory2 ( IServiceProvider serviceProvider )
346+ {
347+ _serviceProvider = serviceProvider ;
348+ }
349+
350+ public IStringLocalizer Create ( Type resourceSource )
351+ {
352+ var stringLocalizerFactorys = _serviceProvider . GetServices < IStringLocalizerFactory > ( ) ;
353+ IStringLocalizerFactory stringLocalizerFactory ;
354+ if ( resourceSource == typeof ( Foo ) )
355+ {
356+ stringLocalizerFactory = stringLocalizerFactorys . Single ( s => s . GetType ( ) . Name == "JsonStringLocalizerFactory" ) ;
357+ }
358+ else
359+ {
360+ stringLocalizerFactory = _serviceProvider . GetServices < IStringLocalizerFactory > ( ) . Single ( s => s is MockLocalizerFactory ) ;
361+ }
362+ return stringLocalizerFactory . Create ( resourceSource ) ;
363+ }
364+
365+ public IStringLocalizer Create ( string baseName , string location )
366+ {
367+ var stringLocalizerFactory = _serviceProvider . GetServices < IStringLocalizerFactory > ( ) . Single ( s => s is MockLocalizerFactory ) ;
368+ return stringLocalizerFactory . Create ( baseName , location ) ;
369+ }
370+ }
371+
321372 private class MockStringLocalizer : IStringLocalizer
322373 {
323374 public LocalizedString this [ string name ] => GetAllStrings ( true ) . FirstOrDefault ( l => l . Name == name ) ?? new LocalizedString ( name , name , true ) ;
0 commit comments