指南

每个组件的翻译

在组件中内联翻译消息。

如果您想为每个页面或每个组件定义翻译,可以利用 i18n 自定义块。

现在您可以在 Vue 文件中使用 i18n 自定义块来定义翻译:

page.vue
<script setup lang="ts">
const { t } = useI18n({
  useScope: 'local'
})
</script>

<template>
  <p>{{ t('hello') }}</p>
</template>

<i18n lang="json">
{
  "en": {
    "hello": "hello world!"
  },
  "ja": {
    "hello": "こんにちは、世界!"
  }
}
</i18n>

或者使用 Yaml 语法:

page.vue
<!-- 与上面的脚本和模板相同 -->
<i18n lang="yaml">
en:
  hello: 'hello world!'
ja:
  hello: 'こんにちは、世界!'
</i18n>
了解更多关于 i18n 自定义块
当您使用每个组件的翻译时,您需要使用由 useI18n() 导出的 t()而不是 $t()。 要了解更多关于不在每个组件翻译中使用的 $t(),请参阅 Vue I18n 文档中的“注入属性和函数的隐式”部分