Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translations not working inside layout (App.vue) #93

Closed
haztakki opened this issue Dec 26, 2022 · 8 comments
Closed

Translations not working inside layout (App.vue) #93

haztakki opened this issue Dec 26, 2022 · 8 comments

Comments

@haztakki
Copy link

Hello @xiCO2k,
Great package! It works really well, I have been using it for my upcoming project and it's been a breeze. Though I am running into a weird issue that I'm not sure how to solve. Translations don't seem to be working inside a layout (including nested components within). Everything inside the <slot /> seems to work perfectly. I have a feeling it's something to do with how I am using layouts with Inertia.

<!-- other relevant imports -->
import { i18nVue } from "laravel-vue-i18n";

createInertiaApp({
    resolve: (name) => {
        const page = resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob("./Pages/**/*.vue"));

        page.then((module) => {
            module.default.layout = module.default.layout || BaseLayout;
        });

        return page;
    },

    title: (title) => (title ? `${title} - App Name` : "App Name"),

    setup({ el, App, props, plugin }) {
        createApp({ render: () => h(App, props) })
            .mixin({ methods: { route } })
            .use(plugin)
            .use(i18nVue, {
                lang: "fi",
                resolve: async (lang) => {
                    const langs = import.meta.glob("../../lang/*.json");

                    return await langs[`../../lang/${lang}.json`]();
                },
            })
            .mount(el);
    },
});

The same translations that work elsewhere don't work here.

My App.vue just contains the usual boilerplate code (nav, global search, etc) so I won't paste it here.

<!-- lots of boilerplate code -->
<slot />

What's weird is that for pages that use the default layout (Base.vue), the translations work just fine.

I did some more digging... Could it be related to the workaround?

<script>
    import AppLayout from "@/Shared/Layouts/App.vue";

    export default {
        layout: AppLayout,
    };
</script>
<script setup>
...

inertiajs/inertia#651

Maybe you could give some light on this.

@xiCO2k
Copy link
Owner

xiCO2k commented Dec 26, 2022

Hey @hazj will investigate that ASAP

@kovinet
Copy link

kovinet commented Dec 27, 2022

I am having the same issue. On page load translations inside Vue Layout are not working, but if I click somewhere on a link so that the same page is loaded via Inertia router, then translations load).

image

@xiCO2k
Copy link
Owner

xiCO2k commented Jan 21, 2023

Hey @hazj did you already try this with inertia v1?

@haztakki
Copy link
Author

Hello,
Not yet. I will try it soon. I didn't see anything in the v1 changelog that would have an affect on this. I could be wrong though. I'll keep you posted.

@xiCO2k
Copy link
Owner

xiCO2k commented Jan 21, 2023 via email

@haztakki
Copy link
Author

It made no difference. I'll try to spend some time looking into it.

@e4se
Copy link

e4se commented Feb 6, 2023

@hazj do u find any solution how to fix it?? I have the same problem.

@nickvleeuwen
Copy link

For me the problem only occurred on initial page load just like @kovinet . I think this was because the translations where loaded after vue had mounted the page.

When logging something inside the onMounted callback of vue, and logging something inside the onLoad callback of this package, the onMounted log was shown first.

What I ended up doing was mounting the vue instance inside the onLoad callback of this package. I hope I don't destroy something by doing it this way :).

const app = createApp({ render: () => h(App, props) });

return app
.use(i18nVue, {
    resolve: async lang => {
        const langs = import.meta.glob('../../lang/*.json');
        return await langs[`../../lang/${lang}.json`]();
    },
    onLoad: () => {
        app.mount(el); // Mounted here so translations are loaded before vue.
    }
});

Maybe this comment helps some people that are having the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants