I have created localization from Laravel to vue.js, but after login it appers on the screen

  laravel, php

Route:

Route::get('/lang-{lang}.js', [AppHttpControllersLanguageController::class, 'showw']);

app.blade.php (file is always loaded)

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

LanguageController.php

public function __construct()
{
    $this->middleware('auth');
}

public function showw($locale)
{
    if (is_array(config('app.locale'))) {
        $locale = in_array($locale, config('app.locale')) ? $locale : config('app.fallback_locale');

    } else {
        $locale = $locale == config('app.locale') ? $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;
}

LoginContoller.php

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = RouteServiceProvider::HOME;

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest')->except('logout');
}

app.js

import _ from 'lodash'

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

It works perfectly, but I face problem after login, it opens lang file sourse on display, and I should redirect manually, it is strange, because after register it doesn’t show up source file.

after login view

Source: Laravel

Leave a Reply