public void Should_create_string_localizer() { var factory = new JsonStringLocalizerFactory(); var result = factory.Create(typeof(ShouldCreateStringLocalizer)); result.ShouldBeOfType <JsonStringLocalizer>(); }
public void ItShouldCacheTheComputedLocalizer() { // Setup CultureInfo service mock. var cultureInfoServiceMock = SetupCultureInfoServiceMock(); var extensionOptionsContainer = new ExtensionOptionsContainer <MyOptions>(new MyOptions()); // Setup extension service var extensionServiceMock = SetupExtensionServiceMock(new Dictionary <string, string>(), extensionOptionsContainer); // Setup extension resolver service. var extensionResolverServiceMock = SetupResolverServiceMock( (extensionOptionsContainer, extensionServiceMock.Object)); var optionsMock = SetupJsonLocalizationOptionsMock(extensionOptionsContainer); var cacheServiceMock = new Mock <ICacheService>(); var factory = new JsonStringLocalizerFactory( optionsMock.Object, cultureInfoServiceMock.Object, extensionResolverServiceMock.Object, cacheServiceMock.Object, Logger); var localizer = factory.Create(BaseName, Assembly.FullName); Assert.NotNull(localizer); cacheServiceMock.Verify(x => x.Match(Assembly, BaseName, null), Times.Once); cacheServiceMock.Verify(x => x.Cache(Assembly, BaseName, null, localizer), Times.Once); cacheServiceMock.Verify(x => x.Match(Assembly, BaseName, CultureInfo), Times.Once); cacheServiceMock.Verify(x => x.Cache(Assembly, BaseName, CultureInfo, It.IsAny <IStringLocalizer>()), Times.Once); }
/// <summary> /// 构造函数 /// </summary> public FileValidationAttribute() { var options = ServiceProviderHelper.ServiceProvider.GetRequiredService <IOptions <JsonLocalizationOptions> >(); var loggerFactory = ServiceProviderHelper.ServiceProvider.GetRequiredService <ILoggerFactory>(); var factory = new JsonStringLocalizerFactory(options, loggerFactory); Localizer = factory.Create(ResourceType); }
public void CreateFromType() { var factory = new JsonStringLocalizerFactory("Resources"); var localizer = factory.Create(typeof(UtilResources)); #pragma warning disable CS0618 // Type or member is obsolete var all = localizer.WithCulture(new System.Globalization.CultureInfo("es")).GetAllStrings(true); #pragma warning restore CS0618 // Type or member is obsolete }
public BenchmarkJSONLocalizer() { var serviceProvider = new ServiceCollection() .AddLocalization(opts => { opts.ResourcesPath = "Resources"; }) .BuildServiceProvider(); _jsonFactory = new JsonStringLocalizerFactory(env); _jsonLocalizer = _jsonFactory.Create("", ""); }
public void Should_create_string_localizer() { var factory = new JsonStringLocalizerFactory(); var result = factory.Create("JsonLocalization.Test.Localization.base", string.Empty); result.ShouldBeOfType <JsonStringLocalizer>(); var value = result["key"].Value; value.ShouldNotBe("key"); }
public void Should_create_string_localizer() { var factory = new JsonStringLocalizerFactory(); var result = factory.Create(typeof(ShouldCreateStringLocalizerWithValues)); result.ShouldBeOfType <JsonStringLocalizer>(); var value = result["key"].Value; value.ShouldNotBe("key"); }
public JsonStringLocalizerTests() { var _localizationOptions = new Mock <IOptions <JsonLocalizationOptions> >(); _localizationOptions.Setup(o => o.Value) .Returns(() => new JsonLocalizationOptions { ResourcesPath = "Resources" }); var localizerFactory = new JsonStringLocalizerFactory(_localizationOptions.Object, NullLoggerFactory.Instance); var location = "My.Extensions.Localization.Json.Tests"; var basename = $"{location}.Common.{nameof(Test)}"; _localizer = localizerFactory.Create(basename, location); }
/// <summary> /// 获得显示名称方法 /// </summary> /// <returns></returns> public string?GetName() { var ret = Name; if (ResourceType != null && !string.IsNullOrEmpty(ResourceName)) { var options = ServiceProviderHelper.ServiceProvider.GetRequiredService <IOptions <JsonLocalizationOptions> >(); var loggerFactory = ServiceProviderHelper.ServiceProvider.GetRequiredService <ILoggerFactory>(); var factory = new JsonStringLocalizerFactory(options, loggerFactory); var localizer = factory.Create(ResourceType); ret = localizer[ResourceName]; } return(ret); }
public void IsShouldCreateAJsonStringLocalizerUsingTheRightExtensionService() { var key = "key"; var value1 = "value1"; var map1 = new Dictionary <string, string> { [key] = value1, }; var value2 = "value2"; var map2 = new Dictionary <string, string> { [key] = value2, }; // Setup CultureInfo service mock. var cultureInfoServiceMock = SetupCultureInfoServiceMock(); var extensionOptionsContainer1 = new ExtensionOptionsContainer <MyOptions>(new MyOptions()); extensionOptionsContainer1.Options.AssemblyNames = new[] { "OtherAssemblyName" }; var extensionOptionsContainer2 = new ExtensionOptionsContainer <MyOptions>(new MyOptions()); extensionOptionsContainer2.Options.AssemblyNames = new[] { Assembly.GetName().Name }; // Setup extension service var extensionServiceMock1 = SetupExtensionServiceMock(map1, extensionOptionsContainer1); var extensionServiceMock2 = SetupExtensionServiceMock(map2, extensionOptionsContainer2); // Setup extension resolver service. var extensionResolverServiceMock = SetupResolverServiceMock( (extensionOptionsContainer1, extensionServiceMock1.Object), (extensionOptionsContainer2, extensionServiceMock2.Object)); var optionsMock = SetupJsonLocalizationOptionsMock(extensionOptionsContainer1, extensionOptionsContainer2); var cacheServiceMock = new Mock <ICacheService>(); var factory = new JsonStringLocalizerFactory( optionsMock.Object, cultureInfoServiceMock.Object, extensionResolverServiceMock.Object, cacheServiceMock.Object, Logger); var localizer = factory.Create(typeof(JsonStringLocalizerFactoryTest)); Assert.NotNull(localizer); Assert.Equal(value2, localizer[key]); }
public void JsonStringLocalizerFactory_CreateLocalizerWithType() { SetupLocalizationOptions("Resources"); LocalizationHelper.SetCurrentCulture("fr-FR"); // Arrange var localizerFactory = new JsonStringLocalizerFactory(_localizationOptions.Object, _loggerFactory); // Act var localizer = localizerFactory.Create(typeof(Test)); // Assert Assert.NotNull(localizer); Assert.Equal("Bonjour", localizer["Hello"]); }
public async Task ItShouldLoadParentCultureLocalyzer(bool enableHierarchyLoading) { var map = new Dictionary <string, string>(); // Setup CultureInfo service mock. var cultureInfoServiceMock = SetupCultureInfoServiceMock(); var extensionOptionsContainer = new ExtensionOptionsContainer <MyOptions>(new MyOptions()); // Setup extension service var extensionServiceMock = new Mock <IJsonLocalizationExtensionService>(); extensionServiceMock .Setup(s => s.TryLoadAsync(extensionOptionsContainer.Options, Assembly, BaseName, CultureInfo)) .ReturnsAsync(map, TimeSpan.FromMilliseconds(100)); extensionServiceMock .Setup(s => s.TryLoadAsync(extensionOptionsContainer.Options, Assembly, BaseName, CultureInfo.Parent)) .ReturnsAsync(map, TimeSpan.FromMilliseconds(100)); // Setup extension resolver service. var extensionResolverServiceMock = SetupResolverServiceMock( (extensionOptionsContainer, extensionServiceMock.Object)); var optionsMock = SetupJsonLocalizationOptionsMock(extensionOptionsContainer); // We need the real cache service because otherwise the asynchronous parent localizer will be created every time. var cacheService = new CacheService(); var factory = new JsonStringLocalizerFactory( optionsMock.Object, cultureInfoServiceMock.Object, extensionResolverServiceMock.Object, cacheService, Logger); var localizer = factory.Create(typeof(JsonStringLocalizerFactoryTest)); Assert.NotNull(localizer); // make sure the localizer data are loaded. await localizer.LoadAsync(enableHierarchyLoading).ConfigureAwait(false); // verify the parent hierarchy loading behavior. extensionServiceMock.Verify(s => s.TryLoadAsync(extensionOptionsContainer.Options, Assembly, BaseName, CultureInfo), Times.Once); extensionServiceMock.Verify(s => s.TryLoadAsync(extensionOptionsContainer.Options, Assembly, BaseName, CultureInfo.Parent), enableHierarchyLoading ? Times.Once : Times.Never); }
public static IServiceCollection AddAndcultureCodeLocalization(this IServiceCollection services) { var localizerFactory = new JsonStringLocalizerFactory(); services.AddScoped <IStringLocalizer>((sp) => localizerFactory.Create(null)); // Configuration of services services .ConfigureRequestLocalizationOptions() .ConfigureRewriteOptions() .ConfigureRouteOptions(); Console.WriteLine($"Default Localization Culture: {LocalizationUtils.DefaultCultureCode}"); Console.WriteLine($"Localization Cultures: {LocalizationUtils.CultureCodes(", ")}"); return(services); }
public void CreateLocalizerWithBasenameAndLocation(ResourcesType resourcesType) { SetupLocalizationOptions("Resources", resourcesType); LocalizationHelper.SetCurrentCulture("fr-FR"); // Arrange var localizerFactory = new JsonStringLocalizerFactory(_localizationOptions.Object, _loggerFactory); var location = "Legacy.JsonLocalization.Tests"; var basename = $"{location}.{nameof(Test)}"; // Act var localizer = localizerFactory.Create(basename, location); // Assert Assert.NotNull(localizer); Assert.Equal("Bonjour", localizer["Hello"]); }
public void when_non_existing_culture_requested__empty_string_is_retrieved() { // GIVEN: a string localizer factory IStringLocalizerFactory factory = new JsonStringLocalizerFactory( GetFileSystemMock(), new JsonLocalizationOptions { ResourcesPath = @".\lang" }); IStringLocalizer stringLocalizer = factory.Create("res", null); // WHEN: a non existent culture is requested CultureInfo.CurrentUICulture = new CultureInfo("fr-FR"); LocalizedString localizedString = stringLocalizer.GetString("users.values.greeting"); // THEN: a localized string with NotFound flag is requested Assert.True(localizedString.ResourceNotFound); }
public void ItShouldCreateAnAsynchronousLocalizerIfTheExtensionServiceIsAsynchronous() { var key = "key"; var value = "value"; var map = new Dictionary <string, string> { [key] = value, }; // Setup CultureInfo service mock. var cultureInfoServiceMock = SetupCultureInfoServiceMock(); var extensionOptionsContainer = new ExtensionOptionsContainer <MyOptions>(new MyOptions()); // Setup extension service var extensionServiceMock = new Mock <IJsonLocalizationExtensionService>(); extensionServiceMock .Setup(s => s.TryLoadAsync(extensionOptionsContainer.Options, Assembly, BaseName, CultureInfo)) .ReturnsAsync(map, TimeSpan.FromMilliseconds(100)); // Setup extension resolver service. var extensionResolverServiceMock = SetupResolverServiceMock( (extensionOptionsContainer, extensionServiceMock.Object)); var optionsMock = SetupJsonLocalizationOptionsMock(extensionOptionsContainer); var cacheServiceMock = new Mock <ICacheService>(); var factory = new JsonStringLocalizerFactory( optionsMock.Object, cultureInfoServiceMock.Object, extensionResolverServiceMock.Object, cacheServiceMock.Object, Logger); var localizer = factory.Create(typeof(JsonStringLocalizerFactoryTest)); Assert.NotNull(localizer); var proxy = Assert.IsType <StringLocalizerProxy>(localizer); Assert.IsType <JsonStringLocalizerAsync>(proxy.CurrentStringLocalizer); }
public void when_existing_key_and_culture_requested__string_is_retrieved() { // GIVEN: a string localizer factory IStringLocalizerFactory factory = new JsonStringLocalizerFactory( GetFileSystemMock(), new JsonLocalizationOptions { ResourcesPath = @".\lang" }); IStringLocalizer stringLocalizer = factory.Create("res", null); // WHEN: a localization value is requested for a certain culture CultureInfo.CurrentUICulture = new CultureInfo("en-US"); string localized = stringLocalizer.GetString("users.values.greeting"); // THEN: a valid value is requested. Assert.Equal(localized, "hello!"); CultureInfo.CurrentUICulture = new CultureInfo("en"); localized = stringLocalizer.GetString("users.values.greeting"); Assert.Equal(localized, "hallo!"); }
public void ItShouldMatchTheCacheToGetLocalizerProxy() { // Setup CultureInfo service mock. var cultureInfoServiceMock = SetupCultureInfoServiceMock(); // Setup extension service var extensionServiceMock = new Mock <IJsonLocalizationExtensionService>(); // Setup extension resolver service. var extensionResolverServiceMock = new Mock <IExtensionResolverService>(); var optionsMock = new Mock <IOptions <JsonLocalizationOptions> >(); var cacheServiceMock = new Mock <ICacheService>(); var cachedLocalizer = Mock.Of <IStringLocalizer>(); cacheServiceMock.Setup(x => x.Match(Assembly, BaseName, null)).Returns(cachedLocalizer); var factory = new JsonStringLocalizerFactory( optionsMock.Object, cultureInfoServiceMock.Object, extensionResolverServiceMock.Object, cacheServiceMock.Object, Logger); var localizer = factory.Create(BaseName, Assembly.FullName); Assert.NotNull(localizer); cacheServiceMock.Verify(x => x.Match(Assembly, BaseName, null), Times.Once); cacheServiceMock.Verify(x => x.Cache(Assembly, BaseName, null, It.IsAny <IStringLocalizer>()), Times.Never); cacheServiceMock.Verify(x => x.Match(Assembly, BaseName, CultureInfo), Times.Never); cacheServiceMock.Verify(x => x.Cache(Assembly, BaseName, CultureInfo, It.IsAny <IStringLocalizer>()), Times.Never); Assert.Same(cachedLocalizer, localizer); }
public void CreateFromBaseName() { var factory = new JsonStringLocalizerFactory("Resources"); var localizer = factory.Create("Celerik.NetCore.Util", "UtilResources"); }
public async Task ItShouldUseParentCultureLocalyzerWhenKeyNotFound(bool isAsynchronous) { var key = "key"; var value = "value"; var map = new Dictionary <string, string> { [key] = value, }; var key2 = "key2"; var value2 = "value2"; var map2 = new Dictionary <string, string> { [key2] = value2, }; var unknownKey = "test"; // Setup CultureInfo service mock. var cultureInfoServiceMock = SetupCultureInfoServiceMock(); var extensionOptionsContainer = new ExtensionOptionsContainer <MyOptions>(new MyOptions()); // Setup extension service var extensionServiceMock = new Mock <IJsonLocalizationExtensionService>(); if (isAsynchronous) { extensionServiceMock .Setup(s => s.TryLoadAsync(extensionOptionsContainer.Options, Assembly, BaseName, CultureInfo)) .ReturnsAsync(map, TimeSpan.FromMilliseconds(100)); extensionServiceMock .Setup(s => s.TryLoadAsync(extensionOptionsContainer.Options, Assembly, BaseName, CultureInfo.Parent)) .ReturnsAsync(map2, TimeSpan.FromMilliseconds(100)); } else { extensionServiceMock .Setup(s => s.TryLoadAsync(extensionOptionsContainer.Options, Assembly, BaseName, CultureInfo)) .ReturnsAsync(map); extensionServiceMock .Setup(s => s.TryLoadAsync(extensionOptionsContainer.Options, Assembly, BaseName, CultureInfo.Parent)) .ReturnsAsync(map2); } // Setup extension resolver service. var extensionResolverServiceMock = SetupResolverServiceMock( (extensionOptionsContainer, extensionServiceMock.Object)); var optionsMock = SetupJsonLocalizationOptionsMock(extensionOptionsContainer); // We need the real cache service because otherwise the asynchronous parent localizer will be created every time. var cacheService = new CacheService(); var factory = new JsonStringLocalizerFactory( optionsMock.Object, cultureInfoServiceMock.Object, extensionResolverServiceMock.Object, cacheService, Logger); var localizer = factory.Create(typeof(JsonStringLocalizerFactoryTest)); Assert.NotNull(localizer); if (isAsynchronous) { // make sure the localizer data are loaded. await localizer.LoadAsync(true).ConfigureAwait(false); } Assert.Equal(value, localizer[key]); Assert.Equal(value2, localizer[key2]); Assert.Equal(unknownKey, localizer[unknownKey]); var strings = localizer.GetAllStrings(false); var allStrings = localizer.GetAllStrings(true); Assert.Single(strings); Assert.Equal(2, allStrings.Count()); }