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

Use RequestLocalizationOptions.CultureInfoUseUserOverride #14716

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 8 additions & 8 deletions src/OrchardCore.Modules/OrchardCore.Localization/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder ro
var defaultCulture = localizationService.GetDefaultCultureAsync().GetAwaiter().GetResult();
var supportedCultures = localizationService.GetSupportedCulturesAsync().GetAwaiter().GetResult();

var localizationOptions = serviceProvider.GetService<IOptions<RequestLocalizationOptions>>().Value;
var ignoreSystemSettings = serviceProvider.GetService<IOptions<CultureOptions>>().Value.IgnoreSystemSettings;
var cultureOptions = serviceProvider.GetService<IOptions<CultureOptions>>().Value;

new LocalizationOptionsUpdater(localizationOptions, ignoreSystemSettings)
.SetDefaultCulture(defaultCulture)
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);

app.UseRequestLocalization(localizationOptions);
app.UseRequestLocalization(options =>
{
options.CultureInfoUseUserOverride = !cultureOptions.IgnoreSystemSettings;
options.SetDefaultCulture(defaultCulture);
options.AddSupportedCultures(supportedCultures);
options.AddSupportedUICultures(supportedCultures);
});
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/OrchardCore.Modules/OrchardCore.Setup/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,20 @@ public override void ConfigureServices(IServiceCollection services)
public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
{
var localizationOptions = serviceProvider.GetService<IOptions<RequestLocalizationOptions>>().Value;
var ignoreSystemSettings = serviceProvider.GetService<IOptions<CultureOptions>>().Value.IgnoreSystemSettings;
var cultureOptions = serviceProvider.GetService<IOptions<CultureOptions>>().Value;

localizationOptions.CultureInfoUseUserOverride = !cultureOptions.IgnoreSystemSettings;

var localizationOptionsUpdater = new LocalizationOptionsUpdater(localizationOptions, ignoreSystemSettings);
if (!string.IsNullOrEmpty(_defaultCulture))
{
localizationOptionsUpdater.SetDefaultCulture(_defaultCulture);
localizationOptions.SetDefaultCulture(_defaultCulture);

_supportedCultures = _supportedCultures.Union(new[] { _defaultCulture }).ToArray();
}

if (_supportedCultures?.Length > 0)
{
localizationOptionsUpdater
localizationOptions
.AddSupportedCultures(_supportedCultures)
.AddSupportedUICultures(_supportedCultures);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNetCore.Builder;
Expand All @@ -12,11 +13,20 @@ namespace OrchardCore.Localization;
/// This is mainly used in the localization module to update the current <see cref="RequestLocalizationOptions"/> that
/// might set from other modules in Orchard Core pipeline.
/// </remarks>
[Obsolete("This class is deprecated, and will be removed in the upcoming major release.")]
public class LocalizationOptionsUpdater
{
private readonly bool _useUserOverride;
private readonly RequestLocalizationOptions _options;

/// <summary>
/// Initializes a new instance of a <see cref="LocalizationOptionsUpdater"/>.
/// </summary>
/// <param name="options">The <see cref="RequestLocalizationOptions"/>.</param>
public LocalizationOptionsUpdater(RequestLocalizationOptions options)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems there's no need for the LocalizationOptionsUpdater changes, right? I did it before I rid off the use of LocalizationOptionsUpdater

{
_options = options;
}

/// <summary>
/// Initializes a new instance of a <see cref="LocalizationOptionsUpdater"/>.
/// </summary>
Expand All @@ -25,7 +35,7 @@ public class LocalizationOptionsUpdater
public LocalizationOptionsUpdater(RequestLocalizationOptions options, bool ignoreSystemSettings)
{
_options = options;
_useUserOverride = !ignoreSystemSettings;
_options.CultureInfoUseUserOverride = !ignoreSystemSettings;
}

/// <summary>
Expand All @@ -37,7 +47,7 @@ public LocalizationOptionsUpdater AddSupportedCultures(params string[] cultures)
var supportedCultures = new List<CultureInfo>();
foreach (var culture in cultures)
{
supportedCultures.Add(new CultureInfo(culture, _useUserOverride));
supportedCultures.Add(new CultureInfo(culture, _options.CultureInfoUseUserOverride));
}

_options.SupportedCultures = supportedCultures;
Expand All @@ -54,7 +64,7 @@ public LocalizationOptionsUpdater AddSupportedUICultures(params string[] uiCultu
var supportedUICultures = new List<CultureInfo>();
foreach (var culture in uiCultures)
{
supportedUICultures.Add(new CultureInfo(culture, _useUserOverride));
supportedUICultures.Add(new CultureInfo(culture, _options.CultureInfoUseUserOverride));
}

_options.SupportedUICultures = supportedUICultures;
Expand All @@ -68,7 +78,7 @@ public LocalizationOptionsUpdater AddSupportedUICultures(params string[] uiCultu
/// <param name="defaultCulture">The default culture.</param>
public LocalizationOptionsUpdater SetDefaultCulture(string defaultCulture)
{
_options.DefaultRequestCulture = new RequestCulture(new CultureInfo(defaultCulture, _useUserOverride));
_options.DefaultRequestCulture = new RequestCulture(new CultureInfo(defaultCulture, _options.CultureInfoUseUserOverride));

return this;
}
Expand Down