xref: /webtrees/app/Http/Middleware/UseLanguage.php (revision 8a07c98e2c323f7fe8deb3ac46485d8082b4b795)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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
36/**
37 * Middleware to select a language.
38 */
39class UseLanguage implements MiddlewareInterface
40{
41    private ModuleService $module_service;
42
43    /**
44     * UseTheme constructor.
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->get('language-' . Session::get('language', ''));
86
87        // Browser negotiation
88        $locales = $this->module_service->findByInterface(ModuleLanguageInterface::class, true)
89            ->map(static function (ModuleLanguageInterface $module): LocaleInterface {
90                return $module->locale();
91            });
92
93        $default = Locale::create(Site::getPreference('LANGUAGE'));
94        $locale  = Locale::httpAcceptLanguage($request->getServerParams(), $locales->all(), $default);
95
96        yield $languages->get('language-' . $locale->languageTag());
97
98        // No languages enabled?  Use en-US
99        yield new LanguageEnglishUnitedStates();
100    }
101}
102