Vue 3 Gettext
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
-
Module not found: Can't resolve 'vue3-gettext' in '...' or 'require is not defined'
cause Attempting to use CommonJS `require()` syntax or an outdated module resolver with `vue3-gettext` v4+, which is ESM-only.fixChange `require('vue3-gettext')` to `import { ... } from 'vue3-gettext'`. Ensure your project uses an ESM-compatible build setup (e.g., `"type": "module"` in `package.json`). -
Property '$gettext' does not exist on type 'ComponentPublicInstance<...>' (TypeScript error)
cause TypeScript is not correctly augmenting the Vue instance types, or the `vue3-gettext` plugin is not properly installed or configured.fixVerify that `app.use(gettext)` is called after `createApp()`. Ensure your `tsconfig.json` is set up to include the type declarations from `vue3-gettext` (e.g., check `include` array and ensure `node_modules` is not excluded). -
[Vue warn]: Failed to resolve component: translate
cause Using the `<translate>` component or `v-translate` directive which were removed in v4.fixRemove the `<translate>` component and `v-translate` directive. Use the `$gettext` helper function directly in templates: `{{ $gettext("Your message") }}`.
Warnings
- breaking CommonJS outputs have been removed. Applications must use ESM imports for `vue3-gettext`.
- breaking The minimum Node.js version required has been increased to 20.
- breaking The `<translate>` component and `v-translate` directive have been removed.
- breaking Message IDs are no longer trimmed and line endings are normalized from CRLF to LF. This may affect existing translation files if they relied on automatic trimming or varied line endings.
- breaking The configuration interface `GettextConfigOptions` was renamed to `Config`.
- gotcha Global types for Vue instances (`this.$gettext`) are augmented in both `@vue/runtime-core` and `vue` to improve compatibility across different project setups. If you encounter TypeScript errors regarding `$gettext` not existing, check your `tsconfig.json` references.
Install
-
npm install vue3-gettext -
yarn add vue3-gettext -
pnpm add vue3-gettext
Imports
- createGettext
const { createGettext } = require('vue3-gettext')import { createGettext } from 'vue3-gettext' - useGettext
import useGettext from 'vue3-gettext'
import { useGettext } from 'vue3-gettext' - $gettext
import { $gettext } from 'vue3-gettext'This is typically accessed via `this.$gettext` in options API components or directly in templates.
Quickstart
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');