vue-localer
TypeScript icon, indicating that this package has built-in type declarations

1.5.3 • Public • Published

vue-localer

Internationalization plugin for Vue.

Install

$ npm i vue-localer
# or
$ yarn add vue-localer
# or
$ pnpm i vue-localer
# or
$ bun add vue-localer

Usage

First, prepare the multilingual files.

// src/locales/en-US.ts
export default {
  hello: `Hello, {msg}!`,
  world: `Hello, World!`,
};

// src/locales/ja-JP.ts
export default {
  hello: `こんにちは、{msg}!`,
  world: `こんにちは、世界!`,
};

// src/locales/ko-KR.ts
export default {
  hello: `안녕하세요, {msg}!`,
  world: `안녕하세요, 월드!`,
};

Instantiate vue-localer and load multiple language files.

// src/plugins/localer.ts
import { createLocaler } from 'vue-localer';

import enUS from '~/locales/en-US';

export default createLocaler({
  fallbackLocale: 'en-US',
  messages: {
    'en-US': enUS,
    'ja-JP': () => import('~/locales/ja-JP'),
    'ko-KR': () => import('~/locales/ko-KR'),
  },
});

Register the instantiated vue-localer as app-level functionality to Vue.

// src/main.ts
import { createApp } from 'vue';

import localer from '~/plugins/localer';

import App from './App.vue';

const app = createApp(App);

app.use(localer);

app.mount('#root');

Next, by using useLocale, you can obtain the source of the current locale.

<script lang="ts" setup>
import { useLocaler, useLocale } from 'vue-localer';

const { f } = useLocaler();
const locale = useLocale();
</script>

<template>
  <div>{{ f(locale.hello, { msg: 'Vue' }) }}</div>
  <div>{{ locale.world }}</div>
</template>

Documentation

To learn more about vue-localer, check its documentation.

Package Sidebar

Install

npm i vue-localer

Weekly Downloads

9

Version

1.5.3

License

MIT

Unpacked Size

10.9 kB

Total Files

8

Last publish

Collaborators

  • shyam-chen