|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * The Translations Controller |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | + * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | + * |
| 10 | + * @package phpMyFAQ |
| 11 | + * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | + * @copyright 2025 phpMyFAQ Team |
| 13 | + * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | + * @link https://www.phpmyfaq.de |
| 15 | + * @since 2025-03-17 |
| 16 | + */ |
| 17 | + |
| 18 | +namespace phpMyFAQ\Controller\Frontend; |
| 19 | + |
| 20 | +use phpMyFAQ\Controller\AbstractController; |
| 21 | +use phpMyFAQ\Core\Exception; |
| 22 | +use phpMyFAQ\Filter; |
| 23 | +use phpMyFAQ\Language; |
| 24 | +use phpMyFAQ\Translation; |
| 25 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 26 | +use Symfony\Component\HttpFoundation\Request; |
| 27 | +use Symfony\Component\HttpFoundation\Response; |
| 28 | +use Symfony\Component\Routing\Attribute\Route; |
| 29 | + |
| 30 | +class TranslationController extends AbstractController |
| 31 | +{ |
| 32 | + #[Route('api/translations/{language}', name: 'api.private.translations', methods: ['GET'])] |
| 33 | + public function translations(Request $request): JsonResponse |
| 34 | + { |
| 35 | + $language = Filter::filterVar($request->get('language'), FILTER_SANITIZE_SPECIAL_CHARS); |
| 36 | + |
| 37 | + if (!Language::isASupportedLanguage($language)) { |
| 38 | + return $this->json(['error' => 'Language not supported'], Response::HTTP_BAD_REQUEST); |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + Translation::getInstance()->setCurrentLanguage($language); |
| 43 | + return $this->json(Translation::getAll()); |
| 44 | + } catch (Exception $e) { |
| 45 | + return $this->json(['error' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments