Petite Vue I18n
petite-vue-i18n is a specialized, lightweight internationalization library for Vue 3 applications, offering a trimmed-down feature set compared to the full vue-i18n package. Currently at version 11.3.2, it maintains a regular release cadence with frequent bug fixes and minor improvements, often in sync with the main vue-i18n project. Its primary differentiation lies in its significantly smaller bundle size (up to 45% smaller for runtime-only builds) by exclusively supporting the Composition API and focusing solely on core translation capabilities. It omits features like date/number formatting ($n, $d), rich text rendering ($rt), hierarchical message handling out-of-the-box, the v-t directive, and components like i18n-t, making it ideal for projects where only basic text translation is required and bundle size is critical. Migration to the full vue-i18n is designed to be seamless if more features become necessary later.
Common errors
-
Property '$n' does not exist on type 'I18n<MessageSchema, Locales, Values>'
cause Attempting to use the number formatting API (`$n` or `n`) which is excluded from petite-vue-i18n.fixRemove all references to `$n` or `n`. petite-vue-i18n is translation-only. Use full vue-i18n if number formatting is needed. -
Failed to resolve directive: t
cause The `v-t` custom directive is not included in petite-vue-i18n.fixReplace `v-t` directives with template interpolations like `{{ t('your.key') }}` after exposing `t` from `useI18n()` in your component's setup function. -
TypeError: Cannot read properties of undefined (reading 'nestedKey')
cause Attempting to access a nested translation key (e.g., `t('auth.login')`) when petite-vue-i18n's default resolver only supports flat keys.fixEither flatten your locale messages (e.g., `auth_login: '...'`) or provide a custom message resolver function during `createI18n` initialization. -
TypeError: app.use is not a function
cause This usually indicates that `createApp` from 'vue' was not correctly imported or called, meaning `app` is not a valid Vue application instance.fixEnsure you have `import { createApp } from 'vue'` and that `createApp()` is called to initialize your `app` constant before `app.use(i18n)`.
Warnings
- breaking petite-vue-i18n exclusively supports Vue 3's Composition API. Legacy Vue I18n Options API global properties like `$t` via `this` are not directly available without explicitly exposing `t` from `useI18n()` within setup.
- breaking The `v-t` custom directive for template translations is entirely omitted to reduce bundle size. It will not function and may cause errors if used.
- breaking DateTime (`$d`, `d`) and Number Formatting (`$n`, `n`) APIs are deliberately excluded from petite-vue-i18n. Attempting to use these will result in runtime errors.
- gotcha By default, `petite-vue-i18n` handles only simple, flat key-value locale messages. Hierarchical messages (e.g., `messages.en.auth.login`) are not resolved without custom message resolver configurations.
- gotcha The local fallback algorithm strictly follows the array order specified in `fallbackLocale`. This differs from `vue-i18n`'s more flexible fallback strategies, which may check all available locales.
Install
-
npm install petite-vue-i18n -
yarn add petite-vue-i18n -
pnpm add petite-vue-i18n
Imports
- createI18n
const createI18n = require('petite-vue-i18n')import { createI18n } from 'petite-vue-i18n' - useI18n
const { useI18n } = require('petite-vue-i18n')import { useI18n } from 'petite-vue-i18n' - Composer
import type { Composer } from 'petite-vue-i18n'
Quickstart
import { createApp } from 'vue'
import { createI18n, useI18n } from 'petite-vue-i18n'
const messages = {
en: {
'hello world': 'Hello world from petite-vue-i18n!',
'greeting': 'Welcome, {name}!',
'about': 'This is a lightweight i18n solution.'
},
fr: {
'hello world': 'Bonjour le monde de petite-vue-i18n !',
'greeting': 'Bienvenue, {name} !',
'about': 'Ceci est une solution i18n légère.'
}
}
const i18n = createI18n({
locale: 'en',
fallbackLocale: 'fr',
messages
})
const app = createApp({
setup() {
const { t, locale } = useI18n()
return { t, locale }
},
template: `
<div id="app">
<h1>{{ t('hello world') }}</h1>
<p>{{ t('greeting', { name: 'User' }) }}</p>
<p>{{ t('about') }}</p>
<button @click="locale = 'fr'">Switch to French</button>
<button @click="locale = 'en'">Switch to English</button>
</div>
`
})
app.use(i18n)
app.mount('#app')