1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
29
29
import org .springframework .web .servlet .LocaleResolver ;
30
30
31
31
/**
32
- * {@link LocaleResolver} implementation that simply uses the primary locale
33
- * specified in the {@code Accept-Language} header of the HTTP request (that is,
34
- * the locale sent by the client browser, normally that of the client's OS) .
32
+ * {@link LocaleResolver} implementation that looks for a match between locales
33
+ * in the {@code Accept-Language} header and a list of configured supported
34
+ * locales .
35
35
*
36
- * <p>Note: Does not support {@link #setLocale} since the {@code Accept-Language}
37
- * header can only be changed by changing the client's locale settings.
36
+ * <p>See {@link #setSupportedLocales(List)} for further details on how
37
+ * supported and requested locales are matched.
38
+ *
39
+ * <p>Note: This implementation does not support {@link #setLocale} since the
40
+ * {@code Accept-Language} header can only be changed by changing the client's
41
+ * locale settings.
38
42
*
39
43
* @author Juergen Hoeller
40
44
* @author Rossen Stoyanchev
@@ -47,9 +51,20 @@ public class AcceptHeaderLocaleResolver extends AbstractLocaleResolver {
47
51
48
52
49
53
/**
50
- * Configure supported locales to check against the requested locales
51
- * determined via {@link HttpServletRequest#getLocales()}. If this is not
52
- * configured then {@link HttpServletRequest#getLocale()} is used instead.
54
+ * Configure the list of supported locales to compare and match against
55
+ * {@link HttpServletRequest#getLocales() requested locales}.
56
+ * <p>In order for a supported locale to be considered a match, it must match
57
+ * on both country and language. If you want to support a language-only match
58
+ * as a fallback, you must configure the language explicitly as a supported
59
+ * locale.
60
+ * <p>For example, if the supported locales are {@code ["de-DE","en-US"]},
61
+ * then a request for {@code "en-GB"} will not match, and neither will a
62
+ * request for {@code "en"}. If you want to support additional locales for a
63
+ * given language such as {@code "en"}, then you must add it to the list of
64
+ * supported locales.
65
+ * <p>If there is no match, then the {@link #setDefaultLocale(Locale)
66
+ * defaultLocale} is used, if configured, or otherwise falling back on
67
+ * {@link HttpServletRequest#getLocale()}.
53
68
* @param locales the supported locales
54
69
* @since 4.3
55
70
*/
@@ -99,10 +114,10 @@ private Locale findSupportedLocale(HttpServletRequest request, List<Locale> supp
99
114
}
100
115
else if (languageMatch == null ) {
101
116
// Let's try to find a language-only match as a fallback
102
- for (Locale candidate : supportedLocales ) {
103
- if (!StringUtils .hasLength (candidate .getCountry ()) &&
104
- candidate .getLanguage ().equals (locale .getLanguage ())) {
105
- languageMatch = candidate ;
117
+ for (Locale supportedLocale : supportedLocales ) {
118
+ if (!StringUtils .hasLength (supportedLocale .getCountry ()) &&
119
+ supportedLocale .getLanguage ().equals (locale .getLanguage ())) {
120
+ languageMatch = supportedLocale ;
106
121
break ;
107
122
}
108
123
}
0 commit comments