【问题标题】:Spatie Laravel Translation Loader with Inertia JS带有 Inertia JS 的 Spatie Laravel 翻译加载器
【发布时间】:2021-09-29 06:11:03
【问题描述】:

因为我需要将翻译文本存储在数据库中以便用户可以更新它,所以我使用 spatie/laravel-translation-loader 包。在我的应用程序中,我将 Inertia JS 和 Vue 2 结合在一起。我正在寻找有关如何有效地将翻译数据传递给 Vue 组件的建议。

谢谢。

【问题讨论】:

    标签: laravel vue.js translation inertiajs


    【解决方案1】:

    我使用了以下策略:

    我分享了以下翻译数据:

    class HandleInertiaRequests extends Middleware
    ...
    public function share(Request $request)
    {
    ...
    
    'trans' => [
            'common' =>   LanguageLine::getTranslationsForGroup(app()->currentLocale(),'common')
        ],
    ...
    }
    ...
    

    创建了 trans() 函数和全局 mixin

    export default function(group, key) {
        let value = _.get(this.$page.props.trans[group], key);
        if (value) {
            return value;
        } else {
           return key;
        }
    }
    
    import trans from '@/utils/trans';
    Vue.prototype.$trans = trans;
    Vue.mixin({
        methods: {
            trans: trans
        },
    });
    

    在 vue 组件中使用 trans() 为:

    <template>
    
    ...
     <h1> trans('common', 'dashboard') </h1>
    ...
    </template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-19
      相关资源
      最近更新 更多