File tree Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -65,7 +65,6 @@ class PKPRequest
65
65
/** @var string user agent */
66
66
public string $ _userAgent ;
67
67
68
-
69
68
/**
70
69
* get the router instance
71
70
*/
@@ -593,7 +592,7 @@ public function getUserVar(string $key): mixed
593
592
{
594
593
// special treatment for APIRouter. APIHandler gets to fetch parameter first
595
594
$ router = $ this ->getRouter ();
596
-
595
+
597
596
if ($ router instanceof \PKP \core \APIRouter && (!is_null ($ handler = $ router ->getHandler ()))) {
598
597
$ handler = $ router ->getHandler (); /** @var \PKP\handler\APIHandler $handler */
599
598
$ value = $ handler ->getApiController ()->getParameter ($ key );
Original file line number Diff line number Diff line change @@ -134,7 +134,8 @@ public function getLocale(): string
134
134
$ request = $ this ->_getRequest ();
135
135
$ locale = $ request ->getUserVar ('setLocale ' )
136
136
?: $ request ->getSession ()->get ('currentLocale ' )
137
- ?: $ request ->getCookieVar ('currentLocale ' );
137
+ ?: $ request ->getCookieVar ('currentLocale ' )
138
+ ?: $ this ->getPreferredLocale ();
138
139
$ this ->setLocale ($ locale );
139
140
return $ this ->locale ;
140
141
}
@@ -582,4 +583,19 @@ private function _getSubmissionLocaleNames(): array
582
583
->toArray ());
583
584
})();
584
585
}
586
+
587
+ /**
588
+ * Retrieve the preferred user locale from our supported locales using the Accept-Language header
589
+ * If there's no match, it falls back to the server's primary locale
590
+ */
591
+ private function getPreferredLocale (): ?string
592
+ {
593
+ $ serverPreference = $ this ->getPrimaryLocale () ?: LocaleInterface::DEFAULT_LOCALE ;
594
+ $ supportedLocales = array_values ($ this ->_getSupportedLocales ());
595
+ // Move the server preference to the top, in case the user preference doesn't match with the supported locales, the server one will be picked
596
+ if (is_int ($ index = array_search ($ serverPreference , $ supportedLocales ))) {
597
+ array_splice ($ supportedLocales , $ index , 1 );
598
+ }
599
+ return app (\Illuminate \Http \Request::class)->getPreferredLanguage ([$ serverPreference , ...$ supportedLocales ]);
600
+ }
585
601
}
You can’t perform that action at this time.
0 commit comments