Skip to content

Commit

Permalink
Add supported_cultures liquid filter
Browse files Browse the repository at this point in the history
Fix #16202
  • Loading branch information
MikeAlhayek committed May 31, 2024
1 parent 8ef3a02 commit b7619ad
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
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 Down Expand Up @@ -168,11 +169,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
8 changes: 8 additions & 0 deletions src/docs/reference/modules/Liquid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b7619ad

Please sign in to comment.