mirror of
https://github.com/grocy/grocy.git
synced 2026-04-07 05:16:15 +02:00
Locales: use http-accept-language or "language"-cookie
This commit is contained in:
parent
747660d909
commit
c9dbda957e
8
app.php
8
app.php
|
|
@ -69,5 +69,11 @@ $errorMiddleware = $app->addErrorMiddleware(true, false, false);
|
||||||
$errorMiddleware->setDefaultErrorHandler(
|
$errorMiddleware->setDefaultErrorHandler(
|
||||||
new \Grocy\Controllers\ExceptionController($app, $container)
|
new \Grocy\Controllers\ExceptionController($app, $container)
|
||||||
);
|
);
|
||||||
|
if (GROCY_MODE === 'production')
|
||||||
|
{
|
||||||
|
$app->add(new \Grocy\Middleware\LocaleMiddleware($container));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
define(GROCY_LOCALE, GROCY_CULTURE);
|
||||||
|
}
|
||||||
$app->run();
|
$app->run();
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,7 @@ class BaseController
|
||||||
|
|
||||||
protected function getLocalizationService()
|
protected function getLocalizationService()
|
||||||
{
|
{
|
||||||
return LocalizationService::getInstance(GROCY_CULTURE);
|
return LocalizationService::getInstance(GROCY_LOCALE);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getApplicationservice()
|
protected function getApplicationservice()
|
||||||
|
|
|
||||||
65
middleware/LocaleMiddleware.php
Normal file
65
middleware/LocaleMiddleware.php
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Grocy\Middleware;
|
||||||
|
|
||||||
|
|
||||||
|
use Locale;
|
||||||
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
|
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
|
||||||
|
|
||||||
|
class LocaleMiddleware extends BaseMiddleware
|
||||||
|
{
|
||||||
|
const LOCALE_COOKIE_NAME = 'LOCALE';
|
||||||
|
|
||||||
|
public function __invoke(Request $request, RequestHandler $handler): Response
|
||||||
|
{
|
||||||
|
$locale = $this->getLocale($request);
|
||||||
|
define('GROCY_LOCALE', $locale);
|
||||||
|
return $handler->handle($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getLocale(Request $request)
|
||||||
|
{
|
||||||
|
$cookies = $request->getCookieParams();
|
||||||
|
if (isset($cookies[self::LOCALE_COOKIE_NAME])) {
|
||||||
|
$locale = $cookies[self::LOCALE_COOKIE_NAME];
|
||||||
|
if (in_array($locale, scandir(__DIR__ . '/../localization'))) {
|
||||||
|
return $locale;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$langs = join(',', $request->getHeader('Accept-Language'));
|
||||||
|
|
||||||
|
// src: https://gist.github.com/spolischook/0cde9c6286415cddc088
|
||||||
|
$prefLocales = array_reduce(
|
||||||
|
explode(',', $langs),
|
||||||
|
function ($res, $el) {
|
||||||
|
list($l, $q) = array_merge(explode(';q=', $el), [1]);
|
||||||
|
$res[$l] = (float) $q;
|
||||||
|
return $res;
|
||||||
|
}, []);
|
||||||
|
arsort($prefLocales);
|
||||||
|
|
||||||
|
$availableLocales = scandir(__DIR__ . '/../localization');
|
||||||
|
foreach ($prefLocales as $locale => $q) {
|
||||||
|
if(in_array($locale, $availableLocales))
|
||||||
|
{
|
||||||
|
return $locale;
|
||||||
|
}
|
||||||
|
// e.g. en_GB
|
||||||
|
if(in_array(substr($locale, 0, 5), $availableLocales))
|
||||||
|
{
|
||||||
|
return substr($locale, 0, 5);
|
||||||
|
}
|
||||||
|
// e.g: cs
|
||||||
|
// or en
|
||||||
|
// or de
|
||||||
|
if(in_array(substr($locale, 0, 2), $availableLocales))
|
||||||
|
{
|
||||||
|
return substr($locale, 0, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return GROCY_CULTURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -670,3 +670,10 @@ $(Grocy.UserPermissions).each(function (index, item)
|
||||||
$('.permission-'+item.permission_name).addClass('disabled').addClass('not-allowed');
|
$('.permission-'+item.permission_name).addClass('disabled').addClass('not-allowed');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Grocy.SetLanguage = function (lang) {
|
||||||
|
var expires = new Date();
|
||||||
|
// Expires "never" (= 30 years)
|
||||||
|
expires.setDate(expires.getDate() + 365*30);
|
||||||
|
document.cookie = "LOCALE=" + lang + "; SameSite=Lax; expires="+expires.toUTCString();
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ class BaseService
|
||||||
|
|
||||||
protected function getLocalizationService()
|
protected function getLocalizationService()
|
||||||
{
|
{
|
||||||
return LocalizationService::getInstance(GROCY_CULTURE);
|
return LocalizationService::getInstance(GROCY_LOCALE);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getStockservice()
|
protected function getStockservice()
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="{{ GROCY_CULTURE }}">
|
<html lang="{{ GROCY_LOCALE }}">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta http-equiv="x-ua-compatible" content="ie=edge">
|
<meta http-equiv="x-ua-compatible" content="ie=edge">
|
||||||
|
|
@ -52,7 +52,7 @@
|
||||||
Grocy.BaseUrl = '{{ $U('/') }}';
|
Grocy.BaseUrl = '{{ $U('/') }}';
|
||||||
Grocy.CurrentUrlRelative = "/" + window.location.href.split('?')[0].replace(Grocy.BaseUrl, "");
|
Grocy.CurrentUrlRelative = "/" + window.location.href.split('?')[0].replace(Grocy.BaseUrl, "");
|
||||||
Grocy.ActiveNav = '@yield('activeNav', '')';
|
Grocy.ActiveNav = '@yield('activeNav', '')';
|
||||||
Grocy.Culture = '{{ GROCY_CULTURE }}';
|
Grocy.Culture = '{{ GROCY_LOCALE }}';
|
||||||
Grocy.Currency = '{{ GROCY_CURRENCY }}';
|
Grocy.Currency = '{{ GROCY_CURRENCY }}';
|
||||||
Grocy.CalendarFirstDayOfWeek = '{{ GROCY_CALENDAR_FIRST_DAY_OF_WEEK }}';
|
Grocy.CalendarFirstDayOfWeek = '{{ GROCY_CALENDAR_FIRST_DAY_OF_WEEK }}';
|
||||||
Grocy.CalendarShowWeekNumbers = {{ BoolToString(GROCY_CALENDAR_SHOW_WEEK_OF_YEAR) }};
|
Grocy.CalendarShowWeekNumbers = {{ BoolToString(GROCY_CALENDAR_SHOW_WEEK_OF_YEAR) }};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user