DEV Community

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

Collapse
 
4unkur profile image
Daiyrbek Artelov

Hi @Alecs,

Make sure you have lodash imported and globally available. I have my imports in /resources/js/bootstrap.js file which is then imported in the app.js file:

import _ from 'lodash'
window._ = _
Enter fullscreen mode Exit fullscreen mode

I've put it after the registration of components in app.js. Is this correct?

Yes, I have them right there in bootstrap.js file after components registration.

Collapse
 
aefika profile image
Luis Nicho

I think it is better to import the single function you need from lodash instead of importing the whole library: window._forEach = require('lodash/forEach');

Collapse
 
morpheus_ro profile image
Alecs • Edited

hey @4unkur ,

thank you for the reply first of all.
I was having the require bootstrap commented out.

I have one last question and that is what is the idea behind this hole scripts/logic? we cache the translations one time for life and then use it in the app? how can a person extend this to switch the languages if only one language is cached?

Thread Thread
 
sandman83 profile image
Sandman • Edited

@Alecs:
I solved this problem by combining this solution with the solution described here:
laraveldaily.com/multi-language-ro...

Then, the problem of once-in-life caching is still there, however, you have a middleware, which you can use for all necessary routes.
I moved the content of the TranslationServiceProvider's boot method into the handle method of the SetLocale middleware, this granted the calls to Cache::rememberForever in a regular manner.

My technical debt:
I observed, that
$tmpLoc = app()->getLocale();
$tmpSeg = $request->segment(1);
behave not as expected inside the handle method of SetLocal. I.e. while
$tmpSeg = $request->segment(1);
behaves as expected,
$tmpLoc = app()->getLocale();
sometimes overtakes and is set to the current language even before the line
app()->setLocale($request->segment(1));

The consequence is, that the cache is not refreshed in all circumstances it should be. However, I added another field to the cache wherein I track the current language manually. I check this field inside the handle method instead of app()->getLocale(), compare it to $request->segment(1) and if they do not match I call Cache::forget('translations');

So, in the end, I assume, the middleware handle method is the proper place to apply the translation, but there are calls somewhere, which change the app localization even before the middleware is called.

Thread Thread
 
4unkur profile image
Daiyrbek Artelov

@Alecs

Thanks for pointing out. I was busy and not able to check it until now and I can confirm that the solution is not working as expected. I'll probably update the article, but currently, I have no idea how can I solve it. Do you have any ideas?

@Sandman

Your solution seems to be in the right direction, I'll check that too.

Thanks.

Thread Thread
 
4unkur profile image
Daiyrbek Artelov

@morpheus_ro , @sandman83
Please check the updated version guys