xref: /webtrees/app/Http/Middleware/UseLanguage.php (revision 5bd13fbbb0904e682044d9a60b119ce8cb8d7138)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://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\LocaleEnUs;
24use Fisharebest\Localization\Locale\LocaleInterface;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Module\LanguageEnglishUnitedStates;
27use Fisharebest\Webtrees\Module\ModuleLanguageInterface;
28use Fisharebest\Webtrees\Services\ModuleService;
29use Fisharebest\Webtrees\Session;
30use Fisharebest\Webtrees\Tree;
31use Generator;
32use Psr\Http\Message\ResponseInterface;
33use Psr\Http\Message\ServerRequestInterface;
34use Psr\Http\Server\MiddlewareInterface;
35use Psr\Http\Server\RequestHandlerInterface;
36
37use function app;
38
39/**
40 * Middleware to select a language.
41 */
42class UseLanguage implements MiddlewareInterface
43{
44    /** @var ModuleService */
45    private $module_service;
46
47    /**
48     * UseTheme constructor.
49     *
50     * @param ModuleService $module_service
51     */
52    public function __construct(ModuleService $module_service)
53    {
54        $this->module_service = $module_service;
55    }
56
57    /**
58     * @param ServerRequestInterface  $request
59     * @param RequestHandlerInterface $handler
60     *
61     * @return ResponseInterface
62     */
63    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
64    {
65        foreach ($this->languages($request) as $language) {
66            if ($language instanceof ModuleLanguageInterface) {
67                I18N::init($language->locale()->languageTag());
68                Session::put('language', $language->locale()->languageTag());
69                break;
70            }
71        }
72
73        return $handler->handle($request);
74    }
75
76    /**
77     * The language can be chosen in various ways.
78     * Language module names have the form "language-<code>>".
79     *
80     * @param ServerRequestInterface $request
81     *
82     * @return Generator<ModuleLanguageInterface|null>
83     */
84    private function languages(ServerRequestInterface $request): Generator
85    {
86        $tree = $request->getAttribute('tree');
87
88        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class);
89
90        // Last language used
91        yield $languages->get('language-' . Session::get('language', ''));
92
93        // Browser negotiation
94        $locales = $this->module_service->findByInterface(ModuleLanguageInterface::class, true)
95            ->map(static function (ModuleLanguageInterface $module): LocaleInterface {
96                return $module->locale();
97            });
98
99        if ($tree instanceof Tree) {
100            $default = Locale::create($tree->getPreference('LANGUAGE', 'en-US'));
101        } else {
102            $default = new LocaleEnUs();
103        }
104
105        $locale = Locale::httpAcceptLanguage($request->getServerParams(), $locales->all(), $default);
106
107        yield $languages->get('language-' . $locale->languageTag());
108
109        // No languages enabled?  Use en-US
110        yield app(LanguageEnglishUnitedStates::class);
111    }
112}
113