Skip to content

this.$forceUpdate does not refresh the whole page when choosing another language #22

@scarneros

Description

@scarneros

Expected behavior

All Vue components on the page are updated when the language is changed.

Current behavior

Only the language selector is updated and the rest of the page remains the same. this.$forceUpdate() seems not to work.

Versions

  • Laravel: 8.43.0
  • Matice: 1.1.4
  • Vue: 3.0.5

Description

The backend package works fine and the translations on the frontend, when I change the language on the Laravel config side, works wonderfully too. The problem here is that when I change the language with a selector I have created on the frontend, the this.$forceUpdate() method won't refresh all components on the page, so the translation only applies to the selector itself and nowhere else on the page.

I am using Vue3 if that matters. Here is my code on app.js:

...

createApp({
    render: () =>
        h(InertiaApp, {
            initialPage: JSON.parse(el.dataset.page),
            resolveComponent: (name) => require(`./Pages/${name}`).default,
        }),
})
    .mixin({
        methods: {
            route,
            $trans: trans,
            $__: __,
            $transChoice: transChoice,
            $setLocale(locale) {
                if (this.$locale() !== locale) {
                    setLocale(locale);
                    this.$forceUpdate(); // Refresh the vue instance(The whole app in case of SPA) after the locale changes.
                }
            },
            // The current locale
            $locale() {
                return getLocale()
            },
            // A listing of the available locales
            $locales() {
                return locales()
            }
        }
    })
    .use(InertiaPlugin)
    .mount(el);
    
    ...

and this is the code that calls the $setLocale(locale) method on my language selector component:

...

<select v-on:change="$setLocale($event.target.value)" v-model="selectedLang">
    <option v-for="(lang, code) in languages" :value="code">
        {{ lang }}
    </option>
</select>

...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions