7
7
import javax .annotation .Nullable ;
8
8
import org .opentripplanner .framework .i18n .I18NString ;
9
9
10
+ /**
11
+ * This class should always be used when translating fields or handling locales in GraphQL queries.
12
+ */
10
13
public class GraphQLUtils {
11
14
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
12
34
public static String getTranslation (
13
35
@ Nullable I18NString input ,
14
36
DataFetchingEnvironment environment
@@ -19,6 +41,23 @@ public static String getTranslation(
19
41
return input .toString (getLocale (environment ));
20
42
}
21
43
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
+ */
22
61
public static Locale getLocale (DataFetchingEnvironment environment ) {
23
62
var localeString = environment .getArgument ("language" );
24
63
if (localeString != null ) {
@@ -28,6 +67,23 @@ public static Locale getLocale(DataFetchingEnvironment environment) {
28
67
return getLocaleFromEnvironment (environment );
29
68
}
30
69
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
+ */
31
87
public static Locale getLocale (
32
88
DataFetchingEnvironment environment ,
33
89
@ Nullable String localeString
@@ -39,6 +95,23 @@ public static Locale getLocale(
39
95
return getLocaleFromEnvironment (environment );
40
96
}
41
97
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
+ */
42
115
public static Locale getLocale (DataFetchingEnvironment environment , @ Nullable Locale locale ) {
43
116
if (locale != null ) {
44
117
return locale ;
@@ -47,15 +120,33 @@ public static Locale getLocale(DataFetchingEnvironment environment, @Nullable Lo
47
120
return getLocaleFromEnvironment (environment );
48
121
}
49
122
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
+ */
50
137
public static Locale getLocaleFromEnvironment (DataFetchingEnvironment environment ) {
51
138
// This can come from the accept-language header
52
139
var envLocale = environment .getLocale ();
53
140
// This can come from a locale param
54
- var localContextLocale = getDefaultLocale (environment );
141
+ var localContextLocale = getLocalContextLocale (environment );
55
142
return localContextLocale .orElse (envLocale );
56
143
}
57
144
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 ) {
59
150
Map <String , ?> localContext = environment .getLocalContext ();
60
151
if (localContext == null ) {
61
152
return Optional .empty ();
0 commit comments