Hi everyone,
this is my first post here. I'm using Laravel for on of my projects and I need an advise about translations. In blade templates I'm using @lang and everything works perfect. The problem is that I have some strings in js files and I don't know how to translate them. I saw there are some packages but don't know if they are reliable. I'll appreciate any help. Thanks
Are you using a js framework or anything? I usually just send the translation data to the javascript in the blade template. This is easy if you're using something like Vue.
You can't, not directly. If you want to access it at runtime from within js
Either have an ajax-route setup for your translations.
Place them at some place on your html file, so that you can retrieve it from within js.
// In blade file
<meta name="en.messages.hello" content="@lang('messages.hello',[], 'en')">
<meta name="fr.messages.hello" content="@lang('messages.hello',[], 'fr')">
// Now you can easily access them from js.
$('[data-confirm]').on('click', function (e) {
var message = $(this).data('confirm'); // Will use translated string from Blade
if (! confirm(message)) {
e.preventDefault();
e.stopImmediatePropagation();
}
});