Files
pterodactyl-panel/app/Http/Controllers/Base/LocaleController.php
T

71 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Http\Controllers\Base;
2026-10-05 18:10:18 -04:00
use Illuminate\Contracts\Translation\Loader;
use Illuminate\Http\JsonResponse;
use Illuminate\Translation\Translator;
2022-06-26 16:21:07 -04:00
use Pterodactyl\Http\Controllers\Controller;
2025-06-15 19:40:22 -06:00
use Pterodactyl\Http\Requests\Base\LocaleRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Support\JsonValueGuard;
use UnexpectedValueException;
class LocaleController extends Controller
{
protected Loader $loader;
public function __construct(Translator $translator)
{
$this->loader = $translator->getLoader();
}
/**
* Returns translation data given a specific locale and namespace.
*/
2025-06-15 19:40:22 -06:00
public function __invoke(LocaleRequest $request): JsonResponse
{
2026-10-05 18:10:18 -04:00
$locale = JsonValueGuard::string($request->validated('locale'));
$namespace = JsonValueGuard::string($request->validated('namespace'));
$segments = explode('::', $namespace, 2);
$translations = count($segments) === 2
? $this->loader->load($locale, $segments[1], $segments[0])
: $this->loader->load($locale, $namespace);
JsonValueGuard::assertValue($translations);
$response[$locale][$namespace] = $this->i18n($translations);
return new JsonResponse($response, 200, [
// Cache this in the browser for an hour, and allow the browser to use a stale
// cache for up to a day after it was created while it fetches an updated set
// of translation keys.
'Cache-Control' => 'public, max-age=3600, stale-while-revalidate=86400',
2026-10-05 18:10:18 -04:00
'ETag' => hash('sha256', json_encode($response, JSON_THROW_ON_ERROR)),
]);
}
/**
* Convert standard Laravel translation keys that look like ":foo"
* into key structures that are supported by the front-end i18n
* library, like "{{foo}}".
2026-10-05 18:10:18 -04:00
*
* @param array<array-key, JsonInputValue> $data
* @return array<array-key, JsonInputValue>
*/
protected function i18n(array $data): array
{
foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = $this->i18n($value);
} else {
2026-10-05 18:10:18 -04:00
throw_unless(is_string($value), UnexpectedValueException::class, 'Translation leaves must be strings.');
// Rewrite any Laravel-style ":name" placeholders into the "{{name}}" form the
// front-end i18n library understands.
2022-06-18 16:36:19 -04:00
$data[$key] = preg_replace('/:([\w.-]+\w)([^\w:]?|$)/m', '{{$1}}$2', $value);
}
}
return $data;
}
}