@@ -14,7 +14,7 @@ class Localizer
1414 protected $ locales ;
1515
1616 /**
17- * \CoderZero\Localizer\Detectors\Detector instances.
17+ * \CoderZero\Localizer\Detectors\Detector class names or instances.
1818 *
1919 * @var \Illuminate\Support\Collection|array
2020 */
@@ -27,18 +27,27 @@ class Localizer
2727 */
2828 protected $ stores ;
2929
30+ /**
31+ * \CoderZero\Localizer\Detectors\Detector class names.
32+ *
33+ * @var \Illuminate\Support\Collection|array
34+ */
35+ protected $ trustedDetectors ;
36+
3037 /**
3138 * Create a new Localizer instance.
3239 *
3340 * @param \Illuminate\Support\Collection|array $locales
3441 * @param \Illuminate\Support\Collection|array $detectors
3542 * @param \Illuminate\Support\Collection|array $stores
43+ * @param \Illuminate\Support\Collection|array $trustedDetectors
3644 */
37- public function __construct ($ locales , $ detectors , $ stores = [])
45+ public function __construct ($ locales , $ detectors , $ stores = [], $ trustedDetectors = [] )
3846 {
3947 $ this ->setSupportedLocales ($ locales );
4048 $ this ->detectors = $ detectors ;
4149 $ this ->stores = $ stores ;
50+ $ this ->trustedDetectors = $ trustedDetectors ;
4251 }
4352
4453 /**
@@ -52,7 +61,7 @@ public function detect()
5261 $ locales = (array ) $ this ->getInstance ($ detector )->detect ();
5362
5463 foreach ($ locales as $ locale ) {
55- if ($ this ->isSupportedLocale ($ locale )) {
64+ if ($ locale && ( $ this ->isSupportedLocale ($ locale) || $ this -> isTrustedDetector ( $ detector ) )) {
5665 return $ locale ;
5766 }
5867 }
@@ -105,6 +114,28 @@ protected function isSupportedLocale($locale)
105114 return in_array ($ locale , $ this ->locales );
106115 }
107116
117+ /**
118+ * Check if the given Detector class is trusted.
119+ *
120+ * @param \CodeZero\Localizer\Detectors\Detector|string $detector
121+ *
122+ * @return bool
123+ */
124+ protected function isTrustedDetector ($ detector )
125+ {
126+ if (is_string ($ detector )) {
127+ return in_array ($ detector , $ this ->trustedDetectors );
128+ }
129+
130+ foreach ($ this ->trustedDetectors as $ trustedDetector ) {
131+ if ($ detector instanceof $ trustedDetector ) {
132+ return true ;
133+ }
134+ }
135+
136+ return false ;
137+ }
138+
108139 /**
109140 * Get the class from Laravel's IOC container if it is a string.
110141 *
0 commit comments