laravel - 如何在组件的脚本部分中使用vue-i18n转换功能

标签 laravel vue.js vue-i18n

我正在使用laravel-vue-i18n-generator包在laravel项目中的vuejs组件中处理文本翻译。我已经按如下所示设置了app.js:

import VueInternationalization from 'vue-i18n';
import Locale from './vue-i18n-locales.generated';

Vue.use(VueInternationalization);

const lang = 'fa';

const i18n = new VueInternationalization({
    locale: lang,
    messages: Locale
});

const app = new Vue({
    el: '#app',
    i18n,
});


在组件中:
<template>

    <a href="#" class="tip" title="" :title="$t('component.delete.title')" @click.prevent="deleteAction">
        <i :class="icon"></i>
    </a>
</template>

<script>
    import swal from 'sweetalert';
    import axios from 'axios';

    export default {
        inject: ['$i18n'],
        props:{
            endpoint: {
                type: String,
                required: true,
            },
            icon: {
                type: String,
                default: 'fa fa-trash'
            },
            message: {
                type: String,
                default:  this.$i18n.t('component.delete.are_you_sure'),
            },
            confirm: {
                type: String,
                default:  this.$i18n.t('component.delete.confirm'),
            },
            cancel: {
                type: String,
                default:  this.$i18n.t('component.delete.cancel'),
            },
            success: {
                type: String,
                default:  this.$i18n.t('component.delete.success'),
            },
            failed: {
                type: String,
                default:  this.$i18n.t('component.delete.failed'),
            },
        },
        mounted() {
            console.log(this);
        },
        methods:{
            deleteAction(){
                const vm = this;
                swal({
                    text: this.message,
                    buttons: {
                        catch: {
                            text: this.confirm,
                            value: "delete",
                        },
                        cancel: this.cancel
                    },
                    dangerMode: true
                }).then(name => {
                    if (!name) return false;

                    axios.delete(vm.endpoint)
                        .then(function (response) {
                            swal( vm.$i18n.t('component.delete.congrats'),vm.success, 'success').then(() => {
                                location.reload();
                            });
                        })
                        .catch(function (error) {
                            swal( vm.$i18n.t('component.delete.error'), vm.failed, 'error');
                        });
                });
            }
        }

    }
</script>

<style scoped>

</style>

幸运的是$t('component.delete.title')在模板部分可以正常工作,但是在脚本部分,我遇到了以下错误:

未捕获的TypeError:无法读取未定义的属性“t”

我哪里出错了?

最佳答案

在vue.js中,如果出现类似

"_vm.translate is not a function" It is most probably that you havent import the i18n package which contains translate method.This error occures sometimes when you try to add translate using v-bind to html attributes. Example:


 <a-form-item class="mb-0" :label="`${translate('person_name.firstname')}`">

以下步骤可以解决错误。
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.0/vue.js"></script>
<script lang="ts">
import { translate, i18n } from '@/i18n';

@Component({
  components: {
    AgIconBase
  },
 methods:{
   translate
   }
})
</script>

关于laravel - 如何在组件的脚本部分中使用vue-i18n转换功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56355592/

相关文章:

php - 使用另一个 "from" header 从 laravel 发送电子邮件

php - postgresql 中的最后三列值在通过 php laravel 发送时变为 null

php - laravel eloquent orm插入查询自动生成创建于并更新于与数据库不一致

javascript - vuejs 延迟加载组件,无需路由器

javascript - vue-i18n 日期本地化未本地化

laravel public/index.php - 哎呀,好像出了点问题

javascript - 我们如何将 Vue JS 与 ASP.NET MVC 和 nuget 包集成

javascript - Vue.js 获取组件内的元素

javascript - 我怎样才能设法用 vue-i18n 加载我真正需要的语言文件?

vue.js - Vue-router 以编程方式更新路由