Skip to content
On this page

๐ŸŒˆ Getting started โ€‹

To begin, you'll need to install vue-next-i18n

use npm โ€‹

npm install vue-next-i18n

use yarn โ€‹

yarn add vue-next-i18n

๐Ÿš€ Usage โ€‹

When using with a module system, you must explicitly install the vue-next-i18n via app.use():

typescript
// 1. Ready translated locale messages
// The structure of the locale message is the hierarchical object structure with each locale as the top property
const messages = {
  en: {
    message: {
      hello: 'hello world'
    }
  },
  zhCHS: {
    message: {
      hello: 'ไฝ ๅฅฝ ไธ–็•Œ'
    }
  },
  ja: {
    message: {
      hello: 'ใ“ใ‚“ใซใกใฏใ€ไธ–็•Œ'
    }
  }
}

// 2. Create i18n instance with options
import { createApp } from 'vue';
import { createI18n } from 'vue-next-i18n';

const i18n = createI18n({
  locale: 'zhCHS', // set locale, depend on messages object structure keys
  messages, // set locale messages
  localeKeys:['zhCHS','zhCHT','en'] //Not required, default value๏ผš ['zhCHS','zhCHT','en']
})


// 3. Create a vue root instance
const app = createApp({
  // set something options
  // ...
})

// 4. Install i18n instance to make the whole app i18n-aware
app.use(i18n)

// 5. Mount
app.mount('#app')

// Now the app has started!

HTML โ€‹

<div id="app">
  <p>{{ $t("message.hello") }}</p>
</div>
<!-- Output the following: -->
<div id="#app">
  <p>ไฝ ๅฅฝ ไธ–็•Œ</p>
</div>

๐ŸšŒ Composition API โ€‹

typescript
import { useI18n } from 'vue-next-i18n'

export default {
  setup() {
    const i18n = useI18n()
    const { currentLocale, changeLocale } = i18n
    return {
      currentLocale,
      changeLocale
    }
  }
}

๐Ÿ“ฆ API Examples โ€‹

basic โ€‹

const messages = {
  en: {
    message: {
      hello: 'hello world'
    }
  },
  zhCHS: {
    message: {
      hello: 'ไฝ ๅฅฝ ไธ–็•Œ'
    }
  },
  ja: {
    message: {
      hello: 'ใ“ใ‚“ใซใกใฏใ€ไธ–็•Œ'
    }
  }
}

<div id="app">
  <p>{{ $t("message.hello") }}</p>
</div>

support function โ€‹

const messages = {
  en: {
    message: {
      hello: (val) =>  `hello world ${val}`
    }
  },
  zhCHS: {
    message: {
      hello: (val) =>  `ไฝ ๅฅฝ ไธ–็•Œ ${val}`
    }
  },
  ja: {
    message: {
      hello: (val) =>  `ใ“ใ‚“ใซใกใฏใ€ไธ–็•Œ ${val}`
    }
  }
}

<div id="app">
  <p>{{ $t("message.hello",'hahaha') }}</p>
</div>

support $n replacement โ€‹

Inserts the n th (1-indexed) available

const messages = {
  en: {
    message: {
      hello: `hello world $1,$2,$3...`
    }
  },
  zhCHS: {
    message: {
      hello: `ไฝ ๅฅฝ ไธ–็•Œ $1,$2,$3...`
    }
  },
  ja: {
    message: {
      hello:`ใ“ใ‚“ใซใกใฏใ€ไธ–็•Œ $1,$2,$3...`
    }
  }
}

// output: hello world param1,param2,param3
<div id="app">
  <p>{{ $t("message.hello",'param1','param2','param3') }}</p>
</div>

use array messages โ€‹

The array order depends on the localeKeys, default value is ['zhCHS','zhCHT','en']

// example localeKeys: ['zhCHS','en','ja']

<div id="app">
  <p>{{ $t(['ไฝ ๅฅฝ ไธ–็•Œ','hello world','ใ“ใ‚“ใซใกใฏใ€ไธ–็•Œ']) }}</p>
</div>

use i18n option in component โ€‹

export default {
  i18n:{
    en: {
      message: {
        hello: 'hello world'
      }
    },
    zhCHS: {
      message: {
        hello: 'ไฝ ๅฅฝ ไธ–็•Œ'
      }
    },
    ja: {
      message: {
        hello: 'ใ“ใ‚“ใซใกใฏใ€ไธ–็•Œ'
      }
    } 
  },
  setup(){
    // code...
  }
}

<div id="app">
  <p>{{ $t('message.hello') }}</p>
</div>