xref: /webtrees/app/Http/Middleware/UseLanguage.php (revision 7b9aa048c85b91e61ebd8b3da108893ce6e047d7)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\Middleware;
21
22use Fisharebest\Localization\Locale;
23use Fisharebest\Localization\Locale\LocaleInterface;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Module\LanguageEnglishUnitedStates;
26use Fisharebest\Webtrees\Module\ModuleLanguageInterface;
27use Fisharebest\Webtrees\Services\ModuleService;
28use Fisharebest\Webtrees\Session;
29use Fisharebest\Webtrees\Site;
30use Generator;
31use Psr\Http\Message\ResponseInterface;
32use Psr\Http\Message\ServerRequestInterface;
33use Psr\Http\Server\MiddlewareInterface;
34use Psr\Http\Server\RequestHandlerInterface;
35
36use function is_string;
37
38/**
39 * Middleware to select a language.
40 */
41class UseLanguage implements MiddlewareInterface
42{
43    private ModuleService $module_service;
44
45    /**
46     * UseTheme constructor.
47     *
48     * @param ModuleService $module_service
49     */
50    public function __construct(ModuleService $module_service)
51    {
52        $this->module_service = $module_service;
53    }
54
55    /**
56     * @param ServerRequestInterface  $request
57     * @param RequestHandlerInterface $handler
58     *
59     * @return ResponseInterface
60     */
61    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
62    {
63        foreach ($this->languages($request) as $language) {
64            if ($language instanceof ModuleLanguageInterface) {
65                I18N::init($language->locale()->languageTag());
66                Session::put('language', $language->locale()->languageTag());
67                break;
68            }
69        }
70
71        return $handler->handle($request);
72    }
73
74    /**
75     * The language can be chosen in various ways.
76     * Language module names have the form "language-<code>>".
77     *
78     * @param ServerRequestInterface $request
79     *
80     * @return Generator<ModuleLanguageInterface|null>
81     */
82    private function languages(ServerRequestInterface $request): Generator
83    {
84        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true);
85
86        // Last language used
87        yield $languages
88            ->filter(static fn (ModuleLanguageInterface $module): bool => $module->locale()->languageTag() === Session::get('language'))
89            ->first();
90
91        // Browser negotiation
92        $locales = $this->module_service->findByInterface(ModuleLanguageInterface::class, true)
93            ->map(static function (ModuleLanguageInterface $module): LocaleInterface {
94                return $module->locale();
95            });
96
97        $default = Locale::create(Site::getPreference('LANGUAGE'));
98        $locale  = Locale::httpAcceptLanguage($request->getServerParams(), $locales->all(), $default);
99
100        yield $languages
101            ->filter(static fn (ModuleLanguageInterface $module): bool => $module->locale()->languageTag() === $locale->languageTag())
102            ->first();
103
104        // No languages enabled?  Use en-US
105        yield new LanguageEnglishUnitedStates();
106    }
107}
108