Giter VIP home page Giter VIP logo

vite-plugin-vue3-auto-i18n's Introduction

READ THIS FIRST!

This plugin is still in beta. Expect bugs!

vite-plugin-vue3-auto-i18n

NPM version

Introdction

Auto import Vue-i18n in every vue sfc files and auto replace i18n words in vue sfc files.

  • replace matched reactive string variable ref('xxx') with ref(t('xxx')) in both setup script and setup function.✅
  • replace matched literal string 'xxx' with computed(()=>t('xxx')) in both setup script and setup function. ✅
  • replace matched plain node value xxx with $t('xxx') in template. ❌

Installation

npm install -D vite-plugin-vue3-auto-i18n

First, make sure you have added vue-i18n in your project. For example:

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

const messages = {
  en: {
    message: {
      hello: 'hello world',
      Hi: 'hi'
    },
  },
  ch: {
    message: {
      hello: '你好,世界',
      Hi: '嗨'
    },
  },
}

const i18n = createI18n({
  locale: 'ch', // set locale
  fallbackLocale: 'en', // set fallback locale
  messages, // set locale messages
})

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

Then, add this plugin to your vite.config.js.

Load order: This plugin should come after vue plugin, if you use vite-plugin-vue-i18n, then this plugin should also come after it

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueI18n from '@intlify/vite-plugin-vue-i18n' // if any
import autoI18n from 'vite-plugin-vue3-autoI18n'

export default defineConfig({
  plugins: [vue(), vueI18n(), autoI18n()],
})

Usage

write your code as usual, like:

<script setup lang="ts">
import { ref } from 'vue'
// ...
</script>

<template>
  <div>hello world</div>
</template>

Finally,this plugin will matches all the strings in the i18n configuration and replaces them automatically. For example,The above code will be converted to:

<script setup lang="ts">
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
const { t } = useI18n()

// ...
</script>

<template>
  <div>{{ $t('message.hello') }}</div>
</template>

License

MIT License © 2022 love-JS

vite-plugin-vue3-auto-i18n's People

Contributors

exwer avatar

Stargazers

RobbChan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.