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

Dotliquid Ignored CultureInfo settings if CultureInfo.CurrentCulture is InvariantCulture #536

Open
heggi opened this issue Nov 30, 2023 · 1 comment

Comments

@heggi
Copy link

heggi commented Nov 30, 2023

Dotliquid version

2.2.692

Expected behavior

Correct format dates via date filter

Actual behavior

Date format ignored, dates format as invariant full date

Steps to reproduce the Problem (you can add files)

CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

var rendered = DotLiquid.Template
    .Parse("{% param culture='fr-FR'%} {{ MyDate | date: \"dd.MMMM.yyyy\" }}")
    .Render(DotLiquid.Hash.FromAnonymousObject(new
    {
        MyDate = DateTimeOffset.UtcNow,
    }));

Console.WriteLine(rendered);

rendered value must be 01.décembre.2023, but i got 11/30/2023 22:16:48 +00:00

If comment out CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; or replace CurrentCulture via any other Culture (for example en-gb), code works fine.

Next code don't work as expected too

CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

var rendered = DotLiquid.Template
    .Parse("{{ MyDate | date: \"dd.MMMM.yyyy\" }}")
    .Render(DotLiquid.Hash.FromAnonymousObject(new
    {
        MyDate = DateTimeOffset.UtcNow,
    }), CultureInfo.GetCultureInfo("fr-FR"));

Console.WriteLine(rendered);
@microalps
Copy link
Contributor

microalps commented Mar 4, 2024

It seems like the filter is not working, such as using C# syntax. Did you try uppercase Date? In both invariant culture and French, dot separators are the same. Here is a test case proving this working correctly:

[Test]
public void TestCultureDate()
{
    var Dec0123 = new DateTime(2023, 12, 1);
    using (CultureHelper.SetCulture("en-US")) // Pre-select a thread culture for Before value (jp-JP)
    {
        Helper.AssertTemplateResult(
            expected: "01.décembre.2023",
            template: "{% param culture=cultureValue%}{{ MyDate | date: \"dd.MMMM.yyyy\"}}",
            localVariables: Hash.FromAnonymousObject(new { MyDate = Dec0123, cultureValue = "fr-FR" })
        );

        Helper.AssertTemplateResult(
            expected: "01.December.2023",
            template: "{% param culture=cultureValue%}{{ MyDate | date: \"dd.MMMM.yyyy\"}}",
            localVariables: Hash.FromAnonymousObject(new { MyDate = Dec0123, cultureValue = "" }) // ""=InvariantCulture
        );
    }

    using (CultureHelper.SetCulture("")) // Pre-select an invariant culture
    {
        Helper.AssertTemplateResult(
            expected: "01.décembre.2023",
            template: "{% param culture=cultureValue%}{{ MyDate | date: \"dd.MMMM.yyyy\"}}",
            localVariables: Hash.FromAnonymousObject(new { MyDate = Dec0123, cultureValue = "fr-FR" })
        );
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants