@@ -22,6 +22,11 @@ class LanguageNegotiator
2222 */
2323 private $ request ;
2424
25+ /**
26+ * @var bool
27+ */
28+ private $ use_intl = false ;
29+
2530 /**
2631 * @param string $defaultLocale
2732 * @param array $supportedLanguages
@@ -30,7 +35,26 @@ class LanguageNegotiator
3035 public function __construct ($ defaultLocale , $ supportedLanguages , Request $ request )
3136 {
3237 $ this ->defaultLocale = $ defaultLocale ;
33- $ this ->supportedLanguages = $ supportedLanguages ;
38+
39+ if (class_exists ('Locale ' )) {
40+ $ this ->use_intl = true ;
41+
42+ foreach ($ supportedLanguages as $ key => $ supportedLanguage ) {
43+ if ( ! isset ($ supportedLanguage ['lang ' ])) {
44+ $ supportedLanguage ['lang ' ] = Locale::canonicalize ($ key );
45+ } else {
46+ $ supportedLanguage ['lang ' ] = Locale::canonicalize ($ supportedLanguage ['lang ' ]);
47+ }
48+ if (isset ($ supportedLanguage ['regional ' ])) {
49+ $ supportedLanguage ['regional ' ] = Locale::canonicalize ($ supportedLanguage ['regional ' ]);
50+ }
51+
52+ $ this ->supportedLanguages [$ key ] = $ supportedLanguage ;
53+ }
54+ } else {
55+ $ this ->supportedLanguages = $ supportedLanguages ;
56+ }
57+
3458 $ this ->request = $ request ;
3559 }
3660
@@ -57,7 +81,11 @@ public function negotiateLanguage()
5781 if (!empty ($ this ->supportedLanguages [$ key ])) {
5882 return $ key ;
5983 }
60-
84+
85+ if ($ this ->use_intl ) {
86+ $ key = Locale::canonicalize ($ key );
87+ }
88+
6189 // Search for acceptable locale by 'regional' => 'af_ZA' or 'lang' => 'af-ZA' match.
6290 foreach ( $ this ->supportedLanguages as $ key_supported => $ locale ) {
6391 if ( (isset ($ locale ['regional ' ]) && $ locale ['regional ' ] == $ key ) || (isset ($ locale ['lang ' ]) && $ locale ['lang ' ] == $ key ) ) {
@@ -72,7 +100,7 @@ public function negotiateLanguage()
72100 return key ($ this ->supportedLanguages );
73101 }
74102
75- if (class_exists ( ' Locale ' ) && !empty ($ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ])) {
103+ if ($ this -> use_intl && !empty ($ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ])) {
76104 $ http_accept_language = Locale::acceptFromHttp ($ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ]);
77105
78106 if (!empty ($ this ->supportedLanguages [$ http_accept_language ])) {
0 commit comments