Files

91 lines
3.4 KiB
PHP
Raw Permalink 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-27 11:42:22 +00:00
use Pterodactyl\Services\Translations\LanguageRegistry;
2026-10-05 18:10:18 -04:00
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-27 11:42:22 +00:00
$response = [];
$extensions = false;
foreach ($request->locales() as $locale) {
foreach ($request->namespaces() as $namespace) {
if ($namespace === LanguageRegistry::JSON_NAMESPACE) {
$response[$locale][$namespace] = (object) array_filter($this->loader->load($locale, '*', '*'), is_string(...));
continue;
}
$segments = explode('::', $namespace, 2);
$extensions = $extensions || count($segments) === 2;
$translations = count($segments) === 2
? $this->loader->load($locale, $segments[1], $segments[0])
: $this->loader->load($locale, $namespace);
$response[$locale][$namespace] = $this->i18n($translations);
}
}
$json = new JsonResponse($response, 200, [
// Core translations only change with a panel build, and the frontend puts the
// build hash in the URL: cache them in the browser for an hour, and allow a stale
// copy for up to a day while it fetches an updated set of translation keys.
//
// An extension's translations change whenever it is installed or upgraded, so
// the frontend adds their revision to the URL and they are cached the same way.
// Without it the URL would stay the same, and the browser has to revalidate.
2026-10-27 11:42:22 +00:00
'Cache-Control' => $extensions && ! $request->has('revision')
? 'no-cache'
: 'public, max-age=3600, stale-while-revalidate=86400',
]);
$json->setEtag(hash('sha256', json_encode($response, JSON_THROW_ON_ERROR)));
// Answers a matching If-None-Match with an empty 304, which keeps revalidation cheap.
$json->isNotModified($request);
return $json;
}
/**
* 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
*
2026-10-11 19:54:55 -04:00
* @param array<mixed> $data
* @return array<mixed>
*/
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;
}
}