diff --git a/test/OrchardCore.Tests/Localization/DataAnnotationsLocalizationTests.cs b/test/OrchardCore.Tests/Localization/DataAnnotationsLocalizationTests.cs new file mode 100644 index 00000000000..cb25c3683cb --- /dev/null +++ b/test/OrchardCore.Tests/Localization/DataAnnotationsLocalizationTests.cs @@ -0,0 +1,63 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.IO; +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Localization; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.FileProviders; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Localization; +using Microsoft.Extensions.Options; +using OrchardCore.Localization; +using OrchardCore.Tests; +using Xunit; + +namespace OrchardCore.Tests.Localization +{ + public class DataAnnotationsLocalizationTests + { + [Fact] + public async Task LocalizerReturnsDataAnnotationsTranslationFromInnerClass() + => await StartupRunner.Run(typeof(DataAnnotationsLocalizationStartup),"ar", "مرحبا"); + + public class DataAnnotationsLocalizationStartup + { + public void ConfigureServices(IServiceCollection services) + { + services.AddMvc(); + services.AddLocalization(); + services.AddPortableObjectLocalization(options => options.ResourcesPath = "Localization/PoFiles"); + services.Replace(ServiceDescriptor.Singleton()); + } + + public void Configure( + IApplicationBuilder app, + IStringLocalizer localizer) + { + var supportedCultures = new[] { "ar", "en" }; + app.UseRequestLocalization(options => + options + .AddSupportedCultures(supportedCultures) + .AddSupportedUICultures(supportedCultures) + .SetDefaultCulture("ar") + ); + + app.Run(async (context) => + { + await context.Response.WriteAsync(localizer["Hello"]); + }); + } + } + + public class Model + { + [Display(Name ="مرحبا")] + public string Hello { get; set; } + } + } +} diff --git a/test/OrchardCore.Tests/Localization/PoFiles/ar.po b/test/OrchardCore.Tests/Localization/PoFiles/ar.po new file mode 100644 index 00000000000..b3f004ef447 --- /dev/null +++ b/test/OrchardCore.Tests/Localization/PoFiles/ar.po @@ -0,0 +1,4 @@ +#: OrchardCore.Tests.Localization\DataAnnotationsLocalizationTests.cs:77 +msgctxt "OrchardCore.Tests.Localization.DataAnnotationsLocalizationTests.Model" +msgid "Hello" +msgstr "مرحبا" \ No newline at end of file diff --git a/test/OrchardCore.Tests/Stubs/StubPoFileLocationProvider.cs b/test/OrchardCore.Tests/Stubs/StubPoFileLocationProvider.cs new file mode 100644 index 00000000000..9a310466981 --- /dev/null +++ b/test/OrchardCore.Tests/Stubs/StubPoFileLocationProvider.cs @@ -0,0 +1,37 @@ +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Localization; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.FileProviders; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Localization; +using Microsoft.Extensions.Options; +using OrchardCore.Localization; +using OrchardCore.Tests; + +namespace OrchardCore.Tests +{ + public class StubPoFileLocationProvider : ILocalizationFileLocationProvider + { + private readonly IFileProvider _fileProvider; + private readonly string _resourcesContainer; + + public StubPoFileLocationProvider(IHostEnvironment hostingEnvironment, IOptions localizationOptions) + { + var rootPath = new DirectoryInfo(hostingEnvironment.ContentRootPath).Parent.Parent.Parent.FullName; + _fileProvider = new PhysicalFileProvider(rootPath); + _resourcesContainer = localizationOptions.Value.ResourcesPath; + } + + public IEnumerable GetLocations(string cultureName) + { + yield return _fileProvider.GetFileInfo(Path.Combine(_resourcesContainer, cultureName + ".po")); + } + } +} \ No newline at end of file diff --git a/test/OrchardCore.Tests/Utilities/StartupRunner.cs b/test/OrchardCore.Tests/Utilities/StartupRunner.cs new file mode 100644 index 00000000000..033c5df188f --- /dev/null +++ b/test/OrchardCore.Tests/Utilities/StartupRunner.cs @@ -0,0 +1,30 @@ +using System; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Localization; +using Microsoft.AspNetCore.TestHost; +using Xunit; + +namespace OrchardCore.Tests +{ + internal static class StartupRunner + { + public static async Task Run(Type startupType, string culture, string expected) + { + var webHostBuilder = new WebHostBuilder().UseStartup(startupType); + var testHost = new TestServer(webHostBuilder); + + var client = testHost.CreateClient(); + var request = new HttpRequestMessage(); + var cookieValue = $"c={culture}|uic={culture}"; + request.Headers.Add("Cookie", $"{CookieRequestCultureProvider.DefaultCookieName}={cookieValue}"); + + var response = await client.SendAsync(request); + + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expected, await response.Content.ReadAsStringAsync()); + } + } +} \ No newline at end of file