指南
按组件翻译
在你的组件中内联你的翻译消息。
如果你想为每个页面或每个组件定义翻译,你可以利用 i18n 自定义块。
你现在可以在 Vue 文件中使用 i18n 自定义块定义翻译:
<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 语法:
<i18n lang="yaml">
en:
hello: 'hello world!'
ja:
hello: 'こんにちは、世界!'
</i18n>
阅读更多关于 i18n 自定义块
当你使用按组件翻译时,你需要使用
t
,而不是 $t
,它是由 useI18n
导出的。
要了解更多关于 $t
的信息,这在按组件翻译中不使用,请参见 Vue I18n 文档中的“隐式与注入属性和函数”部分。