xiCO2k / laravel-vue-i18n

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

FallbackLocale not working #87

Closed Quadrubo closed 1 year ago

Quadrubo commented 1 year ago

The FallbackLocale option is not working correctly.

// config/app.php
'locale' => 'de',

 'fallback_locale' => 'en',
// resources/lang/en/app.php
return [

    'jetstream' => [

        'Forgot Password' => 'Forgot Password',
        'Two-factor Confirmation' => 'Two-factor Confirmation',
        'Email Verification' => 'Email Verification',

    ],

];
// resources/lang/de/app.php
<?php

return [

    'jetstream' => [

        // 'Forgot Password' => 'Passwort vergessen',
        'Two-factor Confirmation' => 'Zwei-Faktor-Authentifizierung',
        'Email Verification' => 'E-Mail-Verifizierung',

    ],

];
// resources/js/app.js
.use(i18nVue, {
    lang: 'de',
    fallbackLang: 'en',
    resolve: async lang => {
        const langs = import.meta.glob('../lang/*.json');
        return await langs[`../lang/${lang}.json`]();
    }
})

I would expect the trans function to use the fallbackLocale. Instead it echoes out the translation string.

// 'Forgot Password' is set in de/app.php, locale = 'de', fallbackLocale = 'en'
console.log(trans('app.jetstream.Forgot Password')); // -> Passwort vergessen

// 'Forgot Password' is not set in de/app.php, locale = 'de', fallbackLocale = 'en'
console.log(trans('app.jetstream.Forgot Password')); // -> app.jetstream.Forgot Password
xiCO2k commented 1 year ago

Will review that one thanks for the issue.

lenssoft commented 1 year ago

I am experiencing the same issue. I think it could be fixed in setLanguege method.

image

Instead of setting this.activeMessages[key] = null better use the fallbackLang and those messages to be kept in fallbackMessages this.activeMessages[key] = this.fallbackMessages[key] || null

It would comet with the drawback that fallback lang must always be loaded and extra memory usage due to keeping them in fallbackMessages

xiCO2k commented 1 year ago

Hey @lenssoft if you can, please submit a PR for that, and I will review it.

Thanks.

xiCO2k commented 1 year ago

Fixed on #103