Skip to content

Commit 07a1e5c

Browse files
committed
feat(translation): added API to fetch all translation strings for given language (#3317)
1 parent 218f5e7 commit 07a1e5c

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

phpmyfaq/src/api-routes.php

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use phpMyFAQ\Controller\Frontend\FaqController as FaqFrontendController;
4343
use phpMyFAQ\Controller\Frontend\QuestionController as QuestionFrontendController;
4444
use phpMyFAQ\Controller\Frontend\RegistrationController as RegistrationFrontendController;
45+
use phpMyFAQ\Controller\Frontend\TranslationController;
4546
use phpMyFAQ\Controller\Frontend\UnauthorizedUserController;
4647
use phpMyFAQ\Controller\Frontend\UserController;
4748
use phpMyFAQ\Controller\Frontend\VotingController;
@@ -252,6 +253,11 @@
252253
'controller' => [RegistrationFrontendController::class, 'create'],
253254
'methods' => 'POST'
254255
],
256+
'api.private.translations' => [
257+
'path' => 'translations/{language}',
258+
'controller' => [TranslationController::class, 'translations'],
259+
'methods' => 'POST'
260+
],
255261
'api.private.user.password' => [
256262
'path' => 'user/password/update',
257263
'controller' => [UnauthorizedUserController::class, 'updatePassword'],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 <[email protected]>
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+
}

phpmyfaq/src/phpMyFAQ/Controller/Frontend/UserController.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use phpMyFAQ\Filter;
2323
use phpMyFAQ\Session\Token;
2424
use phpMyFAQ\Translation;
25-
use phpMyFAQ\User\CurrentUser;
2625
use phpMyFAQ\User\TwoFactor;
2726
use RobThree\Auth\TwoFactorAuthException;
2827
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -36,7 +35,7 @@ class UserController extends AbstractController
3635
/**
3736
* @throws \Exception
3837
*/
39-
#[Route('api/user/data/update', methods: ['PUT'])]
38+
#[Route('api/user/data/update', name: 'api.private.user.update', methods: ['PUT'])]
4039
public function updateData(Request $request): JsonResponse
4140
{
4241
$this->userIsAuthenticated();

phpmyfaq/src/phpMyFAQ/Translation.php

+13
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ public static function get(string $languageKey): string|array|null
7575
return null;
7676
}
7777

78+
/**
79+
* Returns all translations from the current language.
80+
* @throws Exception
81+
* @return array<string, string>
82+
*/
83+
public static function getAll(): array
84+
{
85+
self::$translation->checkInit();
86+
self::$translation->checkLanguageLoaded();
87+
88+
return self::$translation->loadedLanguages[self::$translation->currentLanguage];
89+
}
90+
7891
/**
7992
* @throws Exception
8093
*/

0 commit comments

Comments
 (0)