All Projects → intlify → Vue I18n Loader

intlify / Vue I18n Loader

Licence: mit
🌐 vue-i18n loader for custom blocks

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Vue I18n Loader

Vue Admin Template
Sample Admin Template based on Vuejs & Vuetify.
Stars: ✭ 177 (-22.71%)
Mutual labels:  webpack, i18n
Vue Admin
基于and-design-vue的vue后台管理系统模板
Stars: ✭ 226 (-1.31%)
Mutual labels:  webpack, i18n
Purs Loader
PureScript loader for webpack
Stars: ✭ 182 (-20.52%)
Mutual labels:  webpack, loader
Webpack Config Handbook
Minimum Webpack config handbook & examples
Stars: ✭ 165 (-27.95%)
Mutual labels:  webpack, loader
Awesome Typescript Loader
Awesome TypeScript loader for webpack
Stars: ✭ 2,357 (+929.26%)
Mutual labels:  webpack, loader
Pug As Jsx Loader
Stars: ✭ 168 (-26.64%)
Mutual labels:  webpack, loader
React Redux Webpack Starter
Learning react
Stars: ✭ 189 (-17.47%)
Mutual labels:  webpack, loader
Vue Webpack Config
Koa2、Webpack、Vue、React、Node
Stars: ✭ 151 (-34.06%)
Mutual labels:  webpack, loader
Vue Blog
🎉 基于vue全家桶 + element-ui 构建的一个后台管理集成解决方案
Stars: ✭ 208 (-9.17%)
Mutual labels:  webpack, i18n
Heyui
🎉UI Toolkit for Web, Vue2.0 http://www.heyui.top
Stars: ✭ 2,373 (+936.24%)
Mutual labels:  webpack, i18n
Ts Tools
TypeScript Tools for Node.js
Stars: ✭ 162 (-29.26%)
Mutual labels:  webpack, loader
Style Resources Loader
CSS processor resources loader for webpack
Stars: ✭ 214 (-6.55%)
Mutual labels:  webpack, loader
Img Loader
Image minimizing loader for webpack
Stars: ✭ 161 (-29.69%)
Mutual labels:  webpack, loader
React Hot Loader Loader
A Webpack Loader that automatically inserts react-hot-loader to your App 👨‍🔬
Stars: ✭ 176 (-23.14%)
Mutual labels:  webpack, loader
Webpack Fast Refresh
React Fast Refresh plugin and loader for webpack
Stars: ✭ 155 (-32.31%)
Mutual labels:  webpack, loader
Webpack2 Lessons
📖《webpack2 包教不包会》
Stars: ✭ 187 (-18.34%)
Mutual labels:  webpack, loader
Omil
📝Webpack loader for Omi.js React.js and Rax.js components 基于 Omi.js,React.js 和 Rax.js 单文件组件的webpack模块加载器
Stars: ✭ 140 (-38.86%)
Mutual labels:  webpack, loader
Workflow
一个工作流平台
Stars: ✭ 1,888 (+724.45%)
Mutual labels:  webpack, loader
Vuesion
Vuesion is a boilerplate that helps product teams build faster than ever with fewer headaches and modern best practices across engineering & design.
Stars: ✭ 2,510 (+996.07%)
Mutual labels:  webpack, i18n
V Selectpage
SelectPage for Vue2, list or table view of pagination, use tags for multiple selection, i18n and server side resources supports
Stars: ✭ 211 (-7.86%)
Mutual labels:  webpack, i18n

Vue I18n Loader logo

@intlify/vue-i18n-loader

Build Status npm npm

vue-i18n loader for custom blocks

NOTE: ⚠️ This next branch is development branch for Vue 3! The version for Vue 2 is now in master branch!


⭐️ Features

  • i18n resource pre-compilation
  • i18n custom block
    • i18n resource definition
    • i18n resource importing
    • Locale of i18n resource definition
    • Locale of i18n resource definition for global scope
    • i18n resource formatting

💿 Installation

NPM

$ npm i --save-dev @intlify/[email protected]

YARN

$ yarn add -D @intlify/[email protected]

NOTE: ⚠️ @intlify/[email protected] is for Vue I18n v9 or later, if you want to use Vue I18n v8.x, you need to install @intlify/vue-i18n-loader.

🚀 i18n resource pre-compilation

Why do we need to require the configuration?

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.

You can pre-commpile by configuring vue-i18n-loader as the webpack loader.

webpack configration

As an example, if your project has the locale messagess in src/locales, your webpack config will look like this:

├── dist
├── index.html
├── package.json
├── src
│   ├── App.vue
│   ├── locales
│   │   ├── en.json
│   │   └── ja.json
│   └── main.js
└── webpack.config.js
import { createApp } from 'vue'
import { createI18n } from 'vue-i18n' // import from runtime only
import App from './App.vue'

// import i18n resources
import en from './locale/en.json'
import ja from './locale/ja.json'

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

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

In the case of the above project, you can use vue-i18n with webpack configuration to the following for runtime only:

module.exports = {
  module: {
    rules: [
      // ...
      {
        test: /\.(json5?|ya?ml)$/, // target json, json5, yaml and yml files
        type: 'javascript/auto',
        loader: '@intlify/vue-i18n-loader',
        include: [ // Use `Rule.include` to specify the files of locale messages to be pre-compiled
          path.resolve(__dirname, 'src/locales')
        ]
      },
      // ...
    ]
  }
}

The above uses webpack's Rule.include to specify the i18n resources to be precompiled. You can also use Rule.exclude to set the target.

🚀 i18n custom block

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

i18n resource definition

<template>
  <p>{{ t('hello') }}</p>
</template>

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

export default {
  name: 'app',
  setup() {
    const { t, locale } = useI18n({
      // ...
    })

    // Somthing todo ...

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

<i18n>
{
  "en": {
    "hello": "hello world!"
  },
  "ja": {
    "hello": "こんにちは、世界!"
  }
}
</i18n>

The locale messages defined at i18n custom blocks are json format default.

i18n resource importing

You also can use src attribute:

<i18n src="./myLang.json"></i18n>
// ./myLang.json
{
  "en": {
    "hello": "hello world!"
  },
  "ja": {
    "hello": "こんにちは、世界!"
  }
}

Locale of i18n resource definition

You can define locale messages for each locale with locale attribute in single-file components:

<i18n locale="en">
{
  "hello": "hello world!"
}
</i18n>

<i18n locale="ja">
{
  "hello": "こんにちは、世界!"
}
</i18n>

The above defines two locales, which are merged at target single-file components.

Locale of i18n resource definition for global scope

You can define locale messages for global scope with global attribute:

<i18n global>
{
  "en": {
    "hello": "hello world!"
  }
}
</i18n>

i18n resource formatting

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

  • yaml
  • json5

example yaml foramt:

<i18n locale="en" lang="yaml">
  hello: "hello world!"
</i18n>

<i18n locale="ja" lang="yml">
  hello: "こんにちは、世界!"
</i18n>

example json5 format:

<i18n lang="json5">
{
  "en": {
    // comments
    "hello": "hello world!"
  }
}
</i18n>

JavaScript

import { createApp } from 'vue'
import { createI18n } from 'vue-i18n'
import App from './App.vue'

// setup i18n instance with globaly
const i18n = createI18n({
  locale: 'ja',
  messages: {
    en: {
      // ...
    },
    ja: {
      // ...
    }
  }
})

const app = createApp(App)

app.use(i18n)
app.mount('#app')

webpack Config

vue-loader (next version):

module.exports = {
  module: {
    rules: [
      // ...
      {
        resourceQuery: /blockType=i18n/,
        type: 'javascript/auto',
        loader: '@intlify/vue-i18n-loader'
      },
      // ...
    ]
  }
}

🔧 Options

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;})()
    }
    

    webpack configration:

    module.exports = {
      module: {
        rules: [
          // ...
          {
            test: /\.(json5?|ya?ml)$/,
            type: 'javascript/auto',
            include: [path.resolve(__dirname, './src/locales')],
            use: [
              {
                loader: '@intlify/vue-i18n-loader',
                options: {
                  forceStringify: true
                }
              }
            ]
          },
          // ...
        ]
      }
    }
    

📜 Changelog

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

💪 Contribution

Please make sure to read the Contributing Guide before making a pull request.

©️ License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].