DocsVue

Quickstart - Vue

Translate a Vue 3 app with the @generic-translation/vue plugin, the T component, and the Generic Translation CLI.

On this page

This adds Spanish, French and Japanese to a Vue 3 app using @generic-translation/vue. The package works with Vite, Nuxt-style SSR and plain @vue/server-renderer. The source is in generic-translation/generic-translation-vue; the full reference is on the Vue reference page.

1. Install

@generic-translation/vue, @generic-translation/core and @generic-translation/cli are not published to npm yet, so pnpm add @generic-translation/vue will not work as written. Clone the parent repository, build the packages and link them.

git clone --recurse-submodules https://github.com/generic-translation/generic-translation.git
for p in core vue cli; do (cd generic-translation/packages/$p && pnpm install && pnpm build); done
{
  "dependencies": {
    "@generic-translation/core": "link:../generic-translation/packages/core",
    "@generic-translation/vue": "link:../generic-translation/packages/vue"
  },
  "devDependencies": {
    "@generic-translation/cli": "link:../generic-translation/packages/cli"
  }
}

The peer dependency is vue ^3.4. Once the packages are published the commands become pnpm add @generic-translation/vue @generic-translation/core and pnpm add -D @generic-translation/cli.

2. Install the plugin

// main.ts
import { createApp } from "vue";
import { createGenericTranslation } from "@generic-translation/vue";
import App from "./App.vue";

const genericTranslation = createGenericTranslation({
  defaultLocale: "en",
  locales: ["es", "fr", "ja"],
  loadTranslations: (locale) => fetch(`/_generic-translation/${locale}.json`).then((r) => r.json()),
});

createApp(App).use(genericTranslation).mount("#app");

The plugin registers T, Var, Num, Currency, DateTime, RelativeTime, Plural, Branch and LocaleSelector globally. You can also import them from @generic-translation/vue.

3. Mark up your templates

<script setup lang="ts">
import { ref } from "vue";
import { useGenericTranslation } from "@generic-translation/vue";

const t = useGenericTranslation();
const user = ref({ name: "Ada" });
const count = ref(3);
</script>

<template>
  <LocaleSelector />
  <T>
    Hello <b><Var name="user">{{ user.name }}</Var></b>!
    <Plural :n="count">
      <template #one>You have one message.</template>
      You have <Num :value="count" /> messages.
    </Plural>
  </T>
  <input :placeholder="t('Search {n, plural, one {# item} other {# items}}', { n: count })" />
  <footer>{{ $t("Made with care") }}</footer>
</template>

Inside <T>, dynamic values go through <Var>, <Num>, <Currency> or <DateTime>; a bare {{ expression }} is an error because its value would become part of the source text. See the serialization rules.

4. Extract and translate

npx generic-translation init --framework vue --locales es,fr,ja
npx generic-translation translate

generic-translation init detects Vue and Nuxt and adds .vue to the src globs. generic-translation translate extracts every <T>, every t("…") / $t("…") call and dictionary entries from your .vue files, translates what is new, and writes public/_generic-translation/{locale}.json with the default config. Without installing @generic-translation/cli you can run npx @generic-translation/cli translate. Both need the package to be on npm, so until then run the built CLI with node /path/to/generic-translation/packages/cli/dist/cli.js.

Instead of local files you can publish to the Generic Translation CDN and pass projectId to createGenericTranslation rather than loadTranslations.

Known extraction limits for .vue files (a shadowed t in v-for, lang="pug" templates) are listed on the CLI commands page.

Server rendering

For SSR, create one plugin per request, pass locale and translations, and await genericTranslation.ready() before rendering. Details are in SSR and hydration.

Next steps

Something wrong or unclear on this page? Open an issue.

Type to search the documentation.