fisharebest /
webtrees
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * webtrees: online genealogy |
||
| 5 | * Copyright (C) 2025 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 | |||
| 18 | declare(strict_types=1); |
||
| 19 | |||
| 20 | namespace Fisharebest\Webtrees\Http\Middleware; |
||
| 21 | |||
| 22 | use Fisharebest\Localization\Locale; |
||
| 23 | use Fisharebest\Localization\Locale\LocaleInterface; |
||
| 24 | use Fisharebest\Webtrees\I18N; |
||
|
0 ignored issues
–
show
|
|||
| 25 | use Fisharebest\Webtrees\Module\LanguageEnglishUnitedStates; |
||
|
0 ignored issues
–
show
The type
Fisharebest\Webtrees\Mod...uageEnglishUnitedStates was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 26 | use Fisharebest\Webtrees\Module\ModuleLanguageInterface; |
||
| 27 | use Fisharebest\Webtrees\Services\ModuleService; |
||
|
0 ignored issues
–
show
The type
Fisharebest\Webtrees\Services\ModuleService was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 28 | use Fisharebest\Webtrees\Session; |
||
| 29 | use Fisharebest\Webtrees\Site; |
||
| 30 | use Generator; |
||
| 31 | use Psr\Http\Message\ResponseInterface; |
||
| 32 | use Psr\Http\Message\ServerRequestInterface; |
||
| 33 | use Psr\Http\Server\MiddlewareInterface; |
||
| 34 | use Psr\Http\Server\RequestHandlerInterface; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Middleware to select a language. |
||
| 38 | */ |
||
| 39 | class UseLanguage implements MiddlewareInterface |
||
| 40 | { |
||
| 41 | private ModuleService $module_service; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param ModuleService $module_service |
||
| 45 | */ |
||
| 46 | public function __construct(ModuleService $module_service) |
||
| 47 | { |
||
| 48 | $this->module_service = $module_service; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param ServerRequestInterface $request |
||
| 53 | * @param RequestHandlerInterface $handler |
||
| 54 | * |
||
| 55 | * @return ResponseInterface |
||
| 56 | */ |
||
| 57 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
| 58 | { |
||
| 59 | foreach ($this->languages($request) as $language) { |
||
| 60 | if ($language instanceof ModuleLanguageInterface) { |
||
| 61 | I18N::init($language->locale()->languageTag()); |
||
| 62 | Session::put('language', $language->locale()->languageTag()); |
||
| 63 | break; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | return $handler->handle($request); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The language can be chosen in various ways. |
||
| 72 | * Language module names have the form "language-<code>>". |
||
| 73 | * |
||
| 74 | * @param ServerRequestInterface $request |
||
| 75 | * |
||
| 76 | * @return Generator<ModuleLanguageInterface|null> |
||
| 77 | */ |
||
| 78 | private function languages(ServerRequestInterface $request): Generator |
||
| 79 | { |
||
| 80 | $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true); |
||
| 81 | |||
| 82 | // Last language used |
||
| 83 | yield $languages |
||
| 84 | ->first(static fn (ModuleLanguageInterface $module): bool => $module->locale()->languageTag() === Session::get('language')); |
||
| 85 | |||
| 86 | // Browser negotiation |
||
| 87 | $locales = $this->module_service->findByInterface(ModuleLanguageInterface::class, true) |
||
| 88 | ->map(static fn (ModuleLanguageInterface $module): LocaleInterface => $module->locale()); |
||
| 89 | |||
| 90 | $default = Locale::create(Site::getPreference('LANGUAGE')); |
||
| 91 | $locale = Locale::httpAcceptLanguage($request->getServerParams(), $locales->all(), $default); |
||
| 92 | |||
| 93 | yield $languages |
||
| 94 | ->first(static fn (ModuleLanguageInterface $module): bool => $module->locale()->languageTag() === $locale->languageTag()); |
||
| 95 | |||
| 96 | // No languages enabled? Use en-US |
||
| 97 | yield new LanguageEnglishUnitedStates(); |
||
| 98 | } |
||
| 99 | } |
||
| 100 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths