vue-banana-i18n

A Banana-i18n wrapper to support localization in Vue.js
8,133 9
Install
npm install vue-banana-i18n
Latest Version:2.3.0
Vue:3
Older releases:2
License:MIT
Last Updated:May 30, 2023
Links: GitHub  ·  npm
Maintainer: santhosh.thottingal

vue-banana-i18n

GitHub license npm version Build

A banana-i18n wrapper to support localization in Vue.js

Playground

For Vue 2 use version 1.x. Version 2.x+ only supports Vue 3.x+

Installation

npm install vue-banana-i18n

then

import i18n from 'vue-banana-i18n'

Basic Usage

<div id="app">
  <h1>{{ $i18n('hello_world') }}</h1>
  <h2 class='result'>{{ $i18n('search_results', 10) }}</h2>
  <div class='status'>{{ $i18n('profile_change_message', 'Alice', 'female') }}</div>
</div>

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

const messages = {
  en: {
    'hello_world': 'Hello world',
    'search_results': 'Found $1 {{PLURAL:$1|result|results}}',
    'profile_change_message': '$1 changed {{GENDER:$2|his|her}} profile picture'
  },
  ml: {
    'hello_world': 'എല്ലാവർക്കും നമസ്കാരം',
    'search_results': '{{PLURAL:$1|$1 ഫലം|$1 ഫലങ്ങൾ|1=ഒരു ഫലം}} കണ്ടെത്തി',
    'profile_change_message': '$1 {{GENDER:$2|അവന്റെ|അവളുടെ}} പ്രൊഫൈൽ പടം മാറ്റി'
  }
}

const app = createApp(App);
const i18nPlugin = createI18n({
  locale: "en",
  messages: messages
});

app.use(i18nPlugin);
app.mount("#app");

Directive

The v-i18n directive as illustrated in below example is also useful. It sets the text for the node. HTML values if any, will be escaped.

<div id="app">
  <h1 v-i18n="'hello_world'"></h1>
  <h2 class='result' v-i18n="{msg: 'search_results', params:[10]}"></h2>
  <div class='status' v-i18n="{msg: 'profile_change_message', params:['Alice', 'female']}"></div>
</div>

Alternative syntax:

<div id="app">
  <h1 v-i18n="'hello_world'"></h1>
  <h2 class='result' v-i18n:search_results="[10]"></h2>
  <div class='status' v-i18n:profile_change_message="['Alice', 'female']"></div>
</div>

To set html of the node, use v-i18n.html or v-i18n-html directive just like above.

Example:

  <h2 class='result' v-i18n:search_results.html="[10]"></h2>

Message format

The Banana i18n system and its messages are documented at banana-i18n

License

MIT

Related Packages

voo-i18n

A small package for implementing translations in Vue.js

5,999 41
vue-i18n

Internationalization plugin for Vue.js

115,437,066 2,712
@panter/vue-i18next

i18next wrapper for vue

377,878 172
vue-i18n-edge

Internationalization plugin for Vue.js

3,888 2,712

Version History

Version Released Vue License
2.3.0 ^3.3.4 MIT
2.2.0 ^3.2.28 MIT
2.1.2 ^3.2.28 MIT
2.0.2 ^3.2.28 MIT
2.0.1 ^3.2.28 MIT