xiCO2k / laravel-vue-i18n

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

Console error when there is no .json translation file available #100

Closed Jacobtims closed 1 year ago

Jacobtims commented 1 year ago

When I don't have a .json translation file in my ./lang folder, I get an error in the console.

Specifications:

Situation:

Console Error:

Uncaught (in promise) TypeError: langs[lang] is not a function
    at resolve (app.js:24:66)
    at avoidException (avoid-exceptions.js:3:16)
    at I18n.resolveLangAsync (index.js:185:20)
    at index.js:159:18
    at new Promise (<anonymous>)
    at I18n.loadLanguageAsync (index.js:155:16)
    at I18n.load (index.js:129:61)
    at new I18n (index.js:113:14)
    at I18n.getSharedInstance (index.js:311:84)
    at Object.install (index.js:83:44)

The error is caused by this line of code in the app.js: return await langs[`../../lang/${lang}.json`]();

I added a temporary workaround: if (typeof langs[`../../lang/${lang}.json`] == "undefined") return;

App.js

import './bootstrap';
import '../css/app.css';

import { createApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/vue3';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import { ZiggyVue } from '../../vendor/tightenco/ziggy/dist/vue.m';

import {i18nVue, trans} from 'laravel-vue-i18n'

const appName = window.document.getElementsByTagName('title')[0]?.innerText || 'Laravel';

createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
    setup({ el, App, props, plugin }) {
        const myApp =  createApp({ render: () => h(App, props) })
            .use(plugin)
            .use(ZiggyVue, Ziggy)
            .use(i18nVue, {
                resolve: async lang => {
                    const langs = import.meta.glob('../../lang/*.json');
                    if (typeof langs[`../../lang/${lang}.json`] == "undefined") return; //Temporary workaround
                    return await langs[`../../lang/${lang}.json`]();
                }
            });
        myApp.config.globalProperties.__ = trans;
        myApp.mount(el);
    },
    progress: {
        color: '#4B5563',
    },
});
xiCO2k commented 1 year ago

Good case, will debug that.

xiCO2k commented 1 year ago

Hey @Jacobtims just tried and did not found any bug.

Check this repo: https://github.com/xiCO2k/laravel-vue-i18n-app