xiCO2k / laravel-vue-i18n

Allows to connect your `Laravel` Framework translation files with `Vue`.
MIT License
598 stars 49 forks source link

I failed to change locale to "en"... #32

Closed PetroGromovo closed 2 years ago

PetroGromovo commented 2 years ago

I need to add i18n support in Laravel 9/Inertiajs2/vuejs3 app and I try to use "laravel-vue-i18n": "^1.4.4". I added 2 dirs AppPath/lang/en and AppPath/lang/ua and in resources/js/app.js I added i18n support :

import { i18nVue } from 'laravel-vue-i18n'
...
createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) => require(`./Pages/${name}.vue`),
    setup({ el, app, props, plugin }) {
        return createApp({ render: () => h(app, props) })
            .use(plugin)
            .use(i18nVue, {
                resolve: lang => import(`../../lang/${lang}.json`),
            })

            .component('inertia-link', Link)
            ...
            .mixin({ methods: { route } })
            .mount(el);
    },
});

and in webpack.mix.js :

require('laravel-vue-i18n/mix');

mix.js('resources/js/app.js', 'public/js')
    .i18n()
    .vue(3)

As result I have 2 generated files in public/js/lang_php_en_json.js and public/js/lang_php_ua_json.js with all labels from AppPath/lang/ and english label is applyed in vue file with {{ $t(' method.

I need to set ua as default language. I set in config/app.php : 'locale' => 'ua', it works.

But I failed to change locale to en with control method :

    public function change_locale(Request $request)
    {
        App::setLocale($request->lang);
        session()->put('locale', $request->lang);

        $app_locale = app()->getLocale();
        \Log::info(  $app_locale ); // it shows “en”

        return response()->json([
            'lang'    => $request->lang,
            'message' => 'Language was successfully changed',
        ], 200);

    }

On client side I still see "ua" labels have I to set changes locale on js side somehow ?

Thanks in advance!

xiCO2k commented 2 years ago

Hey Petro yes, since you are doing that with an AJAX request, you may need to use the: https://github.com/xiCO2k/laravel-vue-i18n#loadlanguageasynclang-string functions as it is shown on the README.

If you want to control all from the backend you just need to make sure the <html lang=""> is set to the correct one.

Hope that helps.