Skip to content

Commit 6013f69

Browse files
committed
Add javadoc
1 parent c4616ba commit 6013f69

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed

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

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,30 @@
77
import javax.annotation.Nullable;
88
import org.opentripplanner.framework.i18n.I18NString;
99

10+
/**
11+
* This class should always be used when translating fields or handling locales in GraphQL queries.
12+
*/
1013
public class GraphQLUtils {
1114

15+
/**
16+
* If input is {@code null}, return null. Otherwise, input is translated using a locale from this
17+
* prioritized list:
18+
* <ol>
19+
* <li>
20+
* {@code language} parameter of a queried field.
21+
* </li>
22+
* <li>
23+
* Locale from the DataFetchingEnvironment's local context (for journey planning queries this is {@code locale} parameter).
24+
* </li>
25+
* <li>
26+
* DataFetchingEnvironment's locale which comes from the accept-language header.
27+
* </li>
28+
* <li>
29+
* Default locale.
30+
* </li>
31+
* </ol>
32+
*/
33+
@Nullable
1234
public static String getTranslation(
1335
@Nullable I18NString input,
1436
DataFetchingEnvironment environment
@@ -19,6 +41,23 @@ public static String getTranslation(
1941
return input.toString(getLocale(environment));
2042
}
2143

44+
/**
45+
* Returns locale from this prioritized list:
46+
* <ol>
47+
* <li>
48+
* {@code language} parameter of a queried field.
49+
* </li>
50+
* <li>
51+
* Locale from the DataFetchingEnvironment's local context (for journey planning queries this is {@code locale} parameter).
52+
* </li>
53+
* <li>
54+
* DataFetchingEnvironment's locale which comes from the accept-language header.
55+
* </li>
56+
* <li>
57+
* Default locale.
58+
* </li>
59+
* </ol>
60+
*/
2261
public static Locale getLocale(DataFetchingEnvironment environment) {
2362
var localeString = environment.getArgument("language");
2463
if (localeString != null) {
@@ -28,6 +67,23 @@ public static Locale getLocale(DataFetchingEnvironment environment) {
2867
return getLocaleFromEnvironment(environment);
2968
}
3069

70+
/**
71+
* Returns locale from this prioritized list:
72+
* <ol>
73+
* <li>
74+
* {@code localeString}.
75+
* </li>
76+
* <li>
77+
* Locale from the DataFetchingEnvironment's local context (for journey planning queries this is {@code locale} parameter).
78+
* </li>
79+
* <li>
80+
* DataFetchingEnvironment's locale which comes from the accept-language header.
81+
* </li>
82+
* <li>
83+
* Default locale.
84+
* </li>
85+
* </ol>
86+
*/
3187
public static Locale getLocale(
3288
DataFetchingEnvironment environment,
3389
@Nullable String localeString
@@ -39,6 +95,23 @@ public static Locale getLocale(
3995
return getLocaleFromEnvironment(environment);
4096
}
4197

98+
/**
99+
* Returns locale from this prioritized list:
100+
* <ol>
101+
* <li>
102+
* {@code locale}.
103+
* </li>
104+
* <li>
105+
* Locale from the DataFetchingEnvironment's local context (for journey planning queries this is {@code locale} parameter).
106+
* </li>
107+
* <li>
108+
* DataFetchingEnvironment's locale which comes from the accept-language header.
109+
* </li>
110+
* <li>
111+
* Default locale.
112+
* </li>
113+
* </ol>
114+
*/
42115
public static Locale getLocale(DataFetchingEnvironment environment, @Nullable Locale locale) {
43116
if (locale != null) {
44117
return locale;
@@ -47,15 +120,33 @@ public static Locale getLocale(DataFetchingEnvironment environment, @Nullable Lo
47120
return getLocaleFromEnvironment(environment);
48121
}
49122

123+
/**
124+
* Returns locale from this prioritized list:
125+
* <ol>
126+
* <li>
127+
* Locale from the DataFetchingEnvironment's local context (for journey planning queries this is {@code locale} parameter).
128+
* </li>
129+
* <li>
130+
* DataFetchingEnvironment's locale which comes from the accept-language header.
131+
* </li>
132+
* <li>
133+
* Default locale.
134+
* </li>
135+
* </ol>
136+
*/
50137
public static Locale getLocaleFromEnvironment(DataFetchingEnvironment environment) {
51138
// This can come from the accept-language header
52139
var envLocale = environment.getLocale();
53140
// This can come from a locale param
54-
var localContextLocale = getDefaultLocale(environment);
141+
var localContextLocale = getLocalContextLocale(environment);
55142
return localContextLocale.orElse(envLocale);
56143
}
57144

58-
private static Optional<Locale> getDefaultLocale(DataFetchingEnvironment environment) {
145+
/**
146+
* Returns locale from the DataFetchingEnvironment's local context (for journey planning queries
147+
* this is {@code locale} parameter) or empty if none exist.
148+
*/
149+
private static Optional<Locale> getLocalContextLocale(DataFetchingEnvironment environment) {
59150
Map<String, ?> localContext = environment.getLocalContext();
60151
if (localContext == null) {
61152
return Optional.empty();

0 commit comments

Comments
 (0)