2

嗨,如果我{{$t('dash.port')}}在模板内部使用,翻译就会发生并且一切正常。现在我有一个 antdv 表,其中我有这样声明的列:

const columns = [
  {
    title:"pone",
    dataIndex: 'pone',
    key: 'pone',
  },
    ...
    ]

//这里是antdv表格组件:

    <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>
//$t 不工作的脚本部分
<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>

我想要的是使用 $t 更改标题,但是当我这样做时,title:"$t('dash.pone')",我得到 $t 未定义。我怎样才能使这项工作?

4

3 回答 3

1

您可以通过导入i18n来使用vue-i18n外部模板(外部组件),然后使用i18n.global中的“ t ” 。

t ”不需要“ $ ”,您可以使用i18n.global.locale更改区域设置

import i18n from '../i18n';
    
const { t } = i18n.global;
    
i18n.global.locale = 'en-US'; // Change "Locale"
    
const data = { 
  name: t('name'), // "t" doesn't need "$"
  description: t('description'), // "t" doesn't need "$"
};
于 2021-09-26T09:34:21.280 回答
1

我还没有学习 vue3,所以我不确定它是如何工作的,但你可能应该看看下面的所有示例:https ://github.com/intlify/vue-i18n-next/tree/master/examples /作品

但也许这个有效?

const app = createApp({
  setup() {
    const { t, locale } = useI18n()

    t('dash.port') // this one maybe works ?

    return { t, locale }
  }
})
于 2021-05-03T15:36:14.803 回答
1

啊,我明白了,您正在使用新的 Vue3 组合 API。嗯,vue-i18n 有点落后,但是下一个版本 9有 repo 。升级包并遵循其迁移说明,然后在设置函数中使用您的翻译,如下所示:

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',
      // ...
    },
  ];
];

于 2021-05-03T13:22:55.130 回答