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

CultureInfo should independent from local computer settings (Lombiq Technologies: OCORE-86) #12467

Merged
merged 38 commits into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8d27f7f
CultureInfo should independent from local computer settings
hishamco Sep 21, 2022
13c7e71
LocalizationOptions -> CultureLocalizationOptions
hishamco Sep 21, 2022
513deb6
Ability to set the user selected culture settings via CultureScope
hishamco Sep 21, 2022
09ae038
Add GetCultureSettingsAsync() to ILocalizationService
hishamco Sep 21, 2022
8a73fe4
Fix the build
hishamco Sep 21, 2022
a3943c6
Use ShellScope.Services
hishamco Sep 21, 2022
c19f09f
Fix CultureScopeTests
hishamco Sep 21, 2022
ed03682
Add cultureSettings param
hishamco Sep 22, 2022
a6814bb
Update LocalizationSettings.cs
sebastienros Sep 22, 2022
c147a58
Update LocalizationService.cs
sebastienros Sep 22, 2022
80f35c7
Update Startup.cs
sebastienros Sep 22, 2022
aec12f5
Update CultureScope.cs
sebastienros Sep 22, 2022
6f82230
Update ILocalizationService.cs
sebastienros Sep 22, 2022
7732ae2
Update DefaultLocalizationService.cs
sebastienros Sep 22, 2022
ea11abe
Update CultureScopeTests.cs
sebastienros Sep 22, 2022
7ad6674
Merge remote-tracking branch 'origin/hishamco/culture-info' into hish…
hishamco Sep 24, 2022
33d949e
Bring CultureLocalizationOptions
hishamco Sep 24, 2022
75a6271
Address feedback
hishamco Sep 24, 2022
1ba99ba
Revert changes in localization service
hishamco Sep 24, 2022
7b1d4ad
Update OC.Settings
hishamco Sep 24, 2022
ba30774
Cleanup
hishamco Sep 24, 2022
f09b7a3
Add docs
hishamco Sep 24, 2022
b9d31dc
Rename params
hishamco Sep 26, 2022
df51b5e
Rename section name
hishamco Sep 26, 2022
198aa65
Move CultureOptions registration into AddDefaultServices()
hishamco Sep 26, 2022
1a64d88
Update appsettings.json
hishamco Sep 26, 2022
ba0796b
Simplification
hishamco Sep 29, 2022
464194a
Add missing docs
hishamco Sep 30, 2022
2af3693
Fix useUserOverride value
hishamco Sep 30, 2022
624d69c
Add SetDefaultCulture() extension method
hishamco Sep 30, 2022
021a0e1
Simplification with OrchardCoreRequestLocalizationOptions
hishamco Sep 30, 2022
1400dbd
Don't use optional params in constructor
hishamco Sep 30, 2022
7139293
Fix the build
hishamco Sep 30, 2022
d716912
Address feedback
hishamco Sep 30, 2022
5c56161
Remove extra line
hishamco Oct 1, 2022
c76fc25
Fix typo
hishamco Oct 1, 2022
01ed694
Refer to the issue in the remarks tag
hishamco Oct 1, 2022
1abcbf0
Address feedback
hishamco Oct 1, 2022
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
5 changes: 4 additions & 1 deletion src/OrchardCore.Cms.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@
// "TenantId": "",
// "CallbackPath": "/signin-oidc",
// "SaveTokens": false
//}
//},
"OrchardCore_Localization": {
"CultureSettings": 0 // 0 for default culture settings, 1 for user-selected culture settings.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace OrchardCore.Localization;

public class CultureLocalizationOptions
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
public CultureSettings CultureSettings { get; set; } = CultureSettings.Default;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OrchardCore.Localization;

public enum CultureSettings
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
Default,
User
}
25 changes: 18 additions & 7 deletions src/OrchardCore.Modules/OrchardCore.Localization/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.Environment.Shell.Configuration;
using OrchardCore.Localization.Drivers;
using OrchardCore.Localization.Models;
using OrchardCore.Localization.Services;
Expand All @@ -21,6 +22,13 @@ namespace OrchardCore.Localization
/// </summary>
public class Startup : StartupBase
{
private readonly IShellConfiguration _shellConfiguration;

public Startup(IShellConfiguration shellConfiguration)
{
_shellConfiguration = shellConfiguration;
}

public override int ConfigureOrder => -100;

/// <inheritdocs />
Expand All @@ -35,6 +43,8 @@ public override void ConfigureServices(IServiceCollection services)
AddDataAnnotationsPortableObjectLocalization();

services.Replace(ServiceDescriptor.Singleton<ILocalizationFileLocationProvider, ModularPoFileLocationProvider>());

services.Configure<CultureLocalizationOptions>(_shellConfiguration.GetSection("OrchardCore_Localization"));
}

/// <inheritdocs />
Expand All @@ -45,14 +55,15 @@ public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder ro
var defaultCulture = localizationService.GetDefaultCultureAsync().GetAwaiter().GetResult();
var supportedCultures = localizationService.GetSupportedCulturesAsync().GetAwaiter().GetResult();

var options = serviceProvider.GetService<IOptions<RequestLocalizationOptions>>().Value;
options.SetDefaultCulture(defaultCulture);
options
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures)
;
var requestLocalizationOptions = serviceProvider.GetService<IOptions<RequestLocalizationOptions>>().Value;
var cultureLocalizationOptions = serviceProvider.GetService<IOptions<CultureLocalizationOptions>>().Value;

requestLocalizationOptions
.SetDefaultCulture(defaultCulture)
.AddSupportedCultures(supportedCultures, cultureLocalizationOptions.CultureSettings == CultureSettings.User)
.AddSupportedUICultures(supportedCultures, cultureLocalizationOptions.CultureSettings == CultureSettings.User);

app.UseRequestLocalization(options);
app.UseRequestLocalization(requestLocalizationOptions);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Globalization;

namespace Microsoft.AspNetCore.Builder;

/// <summary>
/// Extension methods for the <see cref="RequestLocalizationOptions"/>.
/// </summary>
public static class RequestLocalizationOptionsExtensions
{
/// <summary>
/// Adds the set of the supported cultures by the application.
/// </summary>
/// <param name="requestLocalizationOptions">The <see cref="RequestLocalizationOptions"/>.</param>
/// <param name="cultures">The cultures to be added.</param>
/// <param name="userUserSelectedCultureSettings">Whether to use the user-selected culture settings or not. Default to <c>false</c> which uses the default culture settings.</param>
/// <returns>The <see cref="RequestLocalizationOptions"/>.</returns>
public static RequestLocalizationOptions AddSupportedCultures(
this RequestLocalizationOptions requestLocalizationOptions,
string[] cultures,
bool userUserSelectedCultureSettings = false)
{
if (requestLocalizationOptions == null)
{
throw new ArgumentNullException(nameof(requestLocalizationOptions));
}

if (cultures is null)
{
throw new ArgumentNullException(nameof(cultures));
}

var supportedCultures = new List<CultureInfo>(cultures.Length);

foreach (var culture in cultures)
{
supportedCultures.Add(new CultureInfo(culture, userUserSelectedCultureSettings));
}

requestLocalizationOptions.SupportedCultures = supportedCultures;

return requestLocalizationOptions;
}

/// <summary>
/// Adds the set of the supported UI cultures by the application.
/// </summary>
/// <param name="requestLocalizationOptions">The <see cref="RequestLocalizationOptions"/>.</param>
/// <param name="cultures">The cultures to be added.</param>
/// <param name="userUserSelectedCultureSettings">Whether to use the user-selected culture settings or not. Default to <c>false</c> which uses the default culture settings.</param>
/// <returns>The <see cref="RequestLocalizationOptions"/>.</returns>
public static RequestLocalizationOptions AddSupportedUICultures(
this RequestLocalizationOptions requestLocalizationOptions,
string[] cultures,
bool userUserSelectedCultureSettings = false)
{
if (requestLocalizationOptions == null)
{
throw new ArgumentNullException(nameof(requestLocalizationOptions));
}

if (cultures is null)
{
throw new ArgumentNullException(nameof(cultures));
}

var supportedUICultures = new List<CultureInfo>(cultures.Length);

foreach (var culture in cultures)
{
supportedUICultures.Add(new CultureInfo(culture, userUserSelectedCultureSettings));
}

requestLocalizationOptions.SupportedUICultures = supportedUICultures;

return requestLocalizationOptions;
}
}