npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-i18n-mini

v1.2.3

Published

Minimalistic, lightweight, easy-to-use i18n solution for Vue 3

Downloads

7

Readme

Vue i18n mini

This package provides a minimalistic (but complete), very lightweight, easy-to-use i18n solution for Vue 3.

For companion library, see Express i18n mini

Features

  • Lightweight
  • Uses lodash template for string interpolation
  • ...

Installation

npm i vue-i18n-mini

Usage

Usually you may want to use internationalization according to the following scenarios:

  1. A language prefix in URL defines the current language. This scenario is preferred, primarily, for a website where SEO is desired. No prefix means either the default language or we have to redirect to a URL with a prefix.

  2. We do not use language prefixes in URLs here, instead the current language is always defined based on browser's setting: the browser gives ordered list of preferred languages, so we use it to find the best suited language. Additionally to this, we give to an end user ability to choose the different language from our list, and we store this user preference, say, in cookie or in localStorage.

  3. The current language may initially be defined based on the browser's setting (exactly as in the previous scenario), but actually we rely on a profile option of a logged in user. It is usually stored on the server (although it can be cached on the browser side, of course).

  4. Some combinations of the above are possible: for example, the part of our site uses the first scheme (language prefix in URL), and the other part uses the second scheme (user private zone, where no URL prefix is required).

Vue i18n mini tries to cover all these use-cases. Let's take a look at how to use it in practice.

Configure

import {createI18n} from 'vue-i18n-mini'

export const i18n = createI18n({
  defaultLang: 'en',
  fallbackLang: 'en',
  langData: {
    en: {
      hello: 'Hello {name}!'
    },
    de: {
      hello: 'Hallo {name}!'
    }
  }
})

The defaultLang and langData are the only required options.

The defaultLang will be used if no better languages available to meet user's preference.

The fallbackLang is a language to use if translations in the current language are not available. If not set or null, such fallback does not occur.

langData contains all the available languages and messages. Actually, it's recommended to always lazy-load translations instead of including them directly. In this case, they will be loaded asynchronously only when they are needed:

import {createI18n} from 'vue-i18n-mini'

export const i18n = createI18n({
  defaultLang: 'en',
  fallbackLang: 'en',
  langData: {
    en: () => import('./locales/en'),
    de: () => import('./locales/de'),
  }
})

Now, plug it in:

import {createApp} from 'vue'
import {i18n} from './i18n'

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

Now, if you want to use language prefixes in URLs, use Vue router:

import {createRouter} from 'vue-router'

const router = createRouter({
  // options...
})

i18n.useRouter(router)

If you do not use router, you need to initialize the library somewhere. This will read user preferred (saved) language, or will try to find the best language based on the browser's preference. As simple as that:

i18n.init()

Alternatively, inside a component:

export default {
  methods: {
    someMethod() {
      this.$i18n.init()
    }
  }
}

Use

<template>
  <div>{{$t('hello', {name: 'World'})}}</div>
</template>

License

This package is licensed under the MIT license.