|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Util\Localization;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
|
|
|
|
|
|
class Localization
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 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>
|
|
|
|
|
*/
|
|
|
|
|
public static function languages()
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
return Arr::flatten(array_diff(scandir($dir), ['..', '.', 'vendor', '.DS_Store', 'locales.json']));
|
|
|
|
|
}
|
|
|
|
|
}
|