Я использую пакет laravel-vue-i18n-generator для обработки перевода текста в компоненте vuejs в моем проекте laravel. Я настроил 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.info(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')
работает правильно в части шаблона, но в части скрипта у меня есть эта ошибка:
Uncaught TypeError: невозможно прочитать свойство 't' неопределенного
Где я ошибаюсь?
Это работает для меня.
У меня есть папка локалей с index.js, импортирующая два языковых файла, которые я использую, в этом файле доп.
global.$t = Vue.t
Упоминается в части сценария напрямую как
return $t('backend.faq.main')
Вы должны импортировать «i18n» в свой компонент.
<script>
import i18n from '@/i18n'
{
...
data: () => {
return {
message: i18n.t('message'),
}
}
...
}
</script>
Это сработало для меня внутри скриптовой части компонента:
this.$t('message')
В 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>