4
4
import java .util .Locale ;
5
5
import java .util .Map ;
6
6
import java .util .Optional ;
7
+ import javax .annotation .Nullable ;
7
8
import org .opentripplanner .framework .i18n .I18NString ;
8
9
10
+ /**
11
+ * This class should always be used when translating fields or handling locales in GraphQL queries.
12
+ */
9
13
public class GraphQLUtils {
10
14
11
- public static String getTranslation (I18NString input , DataFetchingEnvironment environment ) {
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
34
+ public static String getTranslation (
35
+ @ Nullable I18NString input ,
36
+ DataFetchingEnvironment environment
37
+ ) {
12
38
if (input == null ) {
13
39
return null ;
14
40
}
15
41
return input .toString (getLocale (environment ));
16
42
}
17
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
+ */
18
61
public static Locale getLocale (DataFetchingEnvironment environment ) {
19
62
var localeString = environment .getArgument ("language" );
20
63
if (localeString != null ) {
@@ -24,47 +67,90 @@ public static Locale getLocale(DataFetchingEnvironment environment) {
24
67
return getLocaleFromEnvironment (environment );
25
68
}
26
69
27
- public static Locale getLocale (DataFetchingEnvironment environment , String localeString ) {
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
+ */
87
+ public static Locale getLocale (
88
+ DataFetchingEnvironment environment ,
89
+ @ Nullable String localeString
90
+ ) {
28
91
if (localeString != null ) {
29
92
return Locale .forLanguageTag (localeString );
30
93
}
31
94
32
95
return getLocaleFromEnvironment (environment );
33
96
}
34
97
35
- public static Locale getLocale (DataFetchingEnvironment environment , Locale locale ) {
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
+ */
115
+ public static Locale getLocale (DataFetchingEnvironment environment , @ Nullable Locale locale ) {
36
116
if (locale != null ) {
37
117
return locale ;
38
118
}
39
119
40
120
return getLocaleFromEnvironment (environment );
41
121
}
42
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
+ */
43
137
public static Locale getLocaleFromEnvironment (DataFetchingEnvironment environment ) {
44
138
// 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 ;
139
+ var envLocale = environment .getLocale ();
140
+ // This can come from a locale param
141
+ var localContextLocale = getLocalContextLocale (environment );
142
+ return localContextLocale .orElse (envLocale );
57
143
}
58
144
59
- 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 ) {
60
150
Map <String , ?> localContext = environment .getLocalContext ();
61
151
if (localContext == null ) {
62
152
return Optional .empty ();
63
153
}
64
154
return Optional .ofNullable ((Locale ) localContext .get ("locale" ));
65
155
}
66
-
67
- private static boolean acceptAnyLocale (Locale locale ) {
68
- return locale .getLanguage ().equals ("*" );
69
- }
70
156
}
0 commit comments