Vue 3 Gettext

4.0.0-beta.1 · active · verified Sun Apr 19

Vue 3 Gettext is a library for internationalizing Vue 3 applications using the gettext translation system. It provides a simple, ergonomic API for reactive translations within Vue templates and TypeScript/JavaScript code, supporting pluralization and message contexts. The package includes a CLI tool for automatic message extraction from code files, facilitating the translation workflow. Currently in a 4.0.0-beta.1 state, it is actively developed with continuous enhancements and breaking changes between major and minor alpha/beta releases. Its key differentiator is the direct integration with Vue 3's reactivity system and adherence to the established gettext standard, offering a robust alternative to other i18n solutions for projects already using or preferring gettext.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize vue3-gettext, use $gettext for singular and plural translations in a template, and dynamically change the locale using `useGettext` in the setup function.

import { createApp } from 'vue';
import { createGettext } from 'vue3-gettext';

const app = createApp({
  template: `
    <div>
      <p>{{ $gettext("Welcome, %{name}!") }}</p>
      <p>{{ $gettext("You have %{count} new message.", "You have %{count} new messages.", 1, { count: 1 }) }}</p>
      <p>{{ $gettext("Current locale: %{locale}", { locale: currentLocale }) }}</p>
      <button @click="changeLocale('en')">English</button>
      <button @click="changeLocale('es')">Español</button>
    </div>
  `,
  setup() {
    const { current } = useGettext();
    const currentLocale = ref(current.value);

    const changeLocale = (newLocale) => {
      current.value = newLocale;
      currentLocale.value = newLocale;
    };
    return { currentLocale, changeLocale };
  }
});

const gettext = createGettext({
  defaultLocale: 'en',
  availableLocales: {
    en: 'English',
    es: 'Español'
  },
  translations: {
    es: {
      'Welcome, %{name}!': '¡Bienvenido, %{name}!',
      'You have %{count} new message.': [
        'Tienes %{count} nuevo mensaje.',
        'Tienes %{count} nuevos mensajes.'
      ],
      'Current locale: %{locale}': 'Idioma actual: %{locale}'
    }
  }
});

app.use(gettext);
app.mount('#app');

view raw JSON →