xref: /webtrees/app/Http/Middleware/UseLanguage.php (revision 42e92694fbe3393313d65f948e79ea1439850971)
1150f35adSGreg Roach<?php
2150f35adSGreg Roach
3150f35adSGreg Roach/**
4150f35adSGreg Roach * webtrees: online genealogy
5150f35adSGreg Roach * Copyright (C) 2019 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
15150f35adSGreg Roach * along with this program. If not, see <http://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;
29*42e92694SGreg 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
36150f35adSGreg Roach/**
37150f35adSGreg Roach * Middleware to select a language.
38150f35adSGreg Roach */
39150f35adSGreg Roachclass UseLanguage implements MiddlewareInterface
40150f35adSGreg Roach{
41150f35adSGreg Roach    /** @var ModuleService */
42150f35adSGreg Roach    private $module_service;
43150f35adSGreg Roach
44150f35adSGreg Roach    /**
45150f35adSGreg Roach     * UseTheme constructor.
46150f35adSGreg Roach     *
47150f35adSGreg Roach     * @param ModuleService $module_service
48150f35adSGreg Roach     */
49150f35adSGreg Roach    public function __construct(ModuleService $module_service)
50150f35adSGreg Roach    {
51150f35adSGreg Roach        $this->module_service = $module_service;
52150f35adSGreg Roach    }
53150f35adSGreg Roach
54150f35adSGreg Roach    /**
55150f35adSGreg Roach     * @param ServerRequestInterface  $request
56150f35adSGreg Roach     * @param RequestHandlerInterface $handler
57150f35adSGreg Roach     *
58150f35adSGreg Roach     * @return ResponseInterface
59150f35adSGreg Roach     */
60150f35adSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
61150f35adSGreg Roach    {
62150f35adSGreg Roach        foreach ($this->languages($request) as $language) {
63150f35adSGreg Roach            if ($language instanceof ModuleLanguageInterface) {
64150f35adSGreg Roach                I18N::init($language->locale()->languageTag());
65150f35adSGreg Roach                Session::put('language', $language->locale()->languageTag());
66150f35adSGreg Roach                break;
67150f35adSGreg Roach            }
68150f35adSGreg Roach        }
69150f35adSGreg Roach
70150f35adSGreg Roach        return $handler->handle($request);
71150f35adSGreg Roach    }
72150f35adSGreg Roach
73150f35adSGreg Roach    /**
74150f35adSGreg Roach     * The language can be chosen in various ways.
75150f35adSGreg Roach     * Language module names have the form "language-<code>>".
76150f35adSGreg Roach     *
77150f35adSGreg Roach     * @param ServerRequestInterface $request
78150f35adSGreg Roach     *
79fc26b4f6SGreg Roach     * @return Generator<ModuleLanguageInterface|null>
80150f35adSGreg Roach     */
81150f35adSGreg Roach    private function languages(ServerRequestInterface $request): Generator
82150f35adSGreg Roach    {
83*42e92694SGreg Roach        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true);
84150f35adSGreg Roach
85150f35adSGreg Roach        // Last language used
86150f35adSGreg Roach        yield $languages->get('language-' . Session::get('language', ''));
87150f35adSGreg Roach
88150f35adSGreg Roach        // Browser negotiation
89150f35adSGreg Roach        $locales = $this->module_service->findByInterface(ModuleLanguageInterface::class, true)
90150f35adSGreg Roach            ->map(static function (ModuleLanguageInterface $module): LocaleInterface {
91150f35adSGreg Roach                return $module->locale();
92150f35adSGreg Roach            });
93150f35adSGreg Roach
94*42e92694SGreg Roach        $default = Locale::create(Site::getPreference('LANGUAGE', 'en-US'));
95150f35adSGreg Roach        $locale  = Locale::httpAcceptLanguage($request->getServerParams(), $locales->all(), $default);
96150f35adSGreg Roach
97150f35adSGreg Roach        yield $languages->get('language-' . $locale->languageTag());
98150f35adSGreg Roach
99150f35adSGreg Roach        // No languages enabled?  Use en-US
100*42e92694SGreg Roach        yield new LanguageEnglishUnitedStates();
101150f35adSGreg Roach    }
102150f35adSGreg Roach}
103