vue-i18n-skilld
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesevue-i18n 11.4.12 (Vue 3)
vue-i18n 11.4.12 (Vue 3)
Internationalization plugin for Vue.js.
- Peer dependency:
vue(package.json:44). No Vue 2 support; use vue-i18n v8 for Vue 2.^3.0.0 - Node engines: (package.json:47).
>= 22 - For Nuxt 3, use instead of installing this directly (https://vue-i18n.intlify.dev/guide/integrations/nuxt3.html).
@nuxtjs/i18n
Vue.js 的国际化插件。
- 对等依赖:
vue(package.json:44)。不支持 Vue 2;Vue 2 请使用 vue-i18n v8。^3.0.0 - Node 引擎版本:(package.json:47)。
>= 22 - 对于 Nuxt 3,请使用 ,而不是直接安装此包(https://vue-i18n.intlify.dev/guide/integrations/nuxt3.html)。
@nuxtjs/i18n
Core rules
核心规则
- Default to Composition API mode. Pass to
legacy: falseand usecreateI18n. Legacy API mode (useI18n(), the default) is deprecated in v11 and removed in v12 (dist/vue-i18n.d.ts:1728).legacy: true - Do not use the directive in new code. Deprecated in v11, removed in v12 (dist/vue-i18n.d.ts:2138). Use
v-t,t(), or the$tcomponent.<i18n-t> - and
tcno longer exist. Dropped in v11;$tc/thandle pluralization via the plural option (verified: no$texport in dist/vue-i18n.d.ts).tc - Interpolation uses syntax. The v8 modulo
{name}syntax was dropped in v10 (https://vue-i18n.intlify.dev/guide/migration/breaking10.html#drop-modulo-syntax).% - Messages are compiled at runtime (JIT) by default since v10. For CSP-strict environments or best performance, pre-compile with (https://vue-i18n.intlify.dev/guide/advanced/optimization.html).
@intlify/unplugin-vue-i18n - Never interpolate user input into messages rendered with without
v-html; translate content, do not concatenate HTML.escapeParameter: true
- 默认使用 Composition API 模式。 向 传入
createI18n,并使用legacy: false。Legacy API 模式(useI18n(),默认值)在 v11 中已弃用,并将在 v12 中移除(dist/vue-i18n.d.ts:1728)。legacy: true - 新代码中不要使用 指令。 该指令在 v11 中已弃用,并将在 v12 中移除(dist/vue-i18n.d.ts:2138)。请改用
v-t、t()或$t组件。<i18n-t> - 和
tc已不存在。 它们在 v11 中被移除;$tc/t可通过 plural 选项处理复数(已验证:dist/vue-i18n.d.ts 中没有$t导出)。tc - 插值使用 语法。 v8 的取模
{name}语法在 v10 中被移除(https://vue-i18n.intlify.dev/guide/migration/breaking10.html#drop-modulo-syntax)。% - 自 v10 起,消息默认在运行时编译(JIT)。 在严格 CSP 环境或需要最佳性能时,使用 进行预编译(https://vue-i18n.intlify.dev/guide/advanced/optimization.html)。
@intlify/unplugin-vue-i18n - 绝不要在使用 渲染的消息中插值用户输入,除非设置
v-html。 应翻译内容,不要拼接 HTML。escapeParameter: true
Setup
设置
js
// i18n.js
import { createI18n } from 'vue-i18n'
const i18n = createI18n({
legacy: false, // Composition API mode
locale: 'ja',
fallbackLocale: 'en',
messages: {
en: { message: { hello: 'hello world' } },
ja: { message: { hello: 'こんにちは、世界' } }
}
})
export default i18njs
// main.js
import { createApp } from 'vue'
import App from './App.vue'
import i18n from './i18n'
createApp(App).use(i18n).mount('#app')app.use(i18n)$t$d$n$rtglobalInjection: trueuseI18ni18n-ti18n-di18n-nglobalInstall: truejs
// i18n.js
import { createI18n } from 'vue-i18n'
const i18n = createI18n({
legacy: false, // Composition API mode
locale: 'ja',
fallbackLocale: 'en',
messages: {
en: { message: { hello: 'hello world' } },
ja: { message: { hello: 'こんにちは、世界' } }
}
})
export default i18njs
// main.js
import { createApp } from 'vue'
import App from './App.vue'
import i18n from './i18n'
createApp(App).use(i18n).mount('#app')app.use(i18n)$t$d$n$rtglobalInjection: trueuseI18ni18n-ti18n-di18n-nglobalInstall: trueTranslate in a component
在组件中翻译
vue
<script setup>
import { useI18n } from 'vue-i18n'
const { t, locale } = useI18n()
</script>
<template>
<h1>{{ t('message.hello') }}</h1>
<p>{{ t('greeting', { name: 'Kyoko' }) }}</p>
<p>{{ $t('message.hello') }}</p><!-- works in templates via globalInjection -->
</template>Call inside only. It throws "Must be called at the top of a function" otherwise.
useI18n()setupsetupvue
<script setup>
import { useI18n } from 'vue-i18n'
const { t, locale } = useI18n()
</script>
<template>
<h1>{{ t('message.hello') }}</h1>
<p>{{ t('greeting', { name: 'Kyoko' }) }}</p>
<p>{{ $t('message.hello') }}</p><!-- works in templates via globalInjection -->
</template>只能在 内部调用 。否则会抛出 "Must be called at the top of a function"。
setupuseI18n()setupCommon tasks
常见任务
Pluralize. Message: (pipe-separated forms, selected by plural rules for the locale).
'no apples | one apple | {count} apples'js
t('apple', count, { count }) // or t('apple', { count }, choice)Custom rules: on options (Composition), (Legacy).
pluralRulesComposerpluralizationRulesFormat dates and numbers.
js
const { d, n } = useI18n()
d(new Date(), 'long') // named format from datetimeFormats option, else Intl fallback
n(1234567, 'currency') // named format from numberFormats optionInterpolate components. Use with slot children per placeholder; see components.
<i18n-t keypath="..." tag="p">Switch locale. from a Composer; also update the document attribute and, on SSR, the handling (https://vue-i18n.intlify.dev/guide/advanced/lazy.html).
locale.value = 'en'langAccept-LanguageLazy load locales. Dynamic plus / ; see optimization.
import()setLocaleMessagemergeLocaleMessage复数化。 消息:(使用竖线分隔各种形式,并根据 locale 的复数规则进行选择)。
'no apples | one apple | {count} apples'js
t('apple', count, { count }) // or t('apple', { count }, choice)自定义规则:Composition 模式在 选项中使用 ,Legacy 模式使用 。
ComposerpluralRulespluralizationRules格式化日期和数字。
js
const { d, n } = useI18n()
d(new Date(), 'long') // named format from datetimeFormats option, else Intl fallback
n(1234567, 'currency') // named format from numberFormats option插值组件。 使用 ,并通过 slot 子节点提供各占位符对应的内容;请参阅组件。
<i18n-t keypath="..." tag="p">切换 locale。 通过 Composer 设置 ;同时更新文档的 属性。在 SSR 环境中,还需要处理 (https://vue-i18n.intlify.dev/guide/advanced/lazy.html)。
locale.value = 'en'langAccept-Language懒加载 locale。 使用动态 ,配合 /;请参阅优化。
import()setLocaleMessagemergeLocaleMessageType-safe keys
类型安全的键
Augment the global , , interfaces to get key completion and inference in without generics; see typescript.
DefineLocaleMessageDefineDateTimeFormatDefineNumberFormatuseI18n扩展全局 、、 接口,无需使用泛型即可在 中获得键补全和类型推断;请参阅 TypeScript。
DefineLocaleMessageDefineDateTimeFormatDefineNumberFormatuseI18nPitfalls
常见问题
- creates a local scope; without
useI18n({ messages: ... })local messages do not merge with global ones (https://vue-i18n.intlify.dev/guide/essentials/scope.html).useScope: 'global' - in templates requires
$tin Composition mode (dist/vue-i18n.d.ts:1735-1746).globalInjection: true - Vue warns about a missing message with key fallback to the key itself; configure
locale/missing/missingWarnto control noise.fallbackWarn - Linked messages (), literal
@:key{, and}@in messages need escaping; see message-syntax.| - Do not import from deep paths such as in bundler projects; use the package root so the correct build resolves (package.json:63-83).
vue-i18n/dist/*
- 会创建局部作用域;如果未设置
useI18n({ messages: ... }),局部消息不会与全局消息合并(https://vue-i18n.intlify.dev/guide/essentials/scope.html)。useScope: 'global' - 在 Composition 模式下,模板中使用 需要启用
$t(dist/vue-i18n.d.ts:1735-1746)。globalInjection: true - 当 locale 消息缺失并回退到键本身时,Vue 会发出警告;可通过配置 /
missing/missingWarn控制噪声。fallbackWarn - 链接消息()以及消息中的字面量
@:key{、}、@需要转义;请参阅消息语法。| - 在打包项目中,不要从 等深层路径导入;应从包根目录导入,以确保正确解析对应的构建产物(package.json:63-83)。
vue-i18n/dist/*
Migration status
迁移状态
| Change | Version | Action |
|---|---|---|
| Legacy API mode deprecated | v11 | Move to |
| v11 | Replace with |
| v11 | Use |
| JIT message compilation default | v10 | Pre-compile for strict CSP |
| v10 | Use |
| v10 | Pass plural via options object |
Details: migration.
| 变更 | 版本 | 操作 |
|---|---|---|
| Legacy API 模式已弃用 | v11 | 在 v12 之前迁移到 |
| v11 | 替换为 |
| v11 | 使用 |
| 默认启用 JIT 消息编译 | v10 | 为严格 CSP 环境进行预编译 |
已移除 | v10 | 使用 |
| v10 | 通过选项对象传入复数选项 |
详细信息:迁移。
References
参考资料
- composition-api: createI18n, useI18n, Composer members, scopes, locale switching
- message-syntax: interpolation, pluralization, linked messages, HTML safety
- components: i18n-t, i18n-n, i18n-d
- formats: datetime and number formatting, custom format definitions
- typescript: schema typing, generated locale types
- optimization: tree shaking flags, pre-compilation, lazy loading, dist files
- migration: v9, v10, v11 breaking changes and v12 removals
- legacy-api: Legacy API mode surface still present in v11
Official documentation: https://vue-i18n.intlify.dev/
API reference: https://vue-i18n.intlify.dev/api/general.html
Changelog (cited, not copied): https://github.com/intlify/vue-i18n/blob/master/CHANGELOG.md
- composition-api:createI18n、useI18n、Composer 成员、作用域、locale 切换
- message-syntax:插值、复数化、链接消息、HTML 安全
- components:i18n-t、i18n-n、i18n-d
- formats:日期时间和数字格式化、自定义格式定义
- typescript:模式类型定义、生成的 locale 类型
- optimization:tree shaking 标志、预编译、懒加载、dist 文件
- migration:v9、v10、v11 的破坏性变更和 v12 的移除项
- legacy-api:v11 中仍然保留的 Legacy API 模式接口