### Vue I18n v12: Options API with setup() example Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/migration/breaking12.md Shows how to integrate Vue I18n's Composition API into an Options API component using the `setup()` function in v12. ```html ``` -------------------------------- ### Hello World Example with petite-vue-i18n Source: https://github.com/intlify/vue-i18n/blob/master/docs/zh/guide/advanced/lite.md Demonstrates a basic 'Hello world' translation using petite-vue-i18n. It shows how to configure messages and use the 't' function in a Vue component's setup. ```javascript const { createApp } = Vue const { createI18n, useI18n } = PetiteVueI18n // 或者对于 ES 模块 // import { createApp } from 'vue' // import { createI18n } from 'petite-vue-i18n' const i18n = createI18n({ locale: 'en', messages: { en: { 'hello world': 'Hello world!' }, ja: { 'hello world': 'こんにちは、世界!' } } }) // 定义 App 组件 const App = { setup() { const { t } = useI18n() return { t } } } const app = createApp(App) app.use(i18n) app.mount('#app') ``` -------------------------------- ### Vue-i18n Basic Setup with Petite-Vue Source: https://github.com/intlify/vue-i18n/blob/master/examples/petite/started.html Demonstrates the core setup for Vue-i18n using `createI18n` and `useI18n` within a Vue application. Ensure `useI18n` is called at the head of the `setup` function. ```javascript const { createApp } = Vue const { createI18n, useI18n } = PetiteVueI18n const i18n = createI18n({ locale: 'ja', messages: { en: { 'message.language': 'Language', 'message.hello': 'hello world!' }, ja: { 'message.language': '言語', 'message.hello': 'こんにちは、世界!' } } }) const app = createApp({ setup() { // `useI18n` return the global composer that is created at `createI18n` // `useI18n` must to be called at head of `setup` function const { t, locale } = useI18n() // Something to do ... // return { t, locale } } }) app.use(i18n) app.mount('#app') ``` -------------------------------- ### Initialize petite-vue-i18n with Vue.js (CDN) Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/advanced/lite.md This example shows how to integrate petite-vue-i18n into a Vue.js application when using CDN links. It demonstrates the basic setup for creating and mounting the Vue app with the i18n plugin. ```html ``` -------------------------------- ### Start JIT Compilation Preview Source: https://github.com/intlify/vue-i18n/blob/master/examples/backend/README.md Starts the preview server for i18n Just-In-Time (JIT) compilation. ```sh npm run preview # start preview ``` -------------------------------- ### Start Pre-compiled Preview Server Source: https://github.com/intlify/vue-i18n/blob/master/examples/backend/README.md Starts the preview server after i18n resources have been pre-compiled. ```sh npm run preview # start preview ``` -------------------------------- ### Install petite-vue-i18n using npm Source: https://github.com/intlify/vue-i18n/blob/master/docs/zh/guide/advanced/lite.md Install the petite-vue-i18n package using npm. The '@next' tag ensures you get the latest version. ```sh npm install petite-vue-i18n@next --save ``` -------------------------------- ### Vue.js Setup with petite-vue-i18n and Translation Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/advanced/lite.md This script sets up a Vue.js application using petite-vue-i18n, including defining messages for different locales and making the translation function `t` available in the component's setup. It demonstrates a 'Hello World' example with internationalization. ```js const { createApp } = Vue const { createI18n, useI18n } = PetiteVueI18n // or for ES modules // import { createApp } from 'vue' // import { createI18n } from 'petite-vue-i18n' const i18n = createI18n({ locale: 'en', messages: { en: { 'hello world': 'Hello world!' }, ja: { 'hello world': 'こんにちは、世界!' } } }) // define App component const App = { setup() { const { t } = useI18n() return { t } } } const app = createApp(App) app.use(i18n) app.mount('#app') ``` -------------------------------- ### Initialize petite-vue-i18n with Vue.js (Package Managers) Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/advanced/lite.md This example demonstrates how to import and initialize petite-vue-i18n within a Vue.js application when installed via package managers. It sets up the i18n instance and mounts the Vue application. ```js import { createApp } from 'vue' import { createI18n } from 'petite-vue-i18n' const i18n = createI18n({ // something vue-i18n options here ... }) const app = createApp({ // something vue options here ... }) app.use(i18n) app.mount('#app') ``` -------------------------------- ### Install petite-vue-i18n using pnpm Source: https://github.com/intlify/vue-i18n/blob/master/docs/zh/guide/advanced/lite.md Install the petite-vue-i18n package using pnpm. The '@next' tag ensures you get the latest version. ```sh pnpm add petite-vue-i18n@next ``` -------------------------------- ### Install petite-vue-i18n using yarn Source: https://github.com/intlify/vue-i18n/blob/master/docs/zh/guide/advanced/lite.md Install the petite-vue-i18n package using yarn. The '@next' tag ensures you get the latest version. ```sh yarn add petite-vue-i18n@next ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/intlify/vue-i18n/blob/master/examples/storybook/README.md Run this command to install all necessary project dependencies using pnpm. ```sh pnpm install ``` -------------------------------- ### Start JIT Compilation Server Source: https://github.com/intlify/vue-i18n/blob/master/examples/backend/README.md Starts the server for i18n Just-In-Time (JIT) compilation. ```sh npm run serve # start server ``` -------------------------------- ### CDN App Initialization Source: https://github.com/intlify/vue-i18n/blob/master/packages/petite-vue-i18n/README.md Initialize your Vue application using petite-vue-i18n with CDN scripts. This example shows basic setup with options. ```javascript const { createApp } = Vue const { createI18n } = PetiteVueI18n const i18n = createI18n({ // something vue-i18n options here ... }) const app = createApp({ // something vue options here ... }) app.use(i18n) app.mount('#app') ``` -------------------------------- ### Run Development Server Source: https://github.com/intlify/vue-i18n/blob/master/examples/backend/README.md Starts the development server for local testing. ```sh npm run dev ``` -------------------------------- ### Migrating Options API to Options API with setup() in v12 Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/migration/breaking12.md This snippet demonstrates migrating Options API code to a v12 compatible structure using the setup() function to expose Composition API bindings. ```javascript import { useI18n } from 'vue-i18n' export default { props: { user: { type: String, required: true }, lang: { type: String, default: 'en' } }, setup() { const { t, d, n, locale } = useI18n() return { t, d, n, i18nLocale: locale } }, computed: { message() { return this.t('welcome', { name: this.user }) } }, watch: { lang(val) { this.i18nLocale = val } }, methods: { greet() { return this.t('hello') } }, mounted() { console.log(this.t('ready')) console.log(this.d(new Date(), 'long')) console.log(this.n(1000, 'currency')) } } ``` -------------------------------- ### Vue I18n v12: Composition API component example Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/migration/breaking12.md Demonstrates using Vue I18n's Composition API within a Vue component's ` ``` -------------------------------- ### Install unplugin-vue-i18n Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/advanced/sfc.md Install the required bundler plugin for Vite or Webpack. ```sh npm install @intlify/unplugin-vue-i18n -D ``` ```sh yarn add @intlify/unplugin-vue-i18n -D ``` ```sh pnpm add -D @intlify/unplugin-vue-i18n ``` -------------------------------- ### Install unplugin-vue-i18n for pnpm Source: https://github.com/intlify/vue-i18n/blob/master/docs/zh/guide/advanced/sfc.md Install the unplugin-vue-i18n package as a development dependency using pnpm. ```sh pnpm add -D @intlify/unplugin-vue-i18n ``` -------------------------------- ### Directory Structure for Locales Source: https://github.com/intlify/vue-i18n/blob/master/docs/zh/guide/advanced/lazy.md Example project directory structure showing the placement of locale JSON files. ```txt ├── dist ├── index.html ├── package.json ├── src │ ├── App.vue │ ├── components │ ├── i18n.js │ ├── index.css │ ├── locales │ │ ├── en.json │ │ └── ja.json │ ├── main.js │ ├── pages │ │ ├── About.vue │ │ └── Home.vue │ └── router.js ``` -------------------------------- ### Install vue-i18n using deno Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/installation.md Use this command to install vue-i18n version 11 with deno. ```sh deno add vue-i18n@11 ``` -------------------------------- ### Install unplugin-vue-i18n for npm Source: https://github.com/intlify/vue-i18n/blob/master/docs/zh/guide/advanced/sfc.md Install the unplugin-vue-i18n package as a development dependency using npm. ```sh npm install @intlify/unplugin-vue-i18n -D ``` -------------------------------- ### Vue App Setup with Vue-i18n Source: https://github.com/intlify/vue-i18n/blob/master/examples/composition/fallback/component.html Sets up the Vue application and initializes Vue-i18n with global messages. The `useI18n` composable is used in the root component setup. ```javascript const { createApp } = Vue const { createI18n, useI18n } = VueI18n const i18n = createI18n({ locale: 'ja', messages: { en: { message: { hello: 'hello world', greeting: 'good morning, world!' } }, ja: { message: { hello: 'こんにちは、世界', greeting: 'おはよう、世界!' } } } }) const Component1 = { template: `

Component1

Component1 locale messages: {{ t("message.hello") }}

Fallback global locale messages: {{ t("message.greeting") }}

`, setup() { const { t } = useI18n({ messages: { en: { message: { hello: 'hello component1' } }, ja: { message: { hello: 'こんにちは、component1' } } } }) // Something to do ... // return { t } } } const app = createApp({ components: { Component1 }, setup() { const { t } = useI18n() // Something to do ... // return { t } } }) app.use(i18n) app.mount('#app') ``` -------------------------------- ### Install unplugin-vue-i18n for yarn Source: https://github.com/intlify/vue-i18n/blob/master/docs/zh/guide/advanced/sfc.md Install the unplugin-vue-i18n package as a development dependency using yarn. ```sh yarn add @intlify/unplugin-vue-i18n -D ``` -------------------------------- ### Install @intlify/core-base using npm Source: https://github.com/intlify/vue-i18n/blob/master/docs/zh/guide/advanced/lite.md Install the @intlify/core-base package using npm. This package provides advanced message resolvers and locale fallbackers that can be used with petite-vue-i18n. ```sh npm install --save @intlify/core-base@next ``` -------------------------------- ### Install @intlify/core-base using yarn Source: https://github.com/intlify/vue-i18n/blob/master/docs/zh/guide/advanced/lite.md Install the @intlify/core-base package using yarn. This package provides advanced message resolvers and locale fallbackers that can be used with petite-vue-i18n. ```sh yarn add @intlify/core-base@next ``` -------------------------------- ### Install @intlify/core-base using pnpm Source: https://github.com/intlify/vue-i18n/blob/master/docs/zh/guide/advanced/lite.md Install the @intlify/core-base package using pnpm. This package provides advanced message resolvers and locale fallbackers that can be used with petite-vue-i18n. ```sh pnpm add @intlify/core-base@next ``` -------------------------------- ### Install vue-i18n using npm Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/installation.md Use this command to install vue-i18n version 11 with npm. ```sh npm install vue-i18n@11 ``` -------------------------------- ### Install vue-i18n using pnpm Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/installation.md Use this command to install vue-i18n version 11 with pnpm. ```sh pnpm add vue-i18n@11 ``` -------------------------------- ### Install vue-i18n using yarn Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/installation.md Use this command to install vue-i18n version 11 with yarn. ```sh yarn add vue-i18n@11 ``` -------------------------------- ### Migrating Options API to ``` -------------------------------- ### Build Nuxt 3 Application for Production Source: https://github.com/intlify/vue-i18n/blob/master/examples/frameworks/nuxt3/README.md Execute this command to create an optimized production build of your Nuxt 3 application. This prepares the app for deployment. ```bash npm run build ``` -------------------------------- ### Use useI18n in Vue Component setup option Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/migration/vue3.md In Vue 3 components using the `setup` option, you can access the `t` function for translations by calling `useI18n()` within the `setup` function. This makes translations available globally within the component's scope. ```vue ``` -------------------------------- ### Vue I18n Configuration and Setup Source: https://github.com/intlify/vue-i18n/blob/master/examples/composition/formatting/named.html Initialize the i18n instance with messages containing named placeholders and integrate it into the Vue application. ```javascript const { createApp } = Vue const { createI18n, useI18n } = VueI18n const i18n = createI18n({ locale: 'ja', messages: { en: { message: { language: 'Language', greeting: 'Hello, {name}!' } }, ja: { message: { language: '言語', greeting: 'こんにちは、{name}!' } } } }) const app = createApp({ setup() { const { t, locale } = useI18n() // Something to do ... // return { t, locale } } }) app.use(i18n) app.mount('#app') ``` -------------------------------- ### Setup and Language Management for Vue I18n Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/advanced/lazy.md Provides functions to initialize vue-i18n, set the current language, and manage HTML lang attribute. Use `setupI18n` for initialization and `setI18nLanguage` for updating the UI language. ```javascript import { nextTick } from 'vue' import { createI18n } from 'vue-i18n' export const SUPPORT_LOCALES = ['en', 'ja'] export function setupI18n(options = { locale: 'en' }) { const i18n = createI18n(options) setI18nLanguage(i18n, options.locale) return i18n } export function setI18nLanguage(i18n, locale) { i18n.global.locale.value = locale /** * NOTE: * If you need to specify the language setting for headers, such as the `fetch` API, set it here. * The following is an example for axios. * * axios.defaults.headers.common['Accept-Language'] = locale */ document.querySelector('html').setAttribute('lang', locale) } ``` -------------------------------- ### Vue-i18n Setup with Named Interpolation Source: https://github.com/intlify/vue-i18n/blob/master/examples/petite/functions/named.html This snippet shows the setup for Vue-i18n using named interpolation with message functions. It includes locale configuration and message definitions for English and Japanese. ```javascript const { createApp } = Vue const { createI18n, useI18n } = PetiteVueI18n const i18n = createI18n({ locale: 'ja', messages: { en: { 'message.language': 'Language', 'message.greeting': ctx => `Hello, ${ctx.named('name')}!` }, ja: { 'message.language': '言語', 'message.greeting': ctx => `こんにちは、${ctx.named('name')}!` } } }) const app = createApp({ setup() { const { t, locale } = useI18n() // Something to do ... // return { t, locale } } }) app.use(i18n) app.mount('#app') ``` -------------------------------- ### Create Locales Directory Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/integrations/nuxt.md Initialize a directory to store external locale resource files. ```sh mkdir locales ``` -------------------------------- ### Use n composable in ``` -------------------------------- ### Console Warnings for Missing Keys Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/essentials/local.md Example of console output when a key is missing in the local scope and falls back to global. ```txt [intlify] Not found 'message.greeting' key in 'ja' locale messages. [intlify] Fall back to translate 'message.greeting' with root locale. ``` -------------------------------- ### Vue 2.6 Composition API Usage with vue-i18n-bridge Source: https://github.com/intlify/vue-i18n/blob/master/docs/jp/guide/migration/vue2.md Demonstrates setting up vue-i18n-bridge with Composition API for Vue 2.6. Ensure you install @vue/composition-api and use the '{ bridge: true }' option when installing vue-i18n. Pass the VueI18n constructor to createI18n. ```js import Vue from 'vue' import VueCompositionAPI, { createApp } from '@vue/composition-api' import { createI18n, useI18n } from 'vue-i18n-bridge' Vue.use(VueCompositionAPI) Vue.use(VueI18n, { bridge: true }) // vue-i18n をインストールするときに '{ bridge: true }' プラグインオプションを指定する必要があります // `createI18n` オプションは vue-i18n (vue-i18n@v9.x) API とほぼ同じです const i18n = createI18n({ legacy: false, locale: 'ja', messages: { en: { message: { hello: 'hello, {name}!' } }, ja: { message: { hello: 'こんにちは、{name}!' } } } }, VueI18n) // `vue-i18n-bridge` が提供する `createI18n` には 2 番目の引数があり、`vue-i18n` が提供する `VueI18n` コンストラクターを渡す**必要があります** const app = createApp({ setup() { // `useI18n` オプションは vue-i18n (vue-i18n@v9.x) API とほぼ同じです const { t, locale } = useI18n() // ... 何かする return { t, locale } } }) app.use(i18n) // `createI18n` によって作成された `i18n` インスタンスをインストールする必要があります app.mount('#app') ``` -------------------------------- ### Use useI18n in Vue Component with ``` -------------------------------- ### Vue-i18n Setup with Custom Plural Rule (JavaScript) Source: https://github.com/intlify/vue-i18n/blob/master/examples/composition/plural/custom.html Initializes Vue-i18n with a custom plural rule for the Russian locale. It includes messages for 'car' and 'banana' with plural variations defined by the custom rule. This setup is necessary for the custom pluralization to work within the Vue application. ```javascript const { createApp } = Vue const { createI18n, useI18n } = VueI18n function customRule(choice, choicesLength, orgRule) { if (choice === 0) { return 0 } const teen = choice > 10 && choice < 20 const endsWithOne = choice % 10 === 1 if (!teen && endsWithOne) { return 1 } if (!teen && choice % 10 >= 2 && choice % 10 <= 4) { return 2 } return choicesLength < 4 ? 2 : 3 } const i18n = createI18n({ locale: 'ru', pluralRules: { ru: customRule }, messages: { ru: { car: '0 машин | {n} машина | {n} машины | {n} машин', banana: 'нет бананов | {n} банан | {n} банана | {n} бананов' } } }) const app = createApp({ setup() { const { t } = useI18n() // Something to do ... // return { t } } }) app.use(i18n) app.mount('#app') ``` -------------------------------- ### Create i18n instance Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/advanced/composition.md Initialize the i18n instance with locales and messages. ```js // ... const i18n = VueI18n.createI18n({ locale: 'ja', fallbackLocale: 'en', messages: { en: { message: { hello: 'hello world' } }, ja: { message: { hello: 'こんにちは、世界' } } } }) // ... ``` -------------------------------- ### Expected Output of Local Scope Example Source: https://github.com/intlify/vue-i18n/blob/master/docs/guide/essentials/local.md The rendered HTML output demonstrating local and global scope message resolution. ```html

Root

こんにちは、世界

Component1 locale messages: こんにちは、component1

Fallback global locale messages: おはよう、世界!

```