### Install @intlify/core-base
Source: https://vue-i18n.intlify.dev/guide/advanced/lite
Commands to install the core-base package for advanced resolution features.
```sh
npm install --save @intlify/core-base@next
```
```sh
yarn add @intlify/core-base@next
```
```sh
pnpm add @intlify/core-base@next
```
--------------------------------
### Run Nuxt 3 Development Server
Source: https://vue-i18n.intlify.dev/guide/integrations/nuxt3
Commands to start the development server.
```sh
npm run dev
```
```sh
yarn dev
```
```sh
pnpm dev
```
--------------------------------
### Use Legacy API
Source: https://vue-i18n.intlify.dev/guide/migration/vue2
Setup example for using the legacy API with vue-i18n-bridge in Vue 2.7.
```js
import Vue from 'vue'
import { createI18n, useI18n } from 'vue-i18n-bridge'
Vue.use(VueI18n, { bridge: true }) // you must specify '{ bridge: true }' plugin option when install vue-i18n
// `createI18n` options is almost same vue-i18n (vue-i18n@v9.x) API
const i18n = createI18n({
locale: 'ja',
messages: {
en: {
message: {
hello: 'hello, {name}!'
}
},
ja: {
message: {
hello: 'こんにちは、{name}!'
}
}
}
}, VueI18n) // `createI18n` which is provide `vue-i18n-bridge` has second argument, you **must** pass `VueI18n` constructor which is provide `vue-i18n`
Vue.use(i18n) // you must install `i18n` instance which is created by `createI18n`
const app = new Vue({ i18n })
app.$mount('#app')
```
--------------------------------
### Install vue-i18n-loader
Source: https://vue-i18n.intlify.dev/guide/advanced/sfc
Install the loader required for Quasar CLI configuration.
```sh
npm install @intlify/vue-i18n-loader -D
```
```sh
yarn add @intlify/vue-i18n-loader -D
```
```sh
pnpm add -D @intlify/vue-i18n-loader
```
--------------------------------
### Use Composition API
Source: https://vue-i18n.intlify.dev/guide/migration/vue2
Setup examples for using the Composition API with vue-i18n-bridge in Vue 2.7 and 2.6.
```js
import Vue from 'vue'
import { createApp } from 'vue-demi'
import { createI18n, useI18n } from 'vue-i18n-bridge'
Vue.use(VueI18n, { bridge: true }) // you must specify '{ bridge: true }' plugin option when install vue-i18n
// `createI18n` options is almost same 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) // `createI18n` which is provide `vue-i18n-bridge` has second argument, you **must** pass `VueI18n` constructor which is provide `vue-i18n`
const app = createApp({
setup() {
// `useI18n` options is almost same vue-i18n (vue-i18n@v9.x) API
const { t, locale } = useI18n()
// ... todo something
return { t, locale }
}
})
app.use(i18n) // you must install `i18n` instance which is created by `createI18n`
app.mount('#app')
```
```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 }) // you must specify '{ bridge: true }' plugin option when install vue-i18n
// `createI18n` options is almost same 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) // `createI18n` which is provide `vue-i18n-bridge` has second argument, you **must** pass `VueI18n` constructor which is provide `vue-i18n`
const app = createApp({
setup() {
// `useI18n` options is almost same vue-i18n (vue-i18n@v9.x) API
const { t, locale } = useI18n()
// ... todo something
return { t, locale }
}
})
app.use(i18n) // you must install `i18n` instance which is created by `createI18n`
app.mount('#app')
```
--------------------------------
### Initialize petite-vue-i18n in browser
Source: https://vue-i18n.intlify.dev/guide/advanced/lite
Setup the i18n instance and mount the Vue application using global variables.
```html
```
--------------------------------
### Install petite-vue-i18n via package managers
Source: https://vue-i18n.intlify.dev/guide/advanced/lite
Commands to add the package to your project dependencies.
```sh
npm install petite-vue-i18n@next --save
```
```sh
yarn add petite-vue-i18n@next
```
```sh
pnpm add petite-vue-i18n@next
```
--------------------------------
### Use useI18n in Vue Component setup
Source: https://vue-i18n.intlify.dev/guide/migration/vue3
Demonstrates using the useI18n hook within the setup option or script setup syntax.
```vue
{{ $t('hello') }} {{ t('hello') }} {{ $t('hello') }} {{ t('hello') }}
$10,000.00
¥10,000
¥10000
$988M
99%
99.12%
12.12
12,145,000,000
``` -------------------------------- ### Install unplugin-vue-i18n Source: https://vue-i18n.intlify.dev/guide/advanced/sfc Install the required plugin for Vite or Webpack support. ```sh npm install @intlify/unplugin-vue-i18n -D ``` ```sh yarn add @intlify/unplugin-vue-i18n -D ``` ```sh pnpm add -D @intlify/unplugin-vue-i18n ``` -------------------------------- ### Message format syntax example Source: https://vue-i18n.intlify.dev/guide/migration/breaking Example of message format syntax using special characters. ```text @.caml:{'no apples'} | {0} apple | {n} apples ``` -------------------------------- ### Install vue-i18n-bridge Source: https://vue-i18n.intlify.dev/guide/migration/vue2 Commands to add the bridge package using various package managers. ```sh npm install vue-i18n-bridge ``` ```sh yarn add vue-i18n-bridge ``` ```sh pnpm add vue-i18n-bridge ``` -------------------------------- ### Install Vue I18n via package managers Source: https://vue-i18n.intlify.dev/guide/installation Commands to install the latest version of Vue I18n using common package managers. ```sh npm install vue-i18n@11 ``` ```sh yarn add vue-i18n@11 ``` ```sh pnpm add vue-i18n@11 ``` -------------------------------- ### Basic HTML localization example Source: https://vue-i18n.intlify.dev/guide/advanced/component Demonstrates the cumbersome manual approach to localizing messages containing HTML tags. ```htmlI accept xxx Terms of Service Agreement
``` ```js const messages = { en: { term1: 'I Accept xxx\'s', term2: 'Terms of Service Agreement' } } ``` ```html{{ $t('term1') }}{{ $t('term2') }}
``` ```htmlI accept xxx Terms of Service Agreement
``` -------------------------------- ### Install Vue I18n Dependency Source: https://vue-i18n.intlify.dev/guide/integrations/nuxt3 Commands to add vue-i18n as a development dependency. ```sh npm install vue-i18n -D ``` ```sh yarn add vue-i18n -D ``` ```sh pnpm add -D vue-i18n ``` -------------------------------- ### Register the i18n plugin Source: https://vue-i18n.intlify.dev/guide/essentials/started Install the i18n instance as a Vue plugin before mounting the application. ```js const app = createApp(Vue) app.use(i18n) app.mount('#app') ``` -------------------------------- ### Define I18n Setup and Lazy Loading Functions Source: https://vue-i18n.intlify.dev/guide/advanced/lazy Configures the I18n instance and provides utility functions for switching languages and dynamically importing locale messages. ```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) { if (i18n.mode === 'legacy') { i18n.global.locale = locale } else { 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) } export async function loadLocaleMessages(i18n, locale) { // load locale messages with dynamic import const messages = await import( /* webpackChunkName: "locale-[request]" */ `./locales/${locale}.json` ) // set locale and locale message i18n.global.setLocaleMessage(locale, messages.default) return nextTick() } ``` -------------------------------- ### Basic useI18n implementation in App.vue Source: https://vue-i18n.intlify.dev/guide/advanced/composition Initialize useI18n within the script setup block to access translation functions. ```vuemessage: {{ t('messages.hello', { name: 'kazupon' }) }}
currency: {{ n(1000, 'currency') }}
``` -------------------------------- ### Change Global Locale in Script Setup Source: https://vue-i18n.intlify.dev/guide/advanced/composition Use the locale property from the global useI18n instance to update the application locale. ```vue ``` -------------------------------- ### Create Plugins Directory and File Source: https://vue-i18n.intlify.dev/guide/integrations/nuxt3 Commands to prepare the directory and file for the Nuxt plugin. ```sh mkdir plugins ``` ```sh touch plugins/i18n.ts ``` -------------------------------- ### Initialize Nuxt 3 Application Source: https://vue-i18n.intlify.dev/guide/integrations/nuxt3 Commands to scaffold a new Nuxt 3 project. ```sh npx nuxi init nuxt3-app-vue-i18n ``` ```sh pnpm dlx nuxi init nuxt3-app-vue-i18n ``` -------------------------------- ### Hello World Script Source: https://vue-i18n.intlify.dev/guide/advanced/lite Initialization of petite-vue-i18n with a simple message object. ```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') ``` -------------------------------- ### Create locale directory and files Source: https://vue-i18n.intlify.dev/guide/integrations/nuxt3 Commands to set up the directory structure and JSON files for storing translations. ```sh mkdir locales ``` ```sh touch locales/en.json # for english touch locales/fr.json # for french touch locales/ja.json # for japanese ``` -------------------------------- ### Hello World Template Source: https://vue-i18n.intlify.dev/guide/advanced/lite Basic HTML structure for displaying translated text. ```html{{ $t('message.hello', 'ja') }}
``` ```vue{{ $t('message.hello', {}, { locale: 'ja' }) }}
``` -------------------------------- ### Translate simple keys in templates Source: https://vue-i18n.intlify.dev/guide/migration/breaking10 Migration of $tc to $t for simple key translation. ```vue{{ $tc('banana') }}
``` ```vue{{ $t('banana', 1) }}
``` -------------------------------- ### Import Vue I18n via CDN Source: https://vue-i18n.intlify.dev/guide/installation Load Vue and Vue I18n globally or as ES modules using unpkg CDN links. ```html ``` ```html ``` ```html ``` -------------------------------- ### Translate with list interpolation and locale Source: https://vue-i18n.intlify.dev/guide/migration/breaking10 Compares the $t signature for list-based interpolation between v9.x and v10. ```vue{{ $t('message.hello', 'ja', ['dio']) }}
``` ```vue{{ $t('message.hello', ['dio'], { locale: 'ja' }) }}
``` -------------------------------- ### Implement Web Component with useI18n Source: https://vue-i18n.intlify.dev/guide/advanced/wc A standard Vue component using the Composition API to access translations. ```vue{{ t('hello') }}
``` -------------------------------- ### Configure Message Resolver and Fallbacker Source: https://vue-i18n.intlify.dev/guide/advanced/lite Registering vue-i18n compatible resolvers at the application entry point. ```js import { createApp } from 'vue' import { createI18n, registerMessageResolver, // register the message resolver API registerLocaleFallbacker, // register the locale fallbacker API } from 'petite-vue-i18n' import { resolveValue, // message resolver of vue-i18n which is used by default fallbackWithLocaleChain // locale fallbacker of vue-i18n which is used by default } from '@intlify/core-base' // register message resolver of vue-i18n registerMessageResolver(resolveValue) // register locale fallbacker of vue-i18n registerLocaleFallbacker(fallbackWithLocaleChain) // some thing code ... // ... ``` -------------------------------- ### Vite Plugin Configuration Source: https://vue-i18n.intlify.dev/guide/advanced/lite Setting the module option in the Vite plugin for petite-vue-i18n. ```diff // vite.config.ts import { defineConfig } from 'vite' import { resolve, dirname } from 'node:path' import { fileURLToPath } from 'url' import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite' export default defineConfig({ /* ... */ plugins: [ /* ... */ VueI18nPlugin({ /* options */ + module: 'petite-vue-i18n', // locale messages resource pre-compile option include: resolve(dirname(fileURLToPath(import.meta.url)), './path/to/src/locales/**'), }), ], }) ``` -------------------------------- ### List Interpolation Syntax Source: https://vue-i18n.intlify.dev/guide/migration/breaking Transitioning from array-like objects to standard arrays for list interpolation parameters. ```javascript import VueI18n from 'vue-i18n' const i18n = new VueI18n({ // ... messages: { en: { greeting: 'hello, {0}!' } }, // ... }) ``` ```html{{ $t('greeting', { '0': 'kazupon' }) }}
``` ```html{{ $t('greeting', ['kazupon']) }}
``` -------------------------------- ### Configure unplugin-vue-i18n for SSR Source: https://vue-i18n.intlify.dev/guide/advanced/optimization Enable SSR support by setting the ssr option to true in the Vite plugin configuration. ```js // vite.config.ts import { defineConfig } from 'vite' import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite' export default defineConfig({ plugins: [ VueI18nPlugin({ ssr: true, // Enable SSR support }), ], }) ``` -------------------------------- ### Using translation functions in templates Source: https://vue-i18n.intlify.dev/guide/advanced/composition Demonstrates using both the global $t and the local t function returned by useI18n in a component template. ```vue{{ $t('hello') }}
``` ```htmlHello, world!
``` -------------------------------- ### Localize Datetime in Templates Source: https://vue-i18n.intlify.dev/guide/essentials/datetime Use the $d helper to format dates in Vue templates. ```html{{ $d(new Date(), 'short') }}
{{ $d(new Date(), 'long', 'ja-JP') }}
``` ```htmlApr 19, 2017
2017年4月19日(水) 午前2:19
``` -------------------------------- ### Export I18n Host as Custom Element Source: https://vue-i18n.intlify.dev/guide/advanced/wc Use defineCustomElement to export the host component for use as a Web Component. ```js import { defineCustomElement } from 'vue' import I18nHost from './components/I18nHost.ce.vue' const I18nHostElement = defineCustomElement(I18nHost) export { I18nHostElement } ``` -------------------------------- ### Array-based Fallback Locales Source: https://vue-i18n.intlify.dev/guide/essentials/fallback Configure multiple fallback locales using an array. ```javascript fallbackLocale: [ 'fr', 'en' ], ``` -------------------------------- ### Create an I18n Host Component Source: https://vue-i18n.intlify.dev/guide/advanced/wc This component creates an i18n instance and provides it to child components using the I18nInjectionKey. ```vuehi there!
hi there!