### Clone Repository and Install Dependencies (Bash) Source: https://github.com/kazupon/vue-i18n/blob/v8.x/examples/integration/contentful/docs/GETTING-STARTED.md This snippet shows the commands to clone the project repository from GitHub and install all necessary project dependencies using npm. ```bash git clone git@github.com:contentful/blog-in-5-minutes.git cd blog-in-5-minutes npm install ``` -------------------------------- ### Start Development Server (Bash) Source: https://github.com/kazupon/vue-i18n/blob/v8.x/examples/integration/contentful/docs/GETTING-STARTED.md This command starts the local development server for the project, typically accessible at localhost:3000, allowing you to preview your site with the imported Contentful data. ```bash npm run dev ``` -------------------------------- ### HTML Setup for Vue I18n Source: https://github.com/kazupon/vue-i18n/blob/v8.x/docs/started.html This snippet shows how to include Vue.js and Vue I18n via CDN in an HTML file. It sets up a basic Vue instance with a div element where translated messages will be displayed using the $t helper. ```html
{{ $t("message.hello") }}
{{ $t("message.hello") }}
こんにちは、世界
{{ $tc('car', 1) }}
{{ $tc('car', 2) }}
{{ $tc('car', 4) }}
{{ $tc('car', 12) }}
{{ $tc('car', 21) }}
{{ $tc('banana', 0) }}
{{ $tc('banana', 4) }}
{{ $tc('banana', 11) }}
{{ $tc('banana', 31) }}
``` -------------------------------- ### Vue-i18n Initialization with Locale Messages Source: https://github.com/kazupon/vue-i18n/blob/v8.x/vuepress/api/README.md Example of initializing VueI18n with locale messages. This setup defines the translations for different languages, which are then used by the 'v-t' directive and '$t' method. The 'messages' option is used during VueI18n instantiation. ```javascript import Vue from 'vue'; import VueI18n from 'vue-i18n'; Vue.use(VueI18n); const messages = { en: { tos: 'Term of Service', term: 'I accept xxx {0}.' }, ja: { tos: '利用規約', term: '私は xxx の{0}に同意します。' } }; const i18n = new VueI18n({ locale: 'en', messages }); new Vue({ i18n, data: { url: '/term' } }).$mount('#app'); ``` -------------------------------- ### Custom Formatter Implementation in Vue-i18n Source: https://github.com/kazupon/vue-i18n/blob/v8.x/vuepress/guide/formatting.md Provides an example of implementing a custom formatter in Vue-i18n using ES2015 class syntax. This allows for advanced interpolation logic, such as ICU message syntax, and requires implementing the `interpolate` method. ```javascript // Custom Formatter implementation class CustomFormatter { constructor (options) { // ... } interpolate (message, values) { // implement interpolation logic here // ... // return the interpolated array return ['resolved message string'] } } // register with `formatter` option const i18n = new VueI18n({ locale: 'en-US', formatter: new CustomFormatter(/* here the constructor options */), messages: { 'en-US': { // ... }, // ... } }) // Run! new Vue({ i18n }).$mount('#app') ``` -------------------------------- ### Vue-i18n Configuration and Vue Instance Setup Source: https://github.com/kazupon/vue-i18n/blob/v8.x/examples/datetime/index.html This JavaScript code configures vue-i18n with messages and datetime formats for 'en-US' and 'ja-JP' locales. It then initializes a Vue instance, injecting the i18n configuration. The instance manages the current locale, 'now', and 'pastDatetime' data properties, and updates the 'now' data property every second. It also includes a watcher to update the i18n locale when the component's locale changes. ```javascript var messages = { 'en-US': { current_datetime: 'current datetime', past_datetime: 'past datetime', past_year: 'past year' }, 'ja-JP': { current_datetime: '現在の日時', past_datetime: '過去の日時', past_year: '過去の年' } } var dateTimeFormats = { 'en-US': { long: { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', timeZone: 'America/Los_Angeles' } }, 'ja-JP': { long: { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true, timeZone: 'Asia/Tokyo' } } } Vue.use(VueI18n) var initial = 'ja-JP' var i18n = new VueI18n({ locale: initial, messages: messages, dateTimeFormats: dateTimeFormats }) var vm = new Vue({ i18n: i18n, data: { locale: initial, now: new Date(), pastDatetime: new Date(Date.UTC(2012, 11, 20, 3, 0, 0)) }, watch: { locale: function (val) { this.$i18n.locale = val } } }).$mount('#app') setInterval(function () { vm.now = new Date() }, 1000) ``` -------------------------------- ### Vue I18n Template for Accessing Pluralization Count Source: https://github.com/kazupon/vue-i18n/blob/v8.x/docs/guide/pluralization.html Shows Vue template examples that utilize the $tc() method to display pluralized strings, specifically demonstrating how the {count} and {n} arguments are used to control the output based on the provided number and optional data. ```html{{ $tc('apple', 10, { count: 10 }) }}
{{ $tc('apple', 10) }}
{{ $tc('banana', 1, { n: 1 }) }}
{{ $tc('banana', 1) }}
{{ $tc('banana', 100, { n: 'too many' }) }}
``` -------------------------------- ### Install Vue I18n via Yarn Source: https://github.com/kazupon/vue-i18n/blob/v8.x/docs/installation.html Installs the Vue I18n package using the Yarn package manager. This is an alternative to NPM for managing project dependencies. ```bash yarn add vue-i18n ``` -------------------------------- ### Vue-i18n Initialization with Number Formats Source: https://github.com/kazupon/vue-i18n/blob/v8.x/vuepress/api/README.md Example of initializing VueI18n with custom number formats. This configuration allows for locale-specific currency, percentage, or other numeric representations. The 'numberFormats' option is used during VueI18n instantiation. ```javascript import Vue from 'vue'; import VueI18n from 'vue-i18n'; Vue.use(VueI18n); const numberFormats = { 'en-US': { currency: { style: 'currency', currency: 'USD' } }, 'ja-JP': { currency: { style: 'currency', currency: 'JPY' } } }; const i18n = new VueI18n({ locale: 'en-US', numberFormats }); new Vue({ i18n, data: { money: 10234 } }).$mount('#app'); ``` -------------------------------- ### Install vue-i18n-loader Source: https://github.com/kazupon/vue-i18n/blob/v8.x/docs/guide/sfc.html Installs the necessary vue-i18n-loader for handling custom i18n blocks in Vue single file components. This is a development dependency. ```bash npm i --save-dev @intlify/vue-i18n-loader@next ``` -------------------------------- ### Vue I18n Installation with Vue CLI Source: https://github.com/kazupon/vue-i18n/blob/v8.x/docs/installation.html Adds Vue I18n to a project using the Vue CLI plugin. This command assumes Vue CLI 3.x is installed globally. ```bash vue add i18n ``` -------------------------------- ### Include Vue I18n via CDN Source: https://github.com/kazupon/vue-i18n/blob/v8.x/docs/installation.html Includes Vue.js and Vue I18n directly from CDNs using script tags. This method is suitable for simple projects or quick testing. ```html ``` -------------------------------- ### Vue I18n CDN with Specific Version Source: https://github.com/kazupon/vue-i18n/blob/v8.x/docs/installation.html Includes a specific version of Vue I18n from a CDN. This allows for more control over the exact version used in the project. ```html ``` -------------------------------- ### Vue SFC with i18n Custom Block Example Source: https://github.com/kazupon/vue-i18n/blob/v8.x/gitbook/en/sfc.md Demonstrates a basic Vue single file component utilizing the `message: {{ $t('hello') }}
{{ $tc('car', 1) }}
{{ $tc('car', 2) }}
{{ $tc('apple', 0) }}
{{ $tc('apple', 1) }}
{{ $tc('apple', 10, { count: 10 }) }}
``` -------------------------------- ### Vue-i18n: Fallback Interpolation Example Source: https://github.com/kazupon/vue-i18n/blob/v8.x/vuepress/guide/fallback.md This JavaScript setup for Vue-i18n enables `formatFallbackMessages: true`. This allows translation keys themselves to act as templates, useful when a direct translation is missing but the key string can be used for interpolation. ```javascript const messages = { ru: { 'Hello {name}': 'Здравствуйте {name}' } } const i18n = new VueI18n({ locale: 'ru', fallbackLocale: 'en', formatFallbackMessages: true, messages }) ``` -------------------------------- ### Vue-i18n Initialization and Data Setup (JavaScript) Source: https://github.com/kazupon/vue-i18n/blob/v8.x/gitbook/en/api.md Shows the JavaScript code for initializing Vue-i18n with messages for different locales and setting up a Vue instance with data. This code is essential for enabling internationalization in a Vue application. ```javascript const messages = { en: { tos: 'Term of Service', term: 'I accept xxx {0}.' }, ja: { tos: '利用規約', term: '私は xxx の{0}に同意します。' } } const i18n = new VueI18n({ locale: 'en', messages }) new Vue({ i18n, data: { url: '/term' } }).$mount('#app') ``` -------------------------------- ### Set global language configuration in Vue-i18n Source: https://github.com/kazupon/vue-i18n/wiki/Usage-for-v3.0 Demonstrates how to get or set the current translation language code using `Vue.config.lang`. The default language is 'en'. This allows for dynamic control over the application's language. Requires vue-i18n to be installed. ```javascript Vue.config.lang = 'ja' ``` -------------------------------- ### VueI18n Class - Constructor Options Source: https://github.com/kazupon/vue-i18n/blob/v8.x/docs/api/index.html Details on the options that can be passed when creating a new VueI18n instance. ```APIDOC ## VueI18n Class - Constructor Options ### Description Options that can be provided when initializing the `VueI18n` constructor. These options are based on the `I18nOptions` interface. #### locale - **Type:** `Locale` - **Default:** `'en-US'` - **Description:** The primary locale for localization. Can include territory and dialect (e.g., 'en-US', 'fr-FR'). #### fallbackLocale - **Type:** `FallbackLocale` - **Default:** `false` - **Description:** The locale to fall back to if a translation is not found in the current locale. Supports complex fallback chains as described in the fallback guide. ``` -------------------------------- ### Component-Based Localization Setup with Vue-i18n Source: https://github.com/kazupon/vue-i18n/blob/v8.x/vuepress/guide/component.md This JavaScript code demonstrates setting up locale information for a root Vue instance and a specific component using Vue-i18n. It shows how translations are applied and how components can fallback to global locale messages if their own messages are not defined. ```javascript const i18n = new VueI18n({ locale: 'ja', messages: { en: { message: { hello: 'hello world', greeting: 'good morning' } }, ja: { message: { hello: 'こんにちは、世界', greeting: 'おはようございます' } } } }) // Define component const Component1 = { template: `Component1 locale messages: {{ $t("message.hello") }}
Fallback global locale messages: {{ $t("message.greeting") }}
{{ $tc('car', 1) }}
{{ $tc('car', 2) }}
{{ $tc('apple', 0) }}
{{ $tc('apple', 1) }}
{{ $tc('apple', 10, { count: 10 }) }}
``` -------------------------------- ### Vue I18n Hot Reloading - Basic Example Source: https://github.com/kazupon/vue-i18n/blob/v8.x/docs/guide/hot-reload.html This JavaScript code demonstrates basic hot reloading for a static set of locales in Vue I18n. It uses Webpack's `module.hot.accept` to explicitly update locale messages when locale files change. Ensure you have Vue and VueI18n installed. ```javascript import Vue from "vue" import VueI18n from "vue-i18n" import en from './en' import ja from './ja' const messages = { en, ja } // VueI18n instance const i18n = new VueI18n({ locale: 'en', messages }) // Run app const app = new Vue({ i18n, // ... }).$mount('#app') // Hot updates if (module.hot) { module.hot.accept(['./en', './ja'], function () { i18n.setLocaleMessage('en', require('./en').default) i18n.setLocaleMessage('ja', require('./ja').default) // Or the following hot updates via $i18n property // app.$i18n.setLocaleMessage('en', require('./en').default) // app.$i18n.setLocaleMessage('ja', require('./ja').default) }) } ``` -------------------------------- ### v-t Directive Examples Source: https://github.com/kazupon/vue-i18n/blob/v8.x/gitbook/en/api.md Demonstrates the usage of the 'v-t' directive for localizing element text content. Supports both string syntax for direct keypaths and object syntax for more complex configurations including locale, arguments, and pluralization choices. This directive requires Vue I18n version 7.3 or higher. ```html ``` -------------------------------- ### Register locale with an external source and callback Source: https://github.com/kazupon/vue-i18n/wiki/Usage-for-v3.0 Illustrates registering a locale dynamically by providing a function that fetches locale data and a callback to set the language after successful loading. This method is useful for asynchronous locale loading. Requires vue-i18n. ```javascript // register with external locale Vue.locale('ja', function () { return fetch('/locales/ja', { method: 'get', // ... }).then(function (json) { return Promise.resolve(json) }).catch(function (error) { return Promise.reject() }) }, function () { Vue.config.lang = 'ja' }) ``` -------------------------------- ### Vue I18n Initialization and Hot Reload Setup Source: https://github.com/kazupon/vue-i18n/blob/v8.x/docs/guide/hot-reload.html This JavaScript code initializes a VueI18n instance with loaded messages and sets up hot module replacement to dynamically update locale messages. It defines a `loadMessages` function, creates a VueI18n instance, mounts the Vue application, and registers a callback for `module.hot.accept` to handle new message loading and apply changes to the i18n instance. Dependencies include Vue and VueI18n. ```javascript const { context, messages } = loadMessages(); // VueI18n instance const i18n = new VueI18n({ locale: "en", messages, }); // Run app const app = new Vue({ i18n, // ... }).$mount('#app'); // Hot updates if (module.hot) { module.hot.accept(context.id, () => { const { messages: newMessages } = loadMessages(); Object.keys(newMessages) .filter((locale) => messages[locale] !== newMessages[locale]) .forEach((locale) => { messages[locale] = newMessages[locale]; i18n.setLocaleMessage(locale, messages[locale]); }); }); } ``` -------------------------------- ### Vue-i18n Setup and Async Language Loading (JavaScript) Source: https://github.com/kazupon/vue-i18n/blob/v8.x/vuepress/guide/lazy-loading.md Sets up Vue-i18n with an initial locale and provides a function to asynchronously load new language files. It uses Webpack's dynamic import for code splitting and manages loaded languages to avoid redundant loading. Dependencies include Vue, VueI18n, and axios. ```javascript //i18n-setup.js import Vue from 'vue' import VueI18n from 'vue-i18n' import messages from '@/lang/en' import axios from 'axios' Vue.use(VueI18n) export const i18n = new VueI18n({ locale: 'en', // set locale fallbackLocale: 'en', messages // set locale messages }) const loadedLanguages = ['en'] // our default language that is preloaded function setI18nLanguage (lang) { i18n.locale = lang axios.defaults.headers.common['Accept-Language'] = lang document.querySelector('html').setAttribute('lang', lang) return lang } export function loadLanguageAsync(lang) { // If the same language if (i18n.locale === lang) { return Promise.resolve(setI18nLanguage(lang)) } // If the language was already loaded if (loadedLanguages.includes(lang)) { return Promise.resolve(setI18nLanguage(lang)) } // If the language hasn't been loaded yet return import(/* webpackChunkName: "lang-[request]" */ `@/lang/${lang}.js`).then( messages => { i18n.setLocaleMessage(lang, messages.default) loadedLanguages.push(lang) return setI18nLanguage(lang) } ) } ```