xref: /webtrees/app/Http/Middleware/UseLanguage.php (revision 531c6e7cb7a0ab5fa4bcb50613b814dd81c279e0)
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     * @param ModuleService $module_service
47     */
48    public function __construct(ModuleService $module_service)
49    {
50        $this->module_service = $module_service;
51    }
52
53    /**
54     * @param ServerRequestInterface  $request
55     * @param RequestHandlerInterface $handler
56     *
57     * @return ResponseInterface
58     */
59    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
60    {
61        foreach ($this->languages($request) as $language) {
62            if ($language instanceof ModuleLanguageInterface) {
63                I18N::init($language->locale()->languageTag());
64                Session::put('language', $language->locale()->languageTag());
65                break;
66            }
67        }
68
69        return $handler->handle($request);
70    }
71
72    /**
73     * The language can be chosen in various ways.
74     * Language module names have the form "language-<code>>".
75     *
76     * @param ServerRequestInterface $request
77     *
78     * @return Generator<ModuleLanguageInterface|null>
79     */
80    private function languages(ServerRequestInterface $request): Generator
81    {
82        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true);
83
84        // Last language used
85        yield $languages
86            ->first(static fn (ModuleLanguageInterface $module): bool => $module->locale()->languageTag() === Session::get('language'));
87
88        // Browser negotiation
89        $locales = $this->module_service->findByInterface(ModuleLanguageInterface::class, true)
90            ->map(static function (ModuleLanguageInterface $module): LocaleInterface {
91                return $module->locale();
92            });
93
94        $default = Locale::create(Site::getPreference('LANGUAGE'));
95        $locale  = Locale::httpAcceptLanguage($request->getServerParams(), $locales->all(), $default);
96
97        yield $languages
98            ->first(static fn (ModuleLanguageInterface $module): bool => $module->locale()->languageTag() === $locale->languageTag());
99
100        // No languages enabled?  Use en-US
101        yield new LanguageEnglishUnitedStates();
102    }
103}
104