Ir al contenido
Works through lefine/strings · not tested in Nuxt

Integración i18n para Nuxt

Nuxt runs the same code as Vue: load the texts in useAsyncData, then build the translator in a computed. It was checked in Vue, not yet in Nuxt itself.

Package
lefine
Entry
lefine/server · lefine/strings
Checked
Vue 3.5 only
lefine
01Terminal
npm install lefine
02strings.ts
// any framework, server or browserimport { createStrings } from "lefine/server";import { createTranslator } from "lefine/strings";const strings = createStrings({ project: "app_..." }); // one per appconst props = await strings.load(locale, ["home"]);      // plain JSONconst t = createTranslator({ ...props, namespace: "home" });
03pages/[locale]/index.vue
<script setup lang="ts">import { computed } from "vue";import { createStrings } from "lefine/server";import { createTranslator } from "lefine/strings";const strings = createStrings({ project: "app_..." });const route = useRoute();const locale = computed(() => String(route.params.locale ?? "en"));// Runs on the server and passes plain JSON to the client.const { data: props } = await useAsyncData(() => strings.load(locale.value, ["home"]), { watch: [locale] });const t = computed(() => (props.value ? createTranslator({ ...props.value, namespace: "home" }) : null));</script><template>  <h1 v-if="t">{{ t("greeting", { name: "World" }) }}</h1></template>
t("greeting", { name: "World" })
Hello, World
01/Setup

Set it up

  1. 01

    Install

    One package. lefine/server and lefine/strings import no React and no Next.

    terminalbash
    npm install lefine
  2. 02

    Load and translate

    createStrings takes your app id; load returns plain JSON; createTranslator gives you t.

    strings.ts
    // any framework, server or browserimport { createStrings } from "lefine/server";import { createTranslator } from "lefine/strings";const strings = createStrings({ project: "app_..." }); // one per appconst props = await strings.load(locale, ["home"]);      // plain JSONconst t = createTranslator({ ...props, namespace: "home" });
  3. 03

    Use it in a page

    useAsyncData runs the load on the server and passes plain JSON to the client.

    pages/[locale]/index.vue
    <script setup lang="ts">import { computed } from "vue";import { createStrings } from "lefine/server";import { createTranslator } from "lefine/strings";const strings = createStrings({ project: "app_..." });const route = useRoute();const locale = computed(() => String(route.params.locale ?? "en"));// Runs on the server and passes plain JSON to the client.const { data: props } = await useAsyncData(() => strings.load(locale.value, ["home"]), { watch: [locale] });const t = computed(() => (props.value ? createTranslator({ ...props.value, namespace: "home" }) : null));</script><template>  <h1 v-if="t">{{ t("greeting", { name: "World" }) }}</h1></template>
02/Notes

Good to know

  • Not tested in Nuxt yet. The Vue check runs the same code path; tell us if something breaks.
  • Size in the browser: loading on the client is about 7 KB gzip (it includes the compiler). Loading on the server and sending the props keeps only the formatter, about 1.3 KB gzip.
  • t.rich returns the parts as an array. There is no component for tags yet, so render the array yourself or keep markup out of those texts.
03/Capabilities

What works

  • The Vue 3.5 check (server render, locale switch) covers the same code. Nuxt itself has not been run.
  • ICU plurals, selects, numbers and dates, from the texts you publish in Lefine.
  • Server render and a locale switch: load again when the locale changes.
  • Texts come from the Lefine edge by app id, so a publish needs no rebuild.
04/FAQ

FAQ

Is there a Nuxt binding?
No. Nuxt works through lefine/strings: the same createTranslator every framework uses, in a few lines of your own. That is all the pattern above is.
Where do I load the texts?
On the server when you can: send the props to the page and only the formatter reaches the browser. Loading in the browser works too.
Get started

Comienza gratis.

Empieza a construir con i18n para Nuxt

Plan gratuito disponible. No se requiere tarjeta de crédito.