-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Liquid Culture accessor (#16956)
Co-authored-by: Sébastien Ros <[email protected]>
- Loading branch information
1 parent
5a14fb7
commit e3cd15c
Showing
3 changed files
with
52 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Values/CultureValue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} |