From 441a03cf68b49e399ecdf13c391229595c98562d Mon Sep 17 00:00:00 2001 From: Matthew Coley Date: Tue, 22 Aug 2023 11:36:31 -0400 Subject: [PATCH] Use interpreter locale as default for strftime functions --- .../jinjava/objects/date/StrftimeFormatter.java | 12 +++++++++++- .../objects/date/StrftimeFormatterTest.java | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/hubspot/jinjava/objects/date/StrftimeFormatter.java b/src/main/java/com/hubspot/jinjava/objects/date/StrftimeFormatter.java index 8480e2f1f..0f4a2da45 100644 --- a/src/main/java/com/hubspot/jinjava/objects/date/StrftimeFormatter.java +++ b/src/main/java/com/hubspot/jinjava/objects/date/StrftimeFormatter.java @@ -4,6 +4,8 @@ import static com.hubspot.jinjava.objects.date.StrftimeFormatter.ConversionComponent.pattern; import com.google.common.collect.ImmutableMap; +import com.hubspot.jinjava.JinjavaConfig; +import com.hubspot.jinjava.interpret.JinjavaInterpreter; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; @@ -158,7 +160,15 @@ public static String format(ZonedDateTime d, Locale locale) { } public static String format(ZonedDateTime d, String strftime) { - return format(d, strftime, Locale.ENGLISH); + return format( + d, + strftime, + JinjavaInterpreter + .getCurrentMaybe() + .map(JinjavaInterpreter::getConfig) + .map(JinjavaConfig::getLocale) + .orElse(Locale.ENGLISH) + ); } public static String format(ZonedDateTime d, String strftime, Locale locale) { diff --git a/src/test/java/com/hubspot/jinjava/objects/date/StrftimeFormatterTest.java b/src/test/java/com/hubspot/jinjava/objects/date/StrftimeFormatterTest.java index acfdff5db..0f4fd9651 100644 --- a/src/test/java/com/hubspot/jinjava/objects/date/StrftimeFormatterTest.java +++ b/src/test/java/com/hubspot/jinjava/objects/date/StrftimeFormatterTest.java @@ -3,6 +3,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import com.hubspot.jinjava.Jinjava; +import com.hubspot.jinjava.JinjavaConfig; +import com.hubspot.jinjava.interpret.JinjavaInterpreter; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Locale; @@ -146,4 +149,17 @@ public void itAllowsLiteralCharacters() { assertThat(StrftimeFormatter.format(d, "1: day %d month %B")) .isEqualTo("1: day 06 month November"); } + + @Test + public void itUsesInterpreterLocaleAsDefault() { + try { + Jinjava jinjava = new Jinjava( + JinjavaConfig.newBuilder().withLocale(Locale.FRENCH).build() + ); + JinjavaInterpreter.pushCurrent(jinjava.newInterpreter()); + assertThat(StrftimeFormatter.format(d, "%B %-d, %Y")).isEqualTo("novembre 6, 2013"); + } finally { + JinjavaInterpreter.popCurrent(); + } + } }