You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pixelfed/app/Util/Localization/Localization.php

70 lines
1.7 KiB
PHTML

<?php
namespace App\Util\Localization;
use Illuminate\Support\Arr;
9 months ago
class Localization
{
1 week ago
/**
* List of available UI language codes.
*
* Reads the static manifest generated by `php artisan i18n:export`
* (lang/locales.json) so the list is deterministic and never served from
* a runtime cache that can go stale. Falls back to scanning the lang/
* directory if the manifest is missing.
*
* @return array<int, string>
*/
9 months ago
public static function languages()
{
1 week ago
return static::localesFromManifest() ?? static::localesFromScan();
}
/**
* Full locale metadata (code, name, nativeName) from the manifest,
* sorted by display name. Empty array if the manifest is missing.
*
* @return array<int, array{code: string, name: string, nativeName: string}>
*/
public static function locales(): array
{
$path = lang_path('locales.json');
if (! is_file($path)) {
return [];
}
$data = json_decode((string) file_get_contents($path), true);
return is_array($data) ? $data : [];
}
/**
* @return array<int, string>|null
*/
protected static function localesFromManifest(): ?array
{
$locales = static::locales();
if (empty($locales)) {
return null;
}
return array_values(array_filter(array_map(
fn ($l) => $l['code'] ?? null,
$locales
)));
}
/**
* @return array<int, string>
*/
protected static function localesFromScan(): array
{
$dir = lang_path();
1 week ago
return Arr::flatten(array_diff(scandir($dir), ['..', '.', 'vendor', '.DS_Store', 'locales.json']));
9 months ago
}
}