From b7619ad7e4072832070b596fea5567dab41e6b9d Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Fri, 31 May 2024 14:51:35 -0700 Subject: [PATCH 1/6] Add supported_cultures liquid filter Fix #16202 --- .../Startup.cs | 2 -- .../Filters/SupportedCulturesFilter.cs | 26 +++++++++++++++++++ .../OrchardCoreBuilderExtensions.cs | 5 ++-- src/docs/reference/modules/Liquid/README.md | 8 ++++++ 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Filters/SupportedCulturesFilter.cs diff --git a/src/OrchardCore.Modules/OrchardCore.ContentLocalization/Startup.cs b/src/OrchardCore.Modules/OrchardCore.ContentLocalization/Startup.cs index 337ca81f121..57cff9e2ba0 100644 --- a/src/OrchardCore.Modules/OrchardCore.ContentLocalization/Startup.cs +++ b/src/OrchardCore.Modules/OrchardCore.ContentLocalization/Startup.cs @@ -1,5 +1,4 @@ using System; -using System.Globalization; using Fluid; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; @@ -35,7 +34,6 @@ public override void ConfigureServices(IServiceCollection services) services.Configure(o => { o.MemberAccessStrategy.Register(); - o.MemberAccessStrategy.Register(); }) .AddLiquidFilter("localization_set"); diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Filters/SupportedCulturesFilter.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Filters/SupportedCulturesFilter.cs new file mode 100644 index 00000000000..42f0f6abb58 --- /dev/null +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Filters/SupportedCulturesFilter.cs @@ -0,0 +1,26 @@ +using System.Globalization; +using System.Linq; +using System.Threading.Tasks; +using Fluid; +using Fluid.Values; +using OrchardCore.Liquid; +using OrchardCore.Localization; + +namespace OrchardCore.DisplayManagement.Liquid.Filters; + +public class SupportedCulturesFilter : ILiquidFilter +{ + private readonly ILocalizationService _localizationService; + + public SupportedCulturesFilter(ILocalizationService localizationService) + { + _localizationService = localizationService; + } + + public async ValueTask ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext context) + { + var supportedCultures = await _localizationService.GetSupportedCulturesAsync(); + + return new ArrayValue(supportedCultures.Select(x => new ObjectValue(CultureInfo.GetCultureInfo(x))).ToArray()); + } +} diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs index c739f118d0c..83fc02159a0 100644 --- a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs @@ -68,6 +68,7 @@ public static OrchardCoreBuilder AddLiquidViews(this OrchardCoreBuilder builder) o.MemberAccessStrategy.Register("*", new ShapeAccessor()); o.MemberAccessStrategy.Register("*", new ShapeAccessor()); o.MemberAccessStrategy.Register(); + o.MemberAccessStrategy.Register(); o.Scope.SetValue("Culture", new ObjectValue(new LiquidCultureAccessor())); o.MemberAccessStrategy.Register((obj, name, ctx) => @@ -168,11 +169,11 @@ public static OrchardCoreBuilder AddLiquidViews(this OrchardCoreBuilder builder) o.MemberAccessStrategy.Register((cookies, name) => cookies.RequestCookieCollection[name]); o.MemberAccessStrategy.Register((headers, name) => headers.HeaderDictionary[name].ToArray()); o.MemberAccessStrategy.Register((headers, name) => headers.RouteValueDictionary[name]); - }) .AddLiquidFilter("append_version") .AddLiquidFilter("resource_url") - .AddLiquidFilter("sanitize_html"); + .AddLiquidFilter("sanitize_html") + .AddLiquidFilter("supported_cultures"); }); return builder; diff --git a/src/docs/reference/modules/Liquid/README.md b/src/docs/reference/modules/Liquid/README.md index 186e855e6d8..c94d388c775 100644 --- a/src/docs/reference/modules/Liquid/README.md +++ b/src/docs/reference/modules/Liquid/README.md @@ -507,6 +507,14 @@ The following properties are available on the `Culture` object. | `Name` | `en-US` | The request's culture as an ISO language code. | | `Dir` | `rtl` | The text writing direction. | +##### supported_cultures filter + +Returns the currently supported cultures. + +```liquid +{{ Culture | supported_cultures }} +``` + ### Environment Represents the current hosting environment. From 62c1e18992eda3c7e3d0b63766c3882a7005a528 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Fri, 31 May 2024 14:58:18 -0700 Subject: [PATCH 2/6] add release notes --- src/docs/releases/2.0.0.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/docs/releases/2.0.0.md b/src/docs/releases/2.0.0.md index 96198fd6f91..0953c16bdbe 100644 --- a/src/docs/releases/2.0.0.md +++ b/src/docs/releases/2.0.0.md @@ -535,3 +535,17 @@ Previously, achieving the same result required more code: Before this release, the `MarkdownField` used a single-line input field by default (named the Standard editor) and offered two different editors: Multi-line with a simple `textarea` and WYSIWYG with a rich editor. Now, by default, it uses a `textarea` as the Standard editor, and the separate Multi-line option has been removed. You have nothing to do, fields configured with the Standard or Multi-line editors previously will both continue to work. Note though, that their editors will now be a `textarea`. + +### Liquid + +New filter named `supported_cultures` was added to allow you to get a list of supported cultures. Here is an example of how to print the name of supported cultures using a list + +``` +{% assign cultures = Culture | supported_cultures %} + +
    +{% for culture in cultures %} +
  • {{ culture.Name }}
  • +{% endfor %} +
+``` From df59e49196c09baa6c3cdf2b1db60f3b406afdcb Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Mon, 3 Jun 2024 07:34:07 -0700 Subject: [PATCH 3/6] Adding more properties for the current culture. --- .../OrchardCoreBuilderExtensions.cs | 3 +++ src/docs/reference/modules/Liquid/README.md | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs index 83fc02159a0..225b46aa5f2 100644 --- a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs @@ -77,6 +77,9 @@ public static OrchardCoreBuilder AddLiquidViews(this OrchardCoreBuilder builder) { nameof(CultureInfo.Name) => new StringValue(CultureInfo.CurrentUICulture.Name), "Dir" => new StringValue(CultureInfo.CurrentUICulture.GetLanguageDirection()), + nameof(CultureInfo.NativeName) => new StringValue(CultureInfo.CurrentUICulture.NativeName), + nameof(CultureInfo.DisplayName) => new StringValue(CultureInfo.CurrentUICulture.DisplayName), + nameof(CultureInfo.TwoLetterISOLanguageName) => new StringValue(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName), _ => NilValue.Instance }; }); diff --git a/src/docs/reference/modules/Liquid/README.md b/src/docs/reference/modules/Liquid/README.md index c94d388c775..2c380496d4b 100644 --- a/src/docs/reference/modules/Liquid/README.md +++ b/src/docs/reference/modules/Liquid/README.md @@ -504,8 +504,11 @@ The following properties are available on the `Culture` object. | Property | Example | Description | | --------- | ---- |------------ | -| `Name` | `en-US` | The request's culture as an ISO language code. | +| `Name` | `en-US` | The ISO language code of the current culture. | | `Dir` | `rtl` | The text writing direction. | +| `DisplayName` | `English (United States)` | The display name of the current culture. | +| `NativeName` | `English (United States)` | The native name of the current culture. | +| `TwoLetterISOLanguageName` | `en` | The two-letter ISO language name of the current culture. | ##### supported_cultures filter From 3a8bd35e5b5a13807ae7c75a1ed99efd7168e362 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Mon, 3 Jun 2024 14:07:47 -0700 Subject: [PATCH 4/6] Update src/docs/releases/2.0.0.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Zoltán Lehóczky --- src/docs/releases/2.0.0.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/docs/releases/2.0.0.md b/src/docs/releases/2.0.0.md index 0953c16bdbe..79e53e3e44a 100644 --- a/src/docs/releases/2.0.0.md +++ b/src/docs/releases/2.0.0.md @@ -538,7 +538,9 @@ You have nothing to do, fields configured with the Standard or Multi-line editor ### Liquid -New filter named `supported_cultures` was added to allow you to get a list of supported cultures. Here is an example of how to print the name of supported cultures using a list +The `Culture` context variable got the new properties for a fuller Liquid culture support: `DisplayName`, `NativeName`, and `TwoLetterISOLanguageName`. These are the same as their .NET counterparts. + +A new filter named `supported_cultures` was added to allow you to get a list of supported cultures. Here is an example of how to print the names of supported cultures using a list: ``` {% assign cultures = Culture | supported_cultures %} From 5dd83241bebc2e3ef9c860176dd9a941e22824d7 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Mon, 3 Jun 2024 14:09:35 -0700 Subject: [PATCH 5/6] Update src/docs/reference/modules/Liquid/README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Zoltán Lehóczky --- src/docs/reference/modules/Liquid/README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/docs/reference/modules/Liquid/README.md b/src/docs/reference/modules/Liquid/README.md index 2c380496d4b..453e638711e 100644 --- a/src/docs/reference/modules/Liquid/README.md +++ b/src/docs/reference/modules/Liquid/README.md @@ -512,11 +512,15 @@ The following properties are available on the `Culture` object. ##### supported_cultures filter -Returns the currently supported cultures. +Returns the currently supported cultures. Here is an example of how to print the names of supported cultures using a list: ```liquid -{{ Culture | supported_cultures }} -``` +{% assign cultures = Culture | supported_cultures %} +
    +{% for culture in cultures %} +
  • {{ culture.Name }}
  • +{% endfor %} +
### Environment From 0237f88c210393de10a0504e93c50ae9d09a7847 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Mon, 3 Jun 2024 14:10:16 -0700 Subject: [PATCH 6/6] fixing note --- src/docs/reference/modules/Liquid/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/docs/reference/modules/Liquid/README.md b/src/docs/reference/modules/Liquid/README.md index 453e638711e..e1557bd02ef 100644 --- a/src/docs/reference/modules/Liquid/README.md +++ b/src/docs/reference/modules/Liquid/README.md @@ -521,6 +521,7 @@ Returns the currently supported cultures. Here is an example of how to print the
  • {{ culture.Name }}
  • {% endfor %} +``` ### Environment