forked from OrchardCMS/OrchardCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
User Localization settings (OrchardCMS#13181)
- Loading branch information
Showing
13 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...chardCore.Modules/OrchardCore.Users/Localization/Drivers/UserLocalizationDisplayDriver.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,68 @@ | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.DisplayManagement.Entities; | ||
using OrchardCore.DisplayManagement.Handlers; | ||
using OrchardCore.DisplayManagement.ModelBinding; | ||
using OrchardCore.DisplayManagement.Views; | ||
using OrchardCore.Localization; | ||
using OrchardCore.Users.Localization.Models; | ||
using OrchardCore.Users.Localization.ViewModels; | ||
using OrchardCore.Users.Models; | ||
|
||
namespace OrchardCore.Users.Localization.Drivers; | ||
|
||
public class UserLocalizationDisplayDriver : SectionDisplayDriver<User, UserLocalizationSettings> | ||
{ | ||
private readonly ILocalizationService _localizationService; | ||
protected readonly IStringLocalizer S; | ||
|
||
public UserLocalizationDisplayDriver( | ||
ILocalizationService localizationService, | ||
IStringLocalizer<UserLocalizationDisplayDriver> localizer) | ||
{ | ||
_localizationService = localizationService; | ||
S = localizer; | ||
} | ||
|
||
public override Task<IDisplayResult> EditAsync(UserLocalizationSettings section, BuildEditorContext context) | ||
{ | ||
return Task.FromResult<IDisplayResult>(Initialize<UserLocalizationViewModel>("UserCulture_Edit", async model => | ||
{ | ||
var supportedCultures = await _localizationService.GetSupportedCulturesAsync(); | ||
var cultureList = supportedCultures.Select(culture => | ||
new SelectListItem | ||
{ | ||
Text = CultureInfo.GetCultureInfo(culture).DisplayName + " (" + culture + ")", | ||
Value = culture | ||
}).ToList(); | ||
cultureList.Insert(0, new SelectListItem() { Text = S["Use site's culture"], Value = "none" }); | ||
// If Invariant Culture is installed as a supported culture we bind it to a different culture code than String.Empty. | ||
var emptyCulture = cultureList.FirstOrDefault(c => c.Value == ""); | ||
if (emptyCulture != null) | ||
{ | ||
emptyCulture.Value = UserLocalizationConstants.Invariant; | ||
} | ||
model.SelectedCulture = section.Culture; | ||
model.CultureList = cultureList; | ||
}).Location("Content:2")); | ||
} | ||
|
||
public override async Task<IDisplayResult> UpdateAsync(User model, UserLocalizationSettings section, IUpdateModel updater, BuildEditorContext context) | ||
{ | ||
var viewModel = new UserLocalizationViewModel(); | ||
|
||
if (await context.Updater.TryUpdateModelAsync(viewModel, Prefix)) | ||
{ | ||
section.Culture = viewModel.SelectedCulture; | ||
} | ||
|
||
return await EditAsync(section, context); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...rchardCore.Modules/OrchardCore.Users/Localization/Extensions/ClaimsPrincipleExtensions.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,9 @@ | ||
using OrchardCore.Users.Localization.Providers; | ||
|
||
namespace System.Security.Claims; | ||
|
||
public static class ClaimsPrincipleExtensions | ||
{ | ||
public static string GetCulture(this ClaimsPrincipal principal) | ||
=> principal.FindFirstValue(UserLocalizationClaimsProvider.CultureClaimType); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/OrchardCore.Modules/OrchardCore.Users/Localization/Models/UserLocalizationSettings.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,9 @@ | ||
namespace OrchardCore.Users.Localization.Models; | ||
|
||
/// <summary> | ||
/// Provides a model for the IEntity property. | ||
/// </summary> | ||
public class UserLocalizationSettings | ||
{ | ||
public string Culture { get; set; } | ||
} |
37 changes: 37 additions & 0 deletions
37
...rdCore.Modules/OrchardCore.Users/Localization/Providers/UserLocalizationClaimsProvider.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; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using OrchardCore.Entities; | ||
using OrchardCore.Users.Localization.Models; | ||
using OrchardCore.Users.Models; | ||
using OrchardCore.Users.Services; | ||
|
||
namespace OrchardCore.Users.Localization.Providers; | ||
|
||
public class UserLocalizationClaimsProvider : IUserClaimsProvider | ||
{ | ||
internal const string CultureClaimType = "culture"; | ||
|
||
public Task GenerateAsync(IUser user, ClaimsIdentity claims) | ||
{ | ||
ArgumentNullException.ThrowIfNull(user); | ||
ArgumentNullException.ThrowIfNull(claims); | ||
|
||
if (user is not User currentUser) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
if (currentUser.Has<UserLocalizationSettings>()) | ||
{ | ||
var localizationSetting = currentUser.As<UserLocalizationSettings>(); | ||
|
||
if (localizationSetting.Culture != "none") | ||
{ | ||
claims.AddClaim(new Claim(CultureClaimType, localizationSetting.Culture == UserLocalizationConstants.Invariant ? "" : localizationSetting.Culture)); | ||
} | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...odules/OrchardCore.Users/Localization/Providers/UserLocalizationRequestCultureProvider.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,29 @@ | ||
using System; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Localization; | ||
|
||
namespace OrchardCore.Users.Localization.Providers; | ||
|
||
public class UserLocalizationRequestCultureProvider : RequestCultureProvider | ||
{ | ||
public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext) | ||
{ | ||
ArgumentNullException.ThrowIfNull(httpContext); | ||
|
||
if (httpContext?.User?.Identity?.IsAuthenticated == false) | ||
{ | ||
return NullProviderCultureResult; | ||
} | ||
|
||
var userCulture = httpContext.User.GetCulture(); // String.Empty here means that it did not find the Culture Claim. | ||
|
||
if (String.IsNullOrWhiteSpace(userCulture)) | ||
{ | ||
return NullProviderCultureResult; | ||
} | ||
|
||
return Task.FromResult(new ProviderCultureResult(userCulture == UserLocalizationConstants.Invariant ? "" : userCulture)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/OrchardCore.Modules/OrchardCore.Users/Localization/Startup.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,23 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.DisplayManagement.Handlers; | ||
using OrchardCore.Modules; | ||
using OrchardCore.Users.Localization.Drivers; | ||
using OrchardCore.Users.Localization.Providers; | ||
using OrchardCore.Users.Models; | ||
using OrchardCore.Users.Services; | ||
|
||
namespace OrchardCore.Users.Localization; | ||
|
||
[Feature("OrchardCore.Users.Localization")] | ||
public class Startup : StartupBase | ||
{ | ||
public override void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddScoped<IDisplayDriver<User>, UserLocalizationDisplayDriver>(); | ||
services.AddScoped<IUserClaimsProvider, UserLocalizationClaimsProvider>(); | ||
|
||
services.Configure<RequestLocalizationOptions>(options => | ||
options.AddInitialRequestCultureProvider(new UserLocalizationRequestCultureProvider())); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/OrchardCore.Modules/OrchardCore.Users/Localization/UserLocalizationConstants.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,7 @@ | ||
namespace OrchardCore.Users.Localization | ||
{ | ||
public static class UserLocalizationConstants | ||
{ | ||
public const string Invariant = "invariant"; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...OrchardCore.Modules/OrchardCore.Users/Localization/ViewModel/UserLocalizationViewModel.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,13 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
|
||
namespace OrchardCore.Users.Localization.ViewModels; | ||
|
||
public class UserLocalizationViewModel | ||
{ | ||
public string SelectedCulture { get; set; } | ||
|
||
[BindNever] | ||
public List<SelectListItem> CultureList { 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
10 changes: 10 additions & 0 deletions
10
src/OrchardCore.Modules/OrchardCore.Users/Views/UserCulture.Edit.cshtml
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,10 @@ | ||
@using OrchardCore.Users.Localization.ViewModels | ||
@model UserLocalizationViewModel | ||
|
||
<div class="mb-3" asp-validation-class-for="SelectedCulture"> | ||
<label asp-for="SelectedCulture">@T["Default User Culture"]</label> | ||
<select asp-for="SelectedCulture" asp-items="Model.CultureList" class="form-select"> | ||
</select> | ||
<span asp-validation-for="SelectedCulture"></span> | ||
<span class="hint">@T["Determines the default culture used by this user."]</span> | ||
</div> |
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
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
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