Skip to content

Commit

Permalink
Merge pull request #1109 from HubSpot/use-interpreter-locale-default
Browse files Browse the repository at this point in the history
Use interpreter locale as default for strftime functions
  • Loading branch information
mattcoley authored Aug 22, 2023
2 parents 3151167 + 441a03c commit ddcc97d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit ddcc97d

Please sign in to comment.