@parlajs/vue
Framework adapterpnpm add @parlajs/vue
Requires Vue 3.5. Re-exports @parlajs/core, with rich text typed as Vue’s VNodeChild.
The showcase runs on it.
vite.config.ts:
import { parla } from '@parlajs/compiler/vite';import vue from '@vitejs/plugin-vue';import { defineConfig } from 'vite';
export default defineConfig({ plugins: [parla(), vue()] });The root component:
<script setup lang="ts">import { createParla, ParlaProvider } from '@parlajs/vue';
import App from './App.vue';import { catalog } from './parla/catalog.js';
const parla = createParla({ catalog });</script>
<template> <ParlaProvider :parla> <Suspense> <App /> <template #fallback>…</template> </Suspense> </ParlaProvider></template>| Export | |
|---|---|
createParla(options) |
Core’s, with rich text rendered as vnodes |
<ParlaProvider :parla> |
Provides the instance, the same for life |
await useParla(list) |
{ t }, a computed ref, once loaded: for an async setup |
useParlaLoaded(list) |
{ t }, whose value is undefined until loaded. Never suspends |
useLocale() |
{ locale, locales, switchLocale }; the first two are computed refs |
Translations<typeof ns> |
The type of t’s value, with VNodeChild rich text |
A failed switch rejects switchLocale. They throw outside <ParlaProvider>,
and after an await outside <script setup>: call them before it.
Loading
Section titled “Loading”await useParla waits in the nearest <Suspense>:
<script setup lang="ts">import { useParla } from '@parlajs/vue';import { checkout } from './parla/namespaces/checkout.js';import Terms from './Terms.vue';
defineProps<{ count: number }>();const { t } = await useParla([checkout]); // waits in <Suspense></script>
<template> <h1>{{ t.checkout.items({ count }) }}</h1> <Terms /></template>Any component can call useParla or take t as a prop. A namespace loads once per locale. Nested
calls load in turn, unless the route loader did first, with await parla.load([…]).
Use useParla by default, and useParlaLoaded for a placeholder of your own, without <Suspense>, or
for what mounts later: a resolved <Suspense> shows no fallback for it.
<script setup lang="ts">import { useParlaLoaded } from '@parlajs/vue';import { checkout } from './parla/namespaces/checkout.js';
const { t } = useParlaLoaded([checkout]);</script>
<template> <span>{{ t?.checkout.title() ?? '…' }}</span></template>Switching locale
Section titled “Switching locale”switchLocale loads first, then switches: nothing waits again.
<script setup lang="ts">import { type Locale, useLocale } from '@parlajs/vue';import { ref } from 'vue';
const { locale, locales, switchLocale } = useLocale();const pending = ref(false);const change = (l: Locale) => { pending.value = true; switchLocale(l) .catch(reportError) .finally(() => (pending.value = false));};</script>
<template> <button v-for="l in locales" :key="l" :disabled="pending || l === locale" @click="change(l)"> {{ l }} </button></template>Rich text
Section titled “Rich text”Each tag’s callback returns a VNodeChild, which <component :is> renders from a function:
<script setup lang="ts">import { useParla } from '@parlajs/vue';import { h } from 'vue';import { checkout } from './parla/namespaces/checkout.js';
const { t } = await useParla([checkout]);const terms = () => t.value.checkout.terms({ link: (c) => h('a', { href: '/terms' }, [c]) });</script>
<template> <p><component :is="terms" /></p></template>