Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nhayder's avatar
Level 13

How to use laravel localization in vue

i'm using vuex, vue and laravel 5.8 on my multilingual app, i'm at a point to add localization to my vue component.

i didn't started yet but i'm checking what is the best way to extend laravel localization to some vue component on the app.

any recommendations on this matter ???

0 likes
9 replies
takdw's avatar

So, I'm sure there are a couple of ways to do this but this is how I did it in a project about 6 months ago.

I added a script tag that loads a "dynamic" JavaScript file. It doesn't point to a real file. It's just there to make a GET request. So first add this line to your master template file.

<script src="/lang-{{ app()->getLocale() }}.js"></script>

Depending on the current locale set, the above script will make a GET request to lang-<your-locale>.js. Then you have to setup a route that responds to this request. Here is how I did it.

routes/web.php

Route::get('/lang-{lang}.js', 'LanguageController@show');

LanguageController.php

public function show($locale)
    {
        $locale = in_array($locale, config('app.locales')) ? $locale : config('app.fallback_locale');

        $files   = glob(resource_path('lang/' . $locale . '/*.php'));
        $strings = [];

        foreach ($files as $file) {
            $name = basename($file, '.php');
            $strings[$name] = require $file;
        }

        $contents = 'window.i18n = ' . json_encode($strings, config('app.debug', false) ? JSON_PRETTY_PRINT : 0) . ';';
        $response = \Response::make($contents, 200);
        $response->header('Content-Type', 'application/javascript');

        return $response;
    }

The controller does a simple thing. It looks inside your Laravel Locale folder and get all the files that match the query passed in (the $locale). Then it reads the contents of those files and basically builds a JS file and returns that. All that file does is store all the language key & value pairs in an i18 variable. Since this variable is stored globally you can access it from any of your Vue files. You can even make a simple helper function to use what you would use in PHP.

import _ from 'lodash'

Vue.prototype.__ = str => _.get(window.i18n, str)

All this does is it gives you a helper function you can use in your Vue files easily. For example you can use it as __('auth.failed')

I hope I explained enough (I'm kinda tired and couldn't explain more, maybe tomorrow :) )

5 likes
David-R's avatar

Excellent TAKDW !

Just a little clarification that sounds obvious for JS developer and not beginner like me

[All this does is it gives you a helper function you can use in your Vue files easily. For example you can use it as ]

Try this below with the accolades in your files *.vue

{{ __('auth.failed') }}

Hanie2000's avatar

@takdw thank you for the solution! but i'm a little bit confused. i'm developing laravel vue application and for localization I did as what you said but it's not working :( can you tell me the name of solution or tutorial for it?

nhayder's avatar
Level 13

Thank you for the time you took for explanation ? it looks like it will definitely work but i'm not sure if this is the recommended way to do that.

takdw's avatar

Yeah. I don't know the recommended way but this approach worked for me pretty well. I still use it on the site to this day and it works like charm.

2 likes
nhayder's avatar
Level 13

@morpheus7 thank you i didn't know about that package till you brought it up, i will definitely check it soon :-)

Please or to participate in this conversation.