1010namespace Lunetics \LocaleBundle \LocaleGuesser ;
1111
1212use Symfony \Component \HttpFoundation \Request ;
13+ use Lunetics \LocaleBundle \Validator \MetaValidator ;
1314
1415/**
1516 * Locale Guesser for detecing the locale from the browser Accept-language string
1920 */
2021class BrowserLocaleGuesser implements LocaleGuesserInterface
2122{
22- private $ allowedLocales ;
23-
23+ /**
24+ * @var string
25+ */
2426 private $ identifiedLocale ;
2527
28+ /**
29+ * @var bool
30+ */
2631 private $ intlExtension ;
2732
33+ /**
34+ * @var MetaValidator
35+ */
36+ private $ metaValidator ;
37+
2838 /**
2939 * Constructor
3040 *
31- * @param array $allowedLocales Array of allowed locales
32- * @param bool $intlExtensionInstalled Wether the intl extension is installed
41+ * @param MetaValidator $metaValidator MetaValidator
42+ * @param bool $intlExtensionInstalled Wether the intl extension is installed
3343 */
34- public function __construct (array $ allowedLocales , $ intlExtensionInstalled = false )
44+ public function __construct (MetaValidator $ metaValidator , $ intlExtensionInstalled = false )
3545 {
36- $ this ->allowedLocales = $ allowedLocales ;
46+ $ this ->metaValidator = $ metaValidator ;
3747 $ this ->intlExtension = $ intlExtensionInstalled ;
3848 }
3949
4050 /**
41- * Guess the locale based on browser settings
51+ * Guess the locale based on the browser settings
4252 *
4353 * @param Request $request
4454 *
4555 * @return boolean
4656 */
4757 public function guessLocale (Request $ request )
4858 {
59+ $ validator = $ this ->metaValidator ;
4960 // Get the preferred locale from the Browser.
5061 $ preferredLocale = $ request ->getPreferredLanguage ();
5162 $ availableLocales = $ request ->getLanguages ();
52- $ allowedLocales = $ this ->allowedLocales ;
53-
5463
5564 if (!$ preferredLocale OR count ($ availableLocales ) === 0 ) {
5665 return false ;
5766 }
5867
5968 // If the preferred primary locale is allowed, return the locale.
60- if (in_array ($ preferredLocale, $ allowedLocales )) {
69+ if ($ validator -> isAllowed ($ preferredLocale )) {
6170 $ this ->identifiedLocale = $ preferredLocale ;
6271
6372 return true ;
6473 }
6574
66- if ($ this ->intlExtension ) {
67- $ primaryLanguage = \Locale::getPrimaryLanguage ($ preferredLocale );
68- } else {
69- $ primaryLanguage = $ this ->getPrimaryLanguage ($ preferredLocale );
70- }
71-
72- if (!in_array ($ primaryLanguage , $ allowedLocales )) {
73-
74- // Try to find a full locale (Language + country)
75- $ matchLocale = function ($ v ) use ($ allowedLocales ) {
76- if (in_array ($ v , $ allowedLocales )) {
77- return true ;
78- }
79-
80- return false ;
81- };
75+ /**$primaryLanguage = $this->getPrimaryLanguage($preferredLocale);
76+ if ($validator->isAllowed($primaryLanguage)) {
77+ $this->identifiedLocale = $preferredLocale;
8278
83- $ result = array_values (array_filter ($ availableLocales , $ matchLocale ));
79+ return true;
80+ }**/
8481
85- if (!empty ($ result )) {
86- $ this ->identifiedLocale = $ result [0 ];
82+ // Get a list of available and allowed locales and return the first result
83+ $ matchLocale = function ($ v ) use ($ validator ) {
84+ return $ validator ->isAllowed ($ v );
85+ };
8786
88- return true ;
89- }
90-
91- // Try to find a language
92- $ availableLanguages = $ this ->compileLanguageArray ($ availableLocales );
93- $ result = array_values (array_filter ($ availableLanguages , $ matchLocale ));
94- if (!empty ($ result )) {
95- $ this ->identifiedLocale = $ result [0 ];
96-
97- return true ;
98- }
99- } else {
100- $ this ->identifiedLocale = $ preferredLocale ;
87+ $ result = array_values (array_filter ($ availableLocales , $ matchLocale ));
88+ if (!empty ($ result )) {
89+ $ this ->identifiedLocale = $ result [0 ];
10190
10291 return true ;
10392 }
@@ -114,36 +103,12 @@ public function guessLocale(Request $request)
114103 */
115104 private function getPrimaryLanguage ($ locale )
116105 {
117- $ primaryLanguage = substr ($ locale , 0 , 2 );
118- if (preg_match ('/[a-z]{2}/ ' , $ primaryLanguage )) {
119- return $ primaryLanguage ;
120- }
121-
122- return null ;
123- }
124-
125- /**
126- * Compiles a list of all available languages from a locale list
127- *
128- * @param array $availableLocales
129- *
130- * @return array
131- */
132- private function compileLanguageArray (array $ availableLocales = array ())
133- {
134- $ providedLanguages = array ();
135- foreach ($ availableLocales as $ locale ) {
136- if ($ this ->intlExtension ) {
137- $ language = \Locale::getPrimaryLanguage ($ locale );
138- } else {
139- $ language = $ this ->getPrimaryLanguage ($ locale );
140- }
141- if ($ language ) {
142- $ providedLanguages [] = $ language ;
143- }
106+ if ($ this ->intlExtension ) {
107+ return \Locale::getPrimaryLanguage ($ locale );
144108 }
109+ $ splittedLocale = explode ('_ ' , $ locale );
145110
146- return array_unique ( $ providedLanguages ) ;
111+ return count ( $ splittedLocale ) > 1 ? $ splittedLocale [ 0 ] : $ locale ;
147112 }
148113
149114 /**
0 commit comments