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

Add supported_cultures liquid filter #16208

Merged
merged 8 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Globalization;
using Fluid;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -35,7 +34,6 @@ public override void ConfigureServices(IServiceCollection services)
services.Configure<TemplateOptions>(o =>
{
o.MemberAccessStrategy.Register<LocalizationPartViewModel>();
o.MemberAccessStrategy.Register<CultureInfo>();
})
.AddLiquidFilter<ContentLocalizationFilter>("localization_set");

Expand Down
Original file line number Diff line number Diff line change
@@ -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<FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext context)
{
var supportedCultures = await _localizationService.GetSupportedCulturesAsync();

return new ArrayValue(supportedCultures.Select(x => new ObjectValue(CultureInfo.GetCultureInfo(x))).ToArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static OrchardCoreBuilder AddLiquidViews(this OrchardCoreBuilder builder)
o.MemberAccessStrategy.Register<Shape>("*", new ShapeAccessor());
o.MemberAccessStrategy.Register<ZoneHolding>("*", new ShapeAccessor());
o.MemberAccessStrategy.Register<ShapeMetadata>();
o.MemberAccessStrategy.Register<CultureInfo>();

o.Scope.SetValue("Culture", new ObjectValue(new LiquidCultureAccessor()));
o.MemberAccessStrategy.Register<LiquidCultureAccessor, FluidValue>((obj, name, ctx) =>
Expand All @@ -76,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
};
});
Expand Down Expand Up @@ -168,11 +172,11 @@ public static OrchardCoreBuilder AddLiquidViews(this OrchardCoreBuilder builder)
o.MemberAccessStrategy.Register<CookieCollectionWrapper, string>((cookies, name) => cookies.RequestCookieCollection[name]);
o.MemberAccessStrategy.Register<HeaderDictionaryWrapper, string[]>((headers, name) => headers.HeaderDictionary[name].ToArray());
o.MemberAccessStrategy.Register<RouteValueDictionaryWrapper, object>((headers, name) => headers.RouteValueDictionary[name]);

})
.AddLiquidFilter<AppendVersionFilter>("append_version")
.AddLiquidFilter<ResourceUrlFilter>("resource_url")
.AddLiquidFilter<SanitizeHtmlFilter>("sanitize_html");
.AddLiquidFilter<SanitizeHtmlFilter>("sanitize_html")
.AddLiquidFilter<SupportedCulturesFilter>("supported_cultures");
});

return builder;
Expand Down
18 changes: 17 additions & 1 deletion src/docs/reference/modules/Liquid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,24 @@ 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

Returns the currently supported cultures. Here is an example of how to print the names of supported cultures using a list:

```liquid
{% assign cultures = Culture | supported_cultures %}
<ul>
{% for culture in cultures %}
<li>{{ culture.Name }}</li>
{% endfor %}
</ul>
```

### Environment

Expand Down
16 changes: 16 additions & 0 deletions src/docs/releases/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,19 @@ 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

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 %}

<ul>
{% for culture in cultures %}
<li>{{ culture.Name }}</li>
{% endfor %}
</ul>
```