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

i18next-vue

v5.0.0

Published

i18next integration for Vue

Downloads

131,519

Readme

i18next-vue

Introduction

This library is a simple wrapper for i18next, simplifying its use in Vue 3.

There is also a Vue 2 version of this package.

Upgrade

In the documentation, you can find information on how to upgrade from @panter/vue-i18next, from i18next-vue 3.x or earlier versions.

Installation

npm install i18next-vue

Initialisation

import Vue from "vue";
import i18next from "i18next";
import I18NextVue from "i18next-vue";
import App from "./App.vue";

/*const i18nInitialized = */i18next.init({ ... });
createApp(App).use(I18NextVue, { i18next }).mount('#app');

// to wait for loading the translations first, do this instead:
// i18nInitialized.then(() => createApp(App).use(I18NextVue, { i18next }).mount('#app'));

Usage

Use the $t translation function, which works like (and uses) the versatile t function from i18next.

There is a full tutorial for setting up i18next-vue. You can check out the live demo version version of it, too.

To learn about more options, check out the full documentation.

Simple example

Given the i18next translations

i18next.init({
	// ...
	resources: {
		en: {
			// language
			translation: {
				// the default namespace
				insurance: "Insurance",
			},
		},
		de: {
			// language
			translation: {
				// the default namespace
				insurance: "Versicherung",
			},
		},
	},
});

You can use

<template>
	<h1>A test in {{ $i18next.language }}</h1>
	<p>{{ $t("insurance") }}</p>
</template>

$t() works both in Options API and Composition API components.

Using the useTranslation() composition function you can access the i18next instance and t() in the setup part, and e.g. get a t() functions for a specific namespace.

<script setup>
import { computed } from "vue";
import { useTranslation } from "i18next-vue";
const { i18next, t } = useTranslation();
const term = computed(() => t("insurance"));
</script>

<template>
	<h1>A test in {{ i18next.language }}</h1>
	<p>inline: {{ t("insurance") }}</p>
	<p>inline with $t: {{ $t("insurance") }}</p>
	<p>computed: {{ term }}</p>
</template>

Translation component

i18next-vue provides a <i18next> translation component, so you can use markup (including Vue components) in translations.

In this example {faq-link} will be replaced by the faq-link slot, i.e. by the router link. You can move {faq-link} around in the translation, so it makes sense for the target language.

i18next.init({
  // ...
  resources: {
    en: {
      translation: {
        "message": "Open the {faq-link} page."
        "faq": "Frequently Asked Questions"
      }
    },
    fr: {
      // ...
    }
  }
})
<template>
	<i18next :translation="$t('message')">
		<template #faq-link>
			<router-link :to="FAQ_ROUTE">
				{{ $t("faq") }}
			</router-link>
		</template>
	</i18next>
</template>

Custom slot values

Custom slot values may be useful when the default braces ({ and }) are wrongly treated by the Locize service or don't satisfy other needs.

Use custom values for recognizing start and end of the insertion points of the <i18next>/TranslationComponent inside the localization term:

// main.js
i18next.init({
  // ...
  resources: {
    en: {
      translation: {
        "message": "Open the <slot>faq-link</slot> page."
        "faq": "FAQ"
      }
    },
    de: {
      // ...
    }
  }
})

app.use(I18NextVue, {
    i18next,
    slotStart: '<slot>',
    slotEnd: '</slot>',
});
<!-- Component.vue -->
<template>
	<i18next :translation="$t('message')">
		<template #faq-link>
			<router-link :to="FAQ_ROUTE">
				{{ $t("faq") }}
			</router-link>
		</template>
	</i18next>
</template>

Contributing

Requirements

  • Node.js >= v20