Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Localization Rule #16153 #16154

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 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
1 change: 1 addition & 0 deletions src/OrchardCore.Cms.Web/OrchardCore.Cms.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OrchardCore.Modules\OrchardCore.Demo\OrchardCore.Demo.csproj" />
<ProjectReference Include="..\OrchardCore\OrchardCore.Application.Cms.Targets\OrchardCore.Application.Cms.Targets.csproj" />
<ProjectReference Include="..\OrchardCore\OrchardCore.Logging.NLog\OrchardCore.Logging.NLog.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using OrchardCore.ContentManagement;
using OrchardCore.Contents;

namespace OrchardCore.Demo.Controllers
{
[Route("api/demo")]
[Authorize(AuthenticationSchemes = "Api"), IgnoreAntiforgeryToken, AllowAnonymous]
[ApiController]
[Authorize(AuthenticationSchemes = "Api"), IgnoreAntiforgeryToken, AllowAnonymous]
public class ContentApiController : ControllerBase
{
private readonly IAuthorizationService _authorizationService;
private readonly IContentManager _contentManager;
private readonly IStringLocalizer S;

public ContentApiController(IAuthorizationService authorizationService, IContentManager contentManager)
public ContentApiController(IAuthorizationService authorizationService, IContentManager contentManager, IStringLocalizer<ContentApiController> stringLocalizer)
{
_authorizationService = authorizationService;
_contentManager = contentManager;
S = stringLocalizer;
}

[HttpGet]
[Route("~/api/demo/sayhello")]
public IActionResult SayHello()
{
return Content(S["Hello!"]);
}

public async Task<IActionResult> GetById(string id)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

msgctxt "OrchardCore.Demo.ContentApiController"
msgid "Hello!"
msgstr "Hello en!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

msgctxt "OrchardCore.Demo.ContentApiController"
msgid "Hello!"
msgstr "你好!"
12 changes: 12 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Demo/OrchardCore.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@
<PackageTags>$(PackageTags) OrchardCoreCMS</PackageTags>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Remove="Localization\en\OrchardCore.Demo.po" />
<EmbeddedResource Remove="Localization\zh-CN\OrchardCore.Demo.po" />
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<Content Include="Localization\**" >
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Admin.Abstractions\OrchardCore.Admin.Abstractions.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Apis.GraphQL.Abstractions\OrchardCore.Apis.GraphQL.Abstractions.csproj" />
Expand All @@ -28,4 +39,5 @@
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Users.Core\OrchardCore.Users.Core.csproj" />
</ItemGroup>


</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public IEnumerable<IFileInfo> GetLocations(string cultureName)

// \src\OrchardCore.Cms.Web\Localization\OrchardCore.Cms.Web-fr-CA.po
yield return _fileProvider.GetFileInfo(PathExtensions.Combine(_resourcesContainer, extension.Id + CultureDelimiter + poFileName));

// \Localization\[CultureName]\[ModuleId].po
yield return _fileProvider.GetFileInfo(PathExtensions.Combine(_resourcesContainer, cultureName, extension.Id + PoFileExtension));
hyzx86 marked this conversation as resolved.
Show resolved Hide resolved
}

// Load all .po files from a culture specific folder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using OrchardCore.Entities;
using OrchardCore.Environment.Shell;
using OrchardCore.Features.Services;
using OrchardCore.Localization;
using OrchardCore.Localization.Models;
using OrchardCore.Settings;
using OrchardCore.Tests.Apis.Context;

namespace OrchardCore.Tests.Localization
{
Expand All @@ -24,6 +30,65 @@ public void TryGetRuleShouldReturnRuleForTopCulture(string culture, string expec
Assert.NotNull(expectedPlural);
Assert.NotNull(testPlural);
Assert.Same(expectedPlural, testPlural);
}

[Fact]
public async Task TestSayHello()
{
var context = new SiteContext();
await context.InitializeAsync();
await context.UsingTenantScopeAsync(async scope =>
{
var shellFeaturesManager = scope.ServiceProvider.GetRequiredService<IShellFeaturesManager>();
var availableFeatures = await shellFeaturesManager.GetAvailableFeaturesAsync();
var featureIds = new string[] { "OrchardCore.Localization.ContentLanguageHeader", "OrchardCore.Localization", "OrchardCore.Demo" };
var features = availableFeatures.Where(feature => featureIds.Contains(feature.Id));

await shellFeaturesManager.EnableFeaturesAsync(features, true);

var siteService = scope.ServiceProvider.GetRequiredService<ISiteService>();
var siteSettings = await siteService.LoadSiteSettingsAsync();
siteSettings.Alter<LocalizationSettings>("LocalizationSettings", localizationSettings =>
{
localizationSettings.DefaultCulture = "en";
localizationSettings.SupportedCultures = ["en", "zh-CN"];
});
await siteService.UpdateSiteSettingsAsync(siteSettings);

var shellSettings = scope.ServiceProvider.GetRequiredService<ShellSettings>();
var shellHost = scope.ServiceProvider.GetRequiredService<IShellHost>();
await shellHost.ReleaseShellContextAsync(shellSettings);
});

// en/OrchardCore.Demo.po
var requestEn = new HttpRequestMessage(HttpMethod.Get, "api/demo/SayHello");
requestEn.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content2 = new StringContent("");
content2.Headers.ContentLanguage.Add("en");
requestEn.Content = content2;
var response2 = await context.Client.SendAsync(requestEn);

response2.EnsureSuccessStatusCode();

var result2 = await response2.Content.ReadAsStringAsync();
Assert.Equal("Hello en!", result2);

// zh-CN/OrchardCore.Demo.po
var request = new HttpRequestMessage(HttpMethod.Get, "api/demo/SayHello");
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent("");
content.Headers.ContentLanguage.Add("zh-CN");
request.Content = content;
var response = await context.Client.SendAsync(request);

response.EnsureSuccessStatusCode();
hyzx86 marked this conversation as resolved.
Show resolved Hide resolved

var result = await response.Content.ReadAsStringAsync();
Assert.Equal("你好!", result);




}
}
}
Loading