2019-11-16 12:46:19 -08:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2019-11-16 12:46:19 -08:00
|
|
|
namespace Pterodactyl\Http\Controllers\Base;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Contracts\Translation\Loader;
|
2019-11-16 12:46:19 -08:00
|
|
|
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;
|
2019-11-16 12:46:19 -08:00
|
|
|
|
|
|
|
|
class LocaleController extends Controller
|
|
|
|
|
{
|
2022-06-11 14:04:09 -04:00
|
|
|
protected Loader $loader;
|
2019-11-16 12:46:19 -08:00
|
|
|
|
|
|
|
|
public function __construct(Translator $translator)
|
|
|
|
|
{
|
2022-06-11 14:04:09 -04:00
|
|
|
$this->loader = $translator->getLoader();
|
2019-11-16 12:46:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns translation data given a specific locale and namespace.
|
|
|
|
|
*/
|
2025-06-15 19:40:22 -06:00
|
|
|
public function __invoke(LocaleRequest $request): JsonResponse
|
2019-11-16 12:46:19 -08:00
|
|
|
{
|
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);
|
2019-11-16 12:46:19 -08:00
|
|
|
|
2022-06-11 14:04:09 -04:00
|
|
|
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)),
|
2019-11-16 12:46:19 -08:00
|
|
|
]);
|
|
|
|
|
}
|
2022-06-11 14:04:09 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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>
|
2022-06-11 14:04:09 -04:00
|
|
|
*/
|
|
|
|
|
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);
|
2022-06-11 14:04:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
2019-11-16 12:46:19 -08:00
|
|
|
}
|