Ir al contenido
Works through lefine/strings

i18n para Vue para apps modernas

Vue works with lefine through lefine/strings: load the texts, build a translator, and keep it in a computed. No plugin, no binding.

Package
lefine
Entry
lefine/server · lefine/strings
Checked
Vue 3.5, SSR
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" });
03Home.vue
<script setup lang="ts">import { computed, ref, watch } from "vue";import { createStrings, type StringsProps } from "lefine/server";import { createTranslator } from "lefine/strings";const strings = createStrings({ project: "app_..." });const locale = ref("en");const props = ref<StringsProps | null>(null);watch(locale, async (l) => (props.value = await strings.load(l, ["home"])), { immediate: true });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 component

    Keep the translator in a computed and load again when the locale changes.

    Home.vue
    <script setup lang="ts">import { computed, ref, watch } from "vue";import { createStrings, type StringsProps } from "lefine/server";import { createTranslator } from "lefine/strings";const strings = createStrings({ project: "app_..." });const locale = ref("en");const props = ref<StringsProps | null>(null);watch(locale, async (l) => (props.value = await strings.load(l, ["home"])), { immediate: true });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

  • 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

  • Checked with Vue 3.5 and no React installed: server render and a switch from English to German.
  • 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 Vue binding?
No. Vue 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 Vue

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