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\LocaleEnUs; 24150f35adSGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface; 25150f35adSGreg Roachuse Fisharebest\Webtrees\I18N; 26150f35adSGreg Roachuse Fisharebest\Webtrees\Module\LanguageEnglishUnitedStates; 27150f35adSGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface; 28150f35adSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 29150f35adSGreg Roachuse Fisharebest\Webtrees\Session; 30150f35adSGreg Roachuse Fisharebest\Webtrees\Tree; 31150f35adSGreg Roachuse Generator; 32150f35adSGreg Roachuse Psr\Http\Message\ResponseInterface; 33150f35adSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 34150f35adSGreg Roachuse Psr\Http\Server\MiddlewareInterface; 35150f35adSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 36150f35adSGreg Roach 37150f35adSGreg Roachuse function app; 38150f35adSGreg Roach 39150f35adSGreg Roach/** 40150f35adSGreg Roach * Middleware to select a language. 41150f35adSGreg Roach */ 42150f35adSGreg Roachclass UseLanguage implements MiddlewareInterface 43150f35adSGreg Roach{ 44150f35adSGreg Roach /** @var ModuleService */ 45150f35adSGreg Roach private $module_service; 46150f35adSGreg Roach 47150f35adSGreg Roach /** 48150f35adSGreg Roach * UseTheme constructor. 49150f35adSGreg Roach * 50150f35adSGreg Roach * @param ModuleService $module_service 51150f35adSGreg Roach */ 52150f35adSGreg Roach public function __construct(ModuleService $module_service) 53150f35adSGreg Roach { 54150f35adSGreg Roach $this->module_service = $module_service; 55150f35adSGreg Roach } 56150f35adSGreg Roach 57150f35adSGreg Roach /** 58150f35adSGreg Roach * @param ServerRequestInterface $request 59150f35adSGreg Roach * @param RequestHandlerInterface $handler 60150f35adSGreg Roach * 61150f35adSGreg Roach * @return ResponseInterface 62150f35adSGreg Roach */ 63150f35adSGreg Roach public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 64150f35adSGreg Roach { 65150f35adSGreg Roach foreach ($this->languages($request) as $language) { 66150f35adSGreg Roach if ($language instanceof ModuleLanguageInterface) { 67150f35adSGreg Roach I18N::init($language->locale()->languageTag()); 68150f35adSGreg Roach Session::put('language', $language->locale()->languageTag()); 69150f35adSGreg Roach break; 70150f35adSGreg Roach } 71150f35adSGreg Roach } 72150f35adSGreg Roach 73150f35adSGreg Roach return $handler->handle($request); 74150f35adSGreg Roach } 75150f35adSGreg Roach 76150f35adSGreg Roach /** 77150f35adSGreg Roach * The language can be chosen in various ways. 78150f35adSGreg Roach * Language module names have the form "language-<code>>". 79150f35adSGreg Roach * 80150f35adSGreg Roach * @param ServerRequestInterface $request 81150f35adSGreg Roach * 82*fc26b4f6SGreg Roach * @return Generator<ModuleLanguageInterface|null> 83150f35adSGreg Roach */ 84150f35adSGreg Roach private function languages(ServerRequestInterface $request): Generator 85150f35adSGreg Roach { 86150f35adSGreg Roach $tree = $request->getAttribute('tree'); 87150f35adSGreg Roach 88150f35adSGreg Roach $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class); 89150f35adSGreg Roach 90150f35adSGreg Roach // Last language used 91150f35adSGreg Roach yield $languages->get('language-' . Session::get('language', '')); 92150f35adSGreg Roach 93150f35adSGreg Roach // Browser negotiation 94150f35adSGreg Roach $locales = $this->module_service->findByInterface(ModuleLanguageInterface::class, true) 95150f35adSGreg Roach ->map(static function (ModuleLanguageInterface $module): LocaleInterface { 96150f35adSGreg Roach return $module->locale(); 97150f35adSGreg Roach }); 98150f35adSGreg Roach 99150f35adSGreg Roach if ($tree instanceof Tree) { 100150f35adSGreg Roach $default = Locale::create($tree->getPreference('LANGUAGE', 'en-US')); 101150f35adSGreg Roach } else { 102150f35adSGreg Roach $default = new LocaleEnUs(); 103150f35adSGreg Roach } 104150f35adSGreg Roach 105150f35adSGreg Roach $locale = Locale::httpAcceptLanguage($request->getServerParams(), $locales->all(), $default); 106150f35adSGreg Roach 107150f35adSGreg Roach yield $languages->get('language-' . $locale->languageTag()); 108150f35adSGreg Roach 109150f35adSGreg Roach // No languages enabled? Use en-US 110150f35adSGreg Roach yield app(LanguageEnglishUnitedStates::class); 111150f35adSGreg Roach } 112150f35adSGreg Roach} 113