Laravel-JS-Localization
Laravel-JS-Localization copied to clipboard
Laravel 8. Uncaught ReferenceError: Lang is not defined
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! :)
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');
any REAL solutions??
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 How can I implement your solution with React ?