Reputation: 87
Hi so if I use {{$t('dash.port')}}
inside of template the translation happens and everything works fine.
Now I have an antdv table where i have columns declared this way :
const columns = [
{
title:"pone",
dataIndex: 'pone',
key: 'pone',
},
...
]
//Here's the antdv table component :
<template>
<a-table :data-source="data" :columns="columns">
<template #filterDropdown="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }">
<div style="padding: 8px">
<a-input
ref="searchInput"
:placeholder="`Search ${column.dataIndex}`"
:value="selectedKeys[0]"
style="width: 188px; margin-bottom: 8px; display: block"
@change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
@pressEnter="handleSearch(selectedKeys, confirm, column.dataIndex)"
/>
<a-button
type="primary"
size="small"
style="width: 90px; margin-right: 8px"
@click="handleSearch(selectedKeys, confirm, column.dataIndex)"
>
<template #icon><SearchOutlined /></template>
Search
</a-button>
<a-button size="small" style="width: 90px" @click="handleReset(clearFilters)">
Reset
</a-button>
</div>
</template>
<template #filterIcon="filtered">
<search-outlined :style="{ color: filtered ? '#108ee9' : undefined }" />
</template>
<template #customRender="{ text, column }">
<span v-if="searchText && searchedColumn === column.dataIndex">
<template
v-for="(fragment, i) in text
.toString()
.split(new RegExp(`(?<=${searchText})|(?=${searchText})`, 'i'))"
>
<mark
v-if="fragment.toLowerCase() === searchText.toLowerCase()"
class="highlight"
:key="i"
>
{{ fragment }}
</mark>
<template v-else>{{ fragment }}</template>
</template>
</span>
<template v-else>
{{ text }}
</template>
</template>
</a-table>
//script part where $t not working
<script>
import { SearchOutlined } from '@ant-design/icons-vue';
import { defineComponent, reactive, ref } from 'vue';
const data = [
{
key: '1',
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park',
},
..
];
export default defineComponent({
components: {
SearchOutlined,
},
setup() {
const state = reactive({
searchText: '',
searchedColumn: '',
});
const searchInput = ref();
const columns = [
{
title: 'pone',
dataIndex: 'pone',
key: 'pone',
slots: {
filterDropdown: 'filterDropdown',
filterIcon: 'filterIcon',
customRender: 'customRender',
},
onFilter: (value, record) =>
record.pone.toString().toLowerCase().includes(value.toLowerCase()),
onFilterDropdownVisibleChange: visible => {
if (visible) {
setTimeout(() => {
console.log(searchInput.value);
searchInput.value.focus();
}, 0);
}
},
},
....
];
const handleSearch = (selectedKeys, confirm, dataIndex) => {
confirm();
state.searchText = selectedKeys[0];
state.searchedColumn = dataIndex;
};
const handleReset = clearFilters => {
clearFilters();
state.searchText = '';
};
return {
data,
columns,
handleSearch,
handleReset,
searchText: '',
searchInput,
searchedColumn: '',
};
},
});
</script>
What I want is to change title using $t but when I do title:"$t('dash.pone')",
I get $t not defined. How can I make this work?
Upvotes: 3
Views: 5465
Reputation: 108
With vue3 and and composition API i had the same issue, i solve the problem with this
Import i18n (change the path)
import i18n from '@/plugins/i18n'
Then for accessing the $t function
i18n.global.t("WordToTranslate")
Upvotes: 1
Reputation: 11
Ah, I see, you are using new Vue3 composition API. Well, vue-i18n is a bit behind, but there is repo for the next version 9. Upgrade the package and follow its migration instructions, then use your translations in setup functions like this:
import { defineComponent, reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
setup() {
const { tm } = useI18n();
const columns = [
{
title: tm('dash.pone'),
dataIndex: 'pone',
key: 'pone',
// ...
},
];
];
Upvotes: 1
Reputation: 46732
I did not learnt vue3 yet so I am not sure on how it works but you should probably give a look to all the examples down there: https://github.com/intlify/vue-i18n-next/tree/master/examples/composition
But maybe this one is working?
const app = createApp({
setup() {
const { t, locale } = useI18n()
t('dash.port') // this one maybe works ?
return { t, locale }
}
})
Upvotes: 1