# Vue Tiny Translation 🌍✨ A minimalist reactive translation plugin for Vue 3. Super lightweight (**~0.35 KB gzipped**) — zero dependencies, full TypeScript support, reactive out of the box. [![npm version](https://img.shields.io/npm/v/vue-tiny-translation.svg)](https://www.npmjs.com/package/vue-tiny-translation) [![npm downloads](https://img.shields.io/npm/dm/vue-tiny-translation.svg)](https://www.npmjs.com/package/vue-tiny-translation) [![bundle size](https://img.shields.io/bundlephobia/minzip/vue-tiny-translation?label=min%2Bgzip)](https://bundlephobia.com/package/vue-tiny-translation) [![types included](https://img.shields.io/badge/types-included-blue.svg)](./src/index.d.ts) [![Vue 3](https://img.shields.io/badge/vue-3-42b883.svg)](https://vuejs.org/) [![license](https://img.shields.io/npm/l/vue-tiny-translation.svg)](./LICENSE) 🌐 **[Live Demo](https://vue-tiny-translation.netlify.app/)** — 35 languages, reactive language switching, no page reload. [vue-tiny-translation demo](https://vue-tiny-translation.netlify.app/) ## Contents - [Features](#features) - [Installation](#installation) - [Quick Start](#quick-start) - [Composition API](#composition-api) - [Loading translations](#loading-translations) - [TypeScript](#typescript) - [CDN usage](#cdn-usage) - [API Reference](#api-reference) - [vs vue-i18n](#vs-vue-i18n) - [Troubleshooting](#troubleshooting) - [Local development](#local-development) - [License](#license) ## Features - 🚀 **Tiny** — ~0.35 KB gzipped. Zero runtime dependencies. - ⚡ **Reactive** — components re-render automatically when the language changes. No page reload. - 🔧 **Four-function API** — `$t`, `translate`, `loadTranslations`, `setTranslations`. Nothing else to learn. - 🌐 **Any source** — load translations from a static file, an API, a CMS, or a bundled import. - 📦 **TypeScript** — full `.d.ts` with `$t` augmentation for `ComponentCustomProperties`. - 🌳 **Tree-shakeable** — `sideEffects: false`, ESM-only, dev warnings stripped from the production build. ## Installation ```bash npm install vue-tiny-translation # or pnpm add vue-tiny-translation # or yarn add vue-tiny-translation ``` ## Quick Start **1. Register the plugin** ```js // main.js import { createApp } from 'vue' import TinyTranslation from 'vue-tiny-translation' import App from './App.vue' createApp(App).use(TinyTranslation).mount('#app') ``` You can also seed initial translations at install time: ```js app.use(TinyTranslation, { hello: 'Hello', goodbye: 'Goodbye' }) ``` **2. Use `$t` in templates** ```vue ``` **3. Load a language** ```vue ``` Switching languages is just another `loadTranslations()` call — every `$t` re-renders reactively. ## Composition API Use `useTranslation()` from ` ``` ## Loading translations ```js import { loadTranslations, setTranslations } from 'vue-tiny-translation' // from a static file in /public await loadTranslations('/translations/en.json') // from your API await loadTranslations('/api/i18n?lang=en') // from an already-imported object (no fetch) import en from './locales/en.json' setTranslations(en) ``` ### Organizing translation files Put JSON files wherever your bundler can serve them. A common layout: ``` public/translations/ en.json fr.json ja.json ``` ```json { "hello": "Hello", "goodbye": "Goodbye", "hero.title": "Welcome", "hero.subtitle": "Enjoy your stay" } ``` Dot-notation keys are strings — there's no nested-object lookup by design. Keep it flat and searchable. ## TypeScript Types ship with the package, no `@types/` install needed. The plugin augments Vue's `ComponentCustomProperties` so `this.$t` is typed in every component. ```ts import { translate, useTranslation, setTranslations } from 'vue-tiny-translation' translate('hello') // string translate('missing', 'Fallback') // string const { t } = useTranslation() const greeting: string = t('hello') setTranslations({ hello: 'Hello', 'hero.title': 'Welcome' }) ``` ## CDN usage Because the package is ESM-only, you can load it directly from a CDN via an import map: ```html
{{ $t('hello') }}
``` ## API Reference | Export | Signature | Description | |---|---|---| | `default` (plugin) | `app.use(plugin, initial?)` | Registers `$t` on every component. Optional initial dictionary. | | `translate` | `(key, fallback?) => value` | Look up a key. Returns the fallback if provided, otherwise the key itself. | | `useTranslation` | `() => { t, loadTranslations, setTranslations }` | Composable for `