Skip to content

Commit c4616ba

Browse files
committed
Use locale param if one is defined over accept-language header
1 parent c2355db commit c4616ba

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

application/src/main/java/org/opentripplanner/framework/graphql/GraphQLUtils.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import java.util.Locale;
55
import java.util.Map;
66
import java.util.Optional;
7+
import javax.annotation.Nullable;
78
import org.opentripplanner.framework.i18n.I18NString;
89

910
public class GraphQLUtils {
1011

11-
public static String getTranslation(I18NString input, DataFetchingEnvironment environment) {
12+
public static String getTranslation(
13+
@Nullable I18NString input,
14+
DataFetchingEnvironment environment
15+
) {
1216
if (input == null) {
1317
return null;
1418
}
@@ -24,15 +28,18 @@ public static Locale getLocale(DataFetchingEnvironment environment) {
2428
return getLocaleFromEnvironment(environment);
2529
}
2630

27-
public static Locale getLocale(DataFetchingEnvironment environment, String localeString) {
31+
public static Locale getLocale(
32+
DataFetchingEnvironment environment,
33+
@Nullable String localeString
34+
) {
2835
if (localeString != null) {
2936
return Locale.forLanguageTag(localeString);
3037
}
3138

3239
return getLocaleFromEnvironment(environment);
3340
}
3441

35-
public static Locale getLocale(DataFetchingEnvironment environment, Locale locale) {
42+
public static Locale getLocale(DataFetchingEnvironment environment, @Nullable Locale locale) {
3643
if (locale != null) {
3744
return locale;
3845
}
@@ -42,18 +49,10 @@ public static Locale getLocale(DataFetchingEnvironment environment, Locale local
4249

4350
public static Locale getLocaleFromEnvironment(DataFetchingEnvironment environment) {
4451
// This can come from the accept-language header
45-
var userLocale = environment.getLocale();
46-
var defaultLocale = getDefaultLocale(environment);
47-
48-
if (userLocale == null) {
49-
return defaultLocale.orElse(Locale.forLanguageTag("*"));
50-
}
51-
52-
if (defaultLocale.isPresent() && acceptAnyLocale(userLocale)) {
53-
return defaultLocale.get();
54-
}
55-
56-
return userLocale;
52+
var envLocale = environment.getLocale();
53+
// This can come from a locale param
54+
var localContextLocale = getDefaultLocale(environment);
55+
return localContextLocale.orElse(envLocale);
5756
}
5857

5958
private static Optional<Locale> getDefaultLocale(DataFetchingEnvironment environment) {
@@ -63,8 +62,4 @@ private static Optional<Locale> getDefaultLocale(DataFetchingEnvironment environ
6362
}
6463
return Optional.ofNullable((Locale) localContext.get("locale"));
6564
}
66-
67-
private static boolean acceptAnyLocale(Locale locale) {
68-
return locale.getLanguage().equals("*");
69-
}
7065
}

application/src/test/java/org/opentripplanner/framework/graphql/GraphQLUtilsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ void testGetLocaleWithDefinedLocaleArg() {
7979
void testGetLocaleWithEnvLocale() {
8080
var frenchLocale = Locale.FRENCH;
8181
var env = DataFetchingEnvironmentImpl.newDataFetchingEnvironment(executionContext)
82-
.localContext(Map.of("locale", Locale.GERMAN))
83-
.locale(frenchLocale)
82+
.locale(Locale.FRENCH)
8483
.build();
8584

8685
var locale = GraphQLUtils.getLocale(env);
@@ -90,10 +89,11 @@ void testGetLocaleWithEnvLocale() {
9089

9190
@Test
9291
void testGetLocaleWithLocalContextLocale() {
93-
// Should use locale from local context if env locale is not defined
92+
// Should use locale from local context even if env locale is defined
9493

9594
var frenchLocale = Locale.FRENCH;
9695
var envWithNoLocale = DataFetchingEnvironmentImpl.newDataFetchingEnvironment(executionContext)
96+
.locale(Locale.GERMAN)
9797
.localContext(Map.of("locale", Locale.FRENCH))
9898
.build();
9999

0 commit comments

Comments
 (0)