• This repository has been archived on 05/Jul/2021
  • Stars
    star
    130
  • Rank 277,575 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🌐 Vite plugin for Vue I18n

THIS REPOSITORY IS DEPRECATED (MOVE TO HERE)

Test npm

Vite plugin for Vue I18n


⭐ Features

  • i18n resources pre-compilation
  • i18n custom block
  • Static bundle importing
  • Bundling optimizations

đŸ’ŋ Installation

NPM

$ npm i --save-dev @intlify/vite-plugin-vue-i18n

YARN

$ yarn add -D @intlify/vite-plugin-vue-i18n

⚠ī¸ Notice

When this plugin is installed, Vue I18n can only use the Composition API, and if you want to use the Legacy API, you need to set the compositionOnly option to false.

Also, if you do a production build with vite, Vue I18n will automatically bundle the runtime only module. If you need on-demand compilation with Message compiler, you need to set the runtimeOnly option to false.

🚀 Usage

i18n resources pre-compilation

Since [email protected], the locale messages are handled with message compiler, which converts them to javascript functions after compiling. After compiling, message compiler converts them into javascript functions, which can improve the performance of the application.

However, with the message compiler, the javascript function conversion will not work in some environments (e.g. CSP). For this reason, [email protected] and later offer a full version that includes compiler and runtime, and a runtime only version.

If you are using the runtime version, you will need to compile before importing locale messages by managing them in a file such as .json.

Vite Config

the below example that examples/composition/vite.config.ts:

import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueI18n from '@intlify/vite-plugin-vue-i18n'

export default defineConfig({
  plugins: [
    vue(), // you need to install `@vitejs/plugin-vue`
    vueI18n({
      // if you want to use Vue I18n Legacy API, you need to set `compositionOnly: false`
      // compositionOnly: false,

      // you need to set i18n resource including paths !
      include: path.resolve(__dirname, './path/to/src/locales/**')
    })
  ]
})

i18n custom block

The below example that examples/composition/App.vue have i18n custom block:

<template>
  <form>
    <label>{{ t('language') }}</label>
    <select v-model="locale">
      <option value="en">en</option>
      <option value="ja">ja</option>
    </select>
  </form>
  <p>{{ t('hello') }}</p>
</template>

<script>
import { useI18n } from 'vue-i18n'

export default {
  name: 'App',
  setup() {
    const { locale, t } = useI18n({
      inheritLocale: true
    })

    return { locale, t }
  }
}
</script>

<i18n>
{
  "en": {
    "language": "Language",
    "hello": "hello, world!"
  },
  "ja": {
    "language": "言čĒž",
    "hello": "こんãĢãĄã¯ã€ä¸–į•Œīŧ"
  }
}
</i18n>

Locale Messages formatting

You can be used by specifying the following format in the lang attribute:

  • json (default)
  • yaml
  • yml
  • json5

example yaml format:

<i18n lang="yaml">
en:
  hello: 'Hello World!'
ja:
  hello: 'こんãĢãĄã¯ã€ä¸–į•Œīŧ'
</i18n>

Static bundle importing

vite-plugin-vue-i18n allows you to statically bundle i18n resources such as json or yaml specified by the include option of the plugin described below as locale messages with the import syntax.

In this case, only one i18n resource can be statically bundled at a time with import syntax, so the these code will be redundant for multiple locales.

import { createApp } from 'vue'
import { createI18n } from 'vue-i18n'
/*
 * The i18n resources in the path specified in the plugin `include` option can be read
 * as vue-i18n optimized locale messages using the import syntax
 */
import en from './src/locales/en.json'
import ja from './src/locales/ja.yaml'
import fr from './src/locales/fr.json5'

const i18n = createI18n({
  locale: 'en',
  messages: {
    en,
    ja,
    fr
  }
})

const app = createApp()
app.use(i18n).mount('#app)

vite-plugin-vue-i18n can use the vite (rollup) mechanism to import all locales at once, using the special identifier @intlify/vite-plugin-vue-i18n/messages, as the bellow:

import { createApp } from 'vue'
import { createI18n } from 'vue-i18n'
/*
 * All i18n resources specified in the plugin `include` option can be loaded
 * at once using the import syntax
 */
import messages from '@intlify/vite-plugin-vue-i18n/messages'

const i18n = createI18n({
  locale: 'en',
  messages
})

const app = createApp()
app.use(i18n).mount('#app)

Client Types

If you want type definition of @intlify/vite-plugin-vue-i18n/messages, add vite-plugin-vue-i18n/client to compilerOptions.types of your tsconfig:

{
  "compilerOptions": {
    "types": ["@intlify/vite-plugin-vue-i18n/client"]
  }
}

Bundle optimizations

vite-plugin-vue-i18n allows you to support bundle size optimization provided by vue-i18n.

đŸ“Ļ Automatic Vue I18n bundling

As noted here, NPM provides many different builds of Vue I18n.

vite-plugin-vue-i18n will automatically select and bundle Vue I18n build according to the following vite behavior:

  • vite dev: vue-i18n.esm-bundler.js
  • vite build: vue-i18n.runtime.esm-bundler.js

About details, See the here

🔧 Options

You can specify options in the plugin option to support bundle size optimization provided by vue-i18n.

The same thing can be configured with the define option, but the plugin option is more friendly. Especially if you are using typescript, you can use intelisense.

About details, See the below section

include

  • Type: string | string[] | undefined

  • Default: undefined

    A minimatch pattern, or array of patterns, you can specify a path to pre-compile i18n resources files. The extensions of i18n resources to be precompiled are as follows:

    - json
    - json5
    - yaml
    - yml
    

    Note json resources matches this option, it will be handled before the internal json plugin of Vite, and will not be processed afterwards, else the option doesn't match, the Vite side will handle.

runtimeOnly

  • Type: boolean

  • Default: true

    Whether or not to automatically use Vue I18n runtime-only in production build, set in the vue-i18n field of Vite resolve.alias option. If false is specified, Vue I18n (vue-i18n) package.json module field will be used.

    For more details, See here

compositionOnly

  • Type: boolean

  • Default: true

    Whether to make vue-i18n's API only composition API. By default the legacy API is tree-shaken.

    For more details, See here

fullInstall

  • Type: boolean

  • Default: true

    Whether to install the full set of APIs, components, etc. provided by Vue I18n. By default, all of them will be installed.

    If false is specified, buld-in components and directive will not be installed in vue and will be tree-shaken.

    For more details, See here

forceStringify

  • Type: boolean

  • Default: false

    Whether pre-compile number and boolean values as message functions that return the string value.

    For example, the following json resources:

    {
      "trueValue": true,
      "falseValue": false,
      "nullValue": null,
      "numberValue": 1
    }

    after pre-compiled (development):

    export default {
      trueValue: (() => {
        const fn = ctx => {
          const { normalize: _normalize } = ctx
          return _normalize(['true'])
        }
        fn.source = 'true'
        return fn
      })(),
      falseValue: (() => {
        const fn = ctx => {
          const { normalize: _normalize } = ctx
          return _normalize(['false'])
        }
        fn.source = 'false'
        return fn
      })(),
      nullValue: (() => {
        const fn = ctx => {
          const { normalize: _normalize } = ctx
          return _normalize(['null'])
        }
        fn.source = 'null'
        return fn
      })(),
      numberValue: (() => {
        const fn = ctx => {
          const { normalize: _normalize } = ctx
          return _normalize(['1'])
        }
        fn.source = '1'
        return fn
      })()
    }

defaultSFCLang

  • Type: string

  • Default: undefined

    Specify the content for all your inlined i18n custom blocks on your SFC.

    defaultSFCLang must have one of the following values:

    - json
    - json5
    - yaml
    - yml
    

    On inlined i18n custom blocks that have specified the lang attribute, the defaultSFCLang is not applied.

    For example, with defaultSFCLang: "yaml" or defaultSFCLang: "yml", this custom block:

    <i18n lang="yaml">
    en:
      hello: Hello
    es:
      hello: Hola
    </i18n>

    and this another one, are equivalent:

    <i18n>
    en:
      hello: Hello
    es:
      hello: Hola
    </i18n>

globalSFCScope

  • Type: boolean

  • Default: undefined

    Whether to include all i18n custom blocks on your SFC on global scope.

    If true, it will be applied to all inlined i18n or imported custom blocks.

    Warning: beware enabling globalSFCScope: true, all i18n custom blocks in all your SFC will be on global scope.

    For example, with globalSFCScope: true, this custom block:

    <i18n lang="yaml" global>
    en:
      hello: Hello
    es:
      hello: Hola
    </i18n>

    and this another one, are equivalent:

    <i18n lang="yaml">
    en:
      hello: Hello
    es:
      hello: Hola
    </i18n>

    You can also use defaultSFCLang: "yaml", following with previous example, this another is also equivalent to previous ones:

    <i18n>
    en:
      hello: Hello
    es:
      hello: Hola
    </i18n>

📜 Changelog

Details changes for each release are documented in the CHANGELOG.md.

❗ Issues

Please make sure to read the Issue Reporting Checklist before opening an issue. Issues not conforming to the guidelines may be closed immediately.

Šī¸ License

MIT

More Repositories

1

vue-i18n-next

Vue I18n for Vue 3
TypeScript
1,904
star
2

vue-i18n-loader

🌐 vue-i18n loader for custom blocks
TypeScript
268
star
3

bundle-tools

bundling for intlify i18n tools
TypeScript
226
star
4

nuxt3

Nuxt 3 Module for vue-i18n-next
TypeScript
202
star
5

vue-cli-plugin-i18n

🌐 Vue CLI plugin to add vue-i18n to your Vue Project
JavaScript
197
star
6

eslint-plugin-vue-i18n

🌐 ESLint plugin for Vue I18n
TypeScript
120
star
7

vue-i18n-extensions

🌐 vue-i18n extensions
TypeScript
89
star
8

vue-i18n-locale-message

🌐 i18n locale messages management tool for vue-i18n
TypeScript
72
star
9

vue-i18n-composable

Composition API for vue-i18n in Vue 2.x
JavaScript
44
star
10

h3

Internationalization middleware & utilities for h3
TypeScript
25
star
11

vite-vue-i18n-starter

⚡ Vite Vue I18n Starter
Vue
24
star
12

routing

The i18n routing libraries
TypeScript
16
star
13

hono

Internationalization middleware & utilities for Hono
TypeScript
14
star
14

utils

Collection of i18n utilities
TypeScript
12
star
15

devtools

⚙ī¸ i18n devtools for debugging Intlify applications
TypeScript
11
star
16

cli

CLI Tooling for i18n development
TypeScript
10
star
17

bridging

Utilities that provide universal compatibility for Vue 2 & Vue 3
JavaScript
10
star
18

rollup-plugin-vue-i18n

🌐 vue-i18n rollup plugin for custom blocks
JavaScript
9
star
19

vue-i18n-jest

🌐 vue-jest wrapper for i18n custom blocks
JavaScript
7
star
20

intlify.dev

🌐 Website for Intlify
Vue
6
star
21

eslint-plugin-svelte

ESLint plugin for internationalization with Svelte
TypeScript
4
star
22

poeditor-service-provider

POEditor service provider for vue-i18n-locale-message
TypeScript
4
star
23

intlify-core

3
star
24

express

intlify for express middleware
TypeScript
2
star
25

blog

📔 The official Intlify blog
Vue
2
star
26

art

🎨 Artworks
1
star
27

.github

1
star