From e3cd15c5991785076629f966fd34c03982e32c4f Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 7 Nov 2024 23:32:02 +0300 Subject: [PATCH] Improve Liquid Culture accessor (#16956) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Ros --- .../LiquidCultureAccessor.cs | 1 + .../OrchardCoreBuilderExtensions.cs | 15 +----- .../Values/CultureValue.cs | 49 +++++++++++++++++++ 3 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/CultureValue.cs diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/LiquidCultureAccessor.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/LiquidCultureAccessor.cs index 5910f332a0f..8ca02563f38 100644 --- a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/LiquidCultureAccessor.cs +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/LiquidCultureAccessor.cs @@ -3,6 +3,7 @@ namespace OrchardCore.Liquid; /// /// This is a placeholder class that allows modules to extend the `Culture` property in the current Liquid scope. /// +[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 { } diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs index 948b37b4591..00c7f8e933a 100644 --- a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/OrchardCoreBuilderExtensions.cs @@ -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; @@ -68,19 +69,7 @@ public static OrchardCoreBuilder AddLiquidViews(this OrchardCoreBuilder builder) o.MemberAccessStrategy.Register(); o.MemberAccessStrategy.Register(); - o.Scope.SetValue("Culture", new ObjectValue(new LiquidCultureAccessor())); - o.MemberAccessStrategy.Register((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((obj, name, ctx) => diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/CultureValue.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/CultureValue.cs new file mode 100644 index 00000000000..70ab899ec22 --- /dev/null +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/CultureValue.cs @@ -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 + }; +}