Laravel-JS-Localization icon indicating copy to clipboard operation
Laravel-JS-Localization copied to clipboard

Laravel 8. Uncaught ReferenceError: Lang is not defined

Open joantp opened this issue 5 years ago • 4 comments

I don't know where this error comes from and how to solve it.

I'have added Mariuzzo\LaravelJsLocalization\LaravelJsLocalizationServiceProvider::class in config/app.php and I am able to generate messages.js.

Please help! :)

joantp avatar Nov 30 '20 09:11 joantp

with the command php:artisan lang:js , it generates a file called messages.js in your public folder (default setting). This file does not only contain your translations, but also lang.js itself. All you have to do is include this into your layout file like this:

and then use it somewhere in your code like this: Lang.get('whatever.some_key');

tebulrich avatar Sep 23 '21 13:09 tebulrich

any REAL solutions??

hasan-ozbey avatar Jun 12 '22 20:06 hasan-ozbey

I fixed this by changing file type to json, and then running php artisan lang:js --json

In app.js you have to manually load Lang lib, and set localised json as massages:

import Lang from 'lang.js';
import translations from './vue-translations.json'; // path to your json file

let lang = new Lang();
lang.setFallback('en');
lang.setMessages(translations);

// You can optionally set different user locale based on session or url param f.e.:
lang.setLocale(window.location.href.split(/[/]/)[3].toLowerCase());

Vue.filter('trans', (...args) => {
    return lang.get(...args);
});

export default lang;

new Vue({
    router,
    lang, // <-- dont forget to include it
    store,
    render: h => h(App),
}).$mount('#app');

This is probably not the best implementation or be perfect fit for you, but it's a start. I run around 20 different languages for my Vue frontend with this config.

Mult1Hunter avatar Jul 13 '22 09:07 Mult1Hunter

@Mult1Hunter How can I implement your solution with React ?

AbdoulBaguiM avatar Nov 15 '22 22:11 AbdoulBaguiM