-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
test/OrchardCore.Tests/Localization/DataAnnotationsLocalizationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ILocalizationFileLocationProvider, StubPoFileLocationProvider>()); | ||
} | ||
|
||
public void Configure( | ||
IApplicationBuilder app, | ||
IStringLocalizer<Model> 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; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#: OrchardCore.Tests.Localization\DataAnnotationsLocalizationTests.cs:77 | ||
msgctxt "OrchardCore.Tests.Localization.DataAnnotationsLocalizationTests.Model" | ||
msgid "Hello" | ||
msgstr "مرحبا" |
37 changes: 37 additions & 0 deletions
37
test/OrchardCore.Tests/Stubs/StubPoFileLocationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> localizationOptions) | ||
{ | ||
var rootPath = new DirectoryInfo(hostingEnvironment.ContentRootPath).Parent.Parent.Parent.FullName; | ||
_fileProvider = new PhysicalFileProvider(rootPath); | ||
_resourcesContainer = localizationOptions.Value.ResourcesPath; | ||
} | ||
|
||
public IEnumerable<IFileInfo> GetLocations(string cultureName) | ||
{ | ||
yield return _fileProvider.GetFileInfo(Path.Combine(_resourcesContainer, cultureName + ".po")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
} |