Skip to content

Commit

Permalink
Improve Liquid Culture accessor (#16956)
Browse files Browse the repository at this point in the history
Co-authored-by: Sébastien Ros <[email protected]>
  • Loading branch information
hishamco and sebastienros authored Nov 7, 2024
1 parent 5a14fb7 commit e3cd15c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace OrchardCore.Liquid;
/// <summary>
/// This is a placeholder class that allows modules to extend the `Culture` property in the current Liquid scope.
/// </summary>
[Obsolete("This class is obsolete and will be removed in a future version. Use the `Culture` property in the current Liquid scope instead.", error: true)]
public class LiquidCultureAccessor
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using OrchardCore.DisplayManagement.Liquid.Filters;
using OrchardCore.DisplayManagement.Liquid.TagHelpers;
using OrchardCore.DisplayManagement.Liquid.Tags;
using OrchardCore.DisplayManagement.Liquid.Values;
using OrchardCore.DisplayManagement.Razor;
using OrchardCore.DisplayManagement.Shapes;
using OrchardCore.DisplayManagement.Zones;
Expand Down Expand Up @@ -68,19 +69,7 @@ public static OrchardCoreBuilder AddLiquidViews(this OrchardCoreBuilder builder)
o.MemberAccessStrategy.Register<ShapeMetadata>();
o.MemberAccessStrategy.Register<CultureInfo>();

o.Scope.SetValue("Culture", new ObjectValue(new LiquidCultureAccessor()));
o.MemberAccessStrategy.Register<LiquidCultureAccessor, FluidValue>((obj, name, ctx) =>
{
return name switch
{
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
};
});
o.Scope.SetValue("Culture", new CultureValue());

o.Scope.SetValue("Environment", new ObjectValue(new LiquidEnvironmentAccessor()));
o.MemberAccessStrategy.Register<LiquidEnvironmentAccessor, FluidValue>((obj, name, ctx) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Globalization;
using System.Text.Encodings.Web;
using Fluid;
using Fluid.Values;
using OrchardCore.Localization;

namespace OrchardCore.DisplayManagement.Liquid.Values;

internal sealed class CultureValue : FluidValue
{
public override FluidValues Type => FluidValues.Object;

public override bool Equals(FluidValue other)
{
if (other is null)
{
return false;
}

return ToStringValue() == other.ToStringValue();
}

public override bool ToBooleanValue() => false;

public override decimal ToNumberValue() => 0;

public override object ToObjectValue() => ToStringValue();

public override string ToStringValue() => CultureInfo.CurrentUICulture.Name;

#pragma warning disable CS0672 // Member overrides obsolete member
public override void WriteTo(TextWriter writer, TextEncoder encoder, CultureInfo cultureInfo)
#pragma warning restore CS0672 // Member overrides obsolete member
=> writer.Write(ToStringValue());

public async override ValueTask WriteToAsync(TextWriter writer, TextEncoder encoder, CultureInfo cultureInfo)
=> await writer.WriteAsync(CultureInfo.CurrentUICulture.Name);

protected override FluidValue GetValue(string name, TemplateContext context)
=> name switch
{
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
};
}

0 comments on commit e3cd15c

Please sign in to comment.