xref: /webtrees/app/Http/Middleware/UseLanguage.php (revision 449b311ecf65f677a2595e1e29f712d11ef22f34)
1150f35adSGreg Roach<?php
2150f35adSGreg Roach
3150f35adSGreg Roach/**
4150f35adSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6150f35adSGreg Roach * This program is free software: you can redistribute it and/or modify
7150f35adSGreg Roach * it under the terms of the GNU General Public License as published by
8150f35adSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9150f35adSGreg Roach * (at your option) any later version.
10150f35adSGreg Roach * This program is distributed in the hope that it will be useful,
11150f35adSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12150f35adSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13150f35adSGreg Roach * GNU General Public License for more details.
14150f35adSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16150f35adSGreg Roach */
17150f35adSGreg Roach
18150f35adSGreg Roachdeclare(strict_types=1);
19150f35adSGreg Roach
20150f35adSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
21150f35adSGreg Roach
22150f35adSGreg Roachuse Fisharebest\Localization\Locale;
23150f35adSGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface;
24150f35adSGreg Roachuse Fisharebest\Webtrees\I18N;
25150f35adSGreg Roachuse Fisharebest\Webtrees\Module\LanguageEnglishUnitedStates;
26150f35adSGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface;
27150f35adSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
28150f35adSGreg Roachuse Fisharebest\Webtrees\Session;
2942e92694SGreg Roachuse Fisharebest\Webtrees\Site;
30150f35adSGreg Roachuse Generator;
31150f35adSGreg Roachuse Psr\Http\Message\ResponseInterface;
32150f35adSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
33150f35adSGreg Roachuse Psr\Http\Server\MiddlewareInterface;
34150f35adSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
35150f35adSGreg Roach
36d8809d62SGreg Roachuse function is_string;
37d8809d62SGreg Roach
38150f35adSGreg Roach/**
39150f35adSGreg Roach * Middleware to select a language.
40150f35adSGreg Roach */
41150f35adSGreg Roachclass UseLanguage implements MiddlewareInterface
42150f35adSGreg Roach{
43c4943cffSGreg Roach    private ModuleService $module_service;
44150f35adSGreg Roach
45150f35adSGreg Roach    /**
46150f35adSGreg Roach     * @param ModuleService $module_service
47150f35adSGreg Roach     */
48150f35adSGreg Roach    public function __construct(ModuleService $module_service)
49150f35adSGreg Roach    {
50150f35adSGreg Roach        $this->module_service = $module_service;
51150f35adSGreg Roach    }
52150f35adSGreg Roach
53150f35adSGreg Roach    /**
54150f35adSGreg Roach     * @param ServerRequestInterface  $request
55150f35adSGreg Roach     * @param RequestHandlerInterface $handler
56150f35adSGreg Roach     *
57150f35adSGreg Roach     * @return ResponseInterface
58150f35adSGreg Roach     */
59150f35adSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
60150f35adSGreg Roach    {
61150f35adSGreg Roach        foreach ($this->languages($request) as $language) {
62150f35adSGreg Roach            if ($language instanceof ModuleLanguageInterface) {
63150f35adSGreg Roach                I18N::init($language->locale()->languageTag());
64150f35adSGreg Roach                Session::put('language', $language->locale()->languageTag());
65150f35adSGreg Roach                break;
66150f35adSGreg Roach            }
67150f35adSGreg Roach        }
68150f35adSGreg Roach
69150f35adSGreg Roach        return $handler->handle($request);
70150f35adSGreg Roach    }
71150f35adSGreg Roach
72150f35adSGreg Roach    /**
73150f35adSGreg Roach     * The language can be chosen in various ways.
74150f35adSGreg Roach     * Language module names have the form "language-<code>>".
75150f35adSGreg Roach     *
76150f35adSGreg Roach     * @param ServerRequestInterface $request
77150f35adSGreg Roach     *
78fc26b4f6SGreg Roach     * @return Generator<ModuleLanguageInterface|null>
79150f35adSGreg Roach     */
80150f35adSGreg Roach    private function languages(ServerRequestInterface $request): Generator
81150f35adSGreg Roach    {
8242e92694SGreg Roach        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true);
83150f35adSGreg Roach
84150f35adSGreg Roach        // Last language used
857b9aa048SGreg Roach        yield $languages
8671e7c06eSGreg Roach            ->first(static fn (ModuleLanguageInterface $module): bool => $module->locale()->languageTag() === Session::get('language'));
87150f35adSGreg Roach
88150f35adSGreg Roach        // Browser negotiation
89150f35adSGreg Roach        $locales = $this->module_service->findByInterface(ModuleLanguageInterface::class, true)
90*f25fc0f9SGreg Roach            ->map(static fn (ModuleLanguageInterface $module): LocaleInterface => $module->locale());
91150f35adSGreg Roach
928a07c98eSGreg Roach        $default = Locale::create(Site::getPreference('LANGUAGE'));
93150f35adSGreg Roach        $locale  = Locale::httpAcceptLanguage($request->getServerParams(), $locales->all(), $default);
94150f35adSGreg Roach
957b9aa048SGreg Roach        yield $languages
9671e7c06eSGreg Roach            ->first(static fn (ModuleLanguageInterface $module): bool => $module->locale()->languageTag() === $locale->languageTag());
97150f35adSGreg Roach
98150f35adSGreg Roach        // No languages enabled?  Use en-US
9942e92694SGreg Roach        yield new LanguageEnglishUnitedStates();
100150f35adSGreg Roach    }
101150f35adSGreg Roach}
102