yanzi wrote a reply+100 XP
4mos ago
List of available languages
For newer Laravel versions (where the lang folder is not in the resources anymore but in the project's base folder) and with some more functionality:
/**
* @param array array of required name replacements, eg. when you want to replace the 'en_BR' with just 'en', put ['en_BR' => 'en']
* @return array return an array of available locales, eg. ['en', 'fr']
*/
private function getAllLocales(array $replacements = []): array {
$locales = collect(array_diff(
scandir(base_path('lang')),
['..', '.'] // remove these strings from the result
))->filter(fn($item) => !str_contains($item, ".")) // removes all the files (these typically contain a dot in their name)
->map(fn($item) => $replacements[$item] ?? $item)->toArray(); // replaces all the given names (as key) in the $replacements argument with the specified new name (as value)
return array_values($locales);
}