lat4732's avatar
Level 12

Retrieve all available language folders

Hello everyone!

Can you give me suggestion on how to retrieve all available language folders? Example tree:

...
-resources
    -lang
        -en
        -es
        ....
    ...
...

How can I retrieve all the available folders inside the lang folder so I can display them in a select dropdown and the administrator can choose the default language preferred for the website?

0 likes
4 replies
gerardnll's avatar
Level 1

Would be nice to cache the result, to avoid too much IO.

/**
     * Scan languages folder and return list of available languages
     *
     * @return array
     */
    private function scanLanguages(): array
    {
        $filtered = ['.', '..'];

        $dirs = [];
        $d = dir(resource_path('lang'));
        while (($entry = $d->read()) !== false) {
            if (is_dir(resource_path('lang').'/'.$entry) && !in_array($entry, $filtered)) {
                $dirs[] = $entry;
            }
        }

        return $dirs;
    }
``
1 like
gerardnll's avatar

@Armani It defaults to the disk configured in filesystem.php, which is by default 'local' and that means it looks inside the storage folder. Not worth the configuration of a new disk because I don't think this use it's inline with the objective of the whole 'filesystem' interface.

Please or to participate in this conversation.