DEV Community

Discussion on: How to use Laravel translations in JS (vue) files?

Collapse
 
patricnox profile image
PatricNox

Great guide, it's super beneficial to know and also works great, thank you for this!

I did however need to adjust the provider a little, to automatically detect which languages the transliteration should support. I achieved this by recursively scanning every folder within resources/lang, and get the locales from there.

App/Providers/TranslationServiceProvider.php

/**
 * Bootstrap the application services.
 *
 * @return void
 */
public function boot()
{
    Cache::rememberForever('translations', function () {
        $translations = collect();
        $locales = array_map(
            function($dir) {
                return basename($dir);
            }, glob('../../resources/lang/*')
        );

        foreach ($locales as $locale) {
            $translations[$locale] = [
                'php' => $this->phpTranslations($locale),
                'json' => $this->jsonTranslations($locale),
            ];
        }

        return $translations;
    });
}
Enter fullscreen mode Exit fullscreen mode

This enables me to not having to edit the provider every time we add support for a new language.

Collapse
 
parapente profile image
Theofilos Intzoglou

In case you are using json files which should be inside the /resources/lang/ directory, you should add the GLOB_ONLYDIR option to the glob function in the code above.