@parlajs/vue 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Marco Pasqualetti
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,30 @@
1
- # Temporary Holding Version
1
+ # @parlajs/vue
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Vue bindings for [Parla](https://github.com/parlajs/parla). Re-exports
4
+ [`@parlajs/core`](../core), with rich text as Vue's `VNodeChild`. Requires Vue 3.5.
5
+
6
+ ```vue
7
+ <script setup lang="ts">
8
+ import { useLocale, useParla } from '@parlajs/vue';
9
+ import { h } from 'vue';
10
+
11
+ import { checkout } from './parla/namespaces/checkout.js'; // written by `parla build`
12
+
13
+ const { t } = await useParla([checkout]); // waits in <Suspense>; useParlaLoaded needs none
14
+ const { locale, switchLocale } = useLocale();
15
+ const terms = () => t.value.checkout.terms({ link: (c) => h('a', { href: '/terms' }, [c]) });
16
+ </script>
17
+
18
+ <template>
19
+ <p>{{ t.checkout.cart.items({ count: 3 }) }}</p>
20
+ <p><component :is="terms" /></p>
21
+ </template>
22
+ ```
23
+
24
+ Wrap the app in `<ParlaProvider :parla>`, with `createParla({ catalog })`.
25
+
26
+ Docs: [`@parlajs/vue`](https://parlajs.github.io/parla/adapters/vue/).
27
+
28
+ ## License
29
+
30
+ [MIT](./LICENSE)
@@ -0,0 +1,7 @@
1
+ export { createRuntime, direction, matchLocale, parseAcceptLanguage, type Catalog, type FunctionName, type FunctionRegistry, type Loader, type Locale, type LocaleRegistry, type MarkupName, type MarkupRegistry, type MessageFactory, type MessageFunction, type MessageFunctionInput, type MessageModule, type MessageRuntime, type RuntimeOptions, type Namespace, type NamespaceKind, type SelectorValue, type Text, } from '@parlajs/core';
2
+ export { createParla, type Loaded, type Parla, type ParlaOptions, type Translations, type WrapRich, } from './parla.ts';
3
+ export { ParlaProvider } from './provider.ts';
4
+ export { useLocale } from './useLocale.ts';
5
+ export { useParla } from './useParla.ts';
6
+ export { useParlaLoaded } from './useParlaLoaded.ts';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,aAAa,EACb,SAAS,EACT,WAAW,EACX,mBAAmB,EACnB,KAAK,OAAO,EACZ,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,MAAM,EACX,KAAK,MAAM,EACX,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,IAAI,GACV,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,WAAW,EACX,KAAK,MAAM,EACX,KAAK,KAAK,EACV,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,QAAQ,GACd,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC"}
package/build/index.js ADDED
@@ -0,0 +1,7 @@
1
+ // Named rather than `export *`, which would clash with this module's own exports.
2
+ export { createRuntime, direction, matchLocale, parseAcceptLanguage, } from '@parlajs/core';
3
+ export { createParla, } from './parla.js';
4
+ export { ParlaProvider } from './provider.js';
5
+ export { useLocale } from './useLocale.js';
6
+ export { useParla } from './useParla.js';
7
+ export { useParlaLoaded } from './useParlaLoaded.js';
@@ -0,0 +1,10 @@
1
+ import type { Loaded as CoreLoaded, Namespace, Parla as CoreParla, ParlaOptions as CoreParlaOptions, Translations as CoreTranslations, WrapRich as CoreWrapRich } from '@parlajs/core';
2
+ import type { VNodeChild } from 'vue';
3
+ export type Parla = CoreParla<VNodeChild>;
4
+ export type ParlaOptions = CoreParlaOptions<VNodeChild>;
5
+ export type Translations<NS extends Namespace> = CoreTranslations<NS, VNodeChild>;
6
+ export type Loaded<L extends ReadonlyArray<Namespace>> = CoreLoaded<L, VNodeChild>;
7
+ export type WrapRich = CoreWrapRich<VNodeChild>;
8
+ /** `createParla` from `@parlajs/core`, with rich text as Vue vnodes. */
9
+ export declare const createParla: (options: ParlaOptions) => Parla;
10
+ //# sourceMappingURL=parla.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parla.d.ts","sourceRoot":"","sources":["../src/parla.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,MAAM,IAAI,UAAU,EACpB,SAAS,EACT,KAAK,IAAI,SAAS,EAClB,YAAY,IAAI,gBAAgB,EAChC,YAAY,IAAI,gBAAgB,EAChC,QAAQ,IAAI,YAAY,EACzB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAGtC,MAAM,MAAM,KAAK,GAAG,SAAS,CAAC,UAAU,CAAC,CAAC;AAC1C,MAAM,MAAM,YAAY,GAAG,gBAAgB,CAAC,UAAU,CAAC,CAAC;AACxD,MAAM,MAAM,YAAY,CAAC,EAAE,SAAS,SAAS,IAAI,gBAAgB,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;AAClF,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,aAAa,CAAC,SAAS,CAAC,IAAI,UAAU,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC;AACnF,MAAM,MAAM,QAAQ,GAAG,YAAY,CAAC,UAAU,CAAC,CAAC;AAEhD,wEAAwE;AAExE,eAAO,MAAM,WAAW,EAAE,CAAC,OAAO,EAAE,YAAY,KAAK,KAAmC,CAAC"}
package/build/parla.js ADDED
@@ -0,0 +1,4 @@
1
+ import { createParla as createCoreParla } from '@parlajs/core';
2
+ /** `createParla` from `@parlajs/core`, with rich text as Vue vnodes. */
3
+ // No `wrapRich`: core's array of parts is a `VNodeChild` already.
4
+ export const createParla = createCoreParla;
@@ -0,0 +1,15 @@
1
+ import type { PropType, ShallowRef, VNodeChild } from 'vue';
2
+ import type { Parla } from './parla.ts';
3
+ export declare const ParlaProvider: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
4
+ parla: {
5
+ type: PropType<Parla>;
6
+ required: true;
7
+ };
8
+ }>, () => VNodeChild, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
9
+ parla: {
10
+ type: PropType<Parla>;
11
+ required: true;
12
+ };
13
+ }>> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
14
+ export declare function useInstance(): Readonly<ShallowRef<Parla>>;
15
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAgB,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAE1E,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAKxC,eAAO,MAAM,aAAa;;cAEU,QAAQ,CAAC,KAAK,CAAC;;;UAapC,UAAU;;;;;iGAEvB,CAAC;AAEH,wBAAgB,WAAW,IAAI,QAAQ,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAMzD"}
@@ -0,0 +1,27 @@
1
+ import { parlaMessage } from '@parlajs/core';
2
+ import { defineComponent, inject, onBeforeMount, onScopeDispose, provide, shallowRef, triggerRef, } from 'vue';
3
+ // Triggered on each locale switch.
4
+ const PARLA = Symbol('parla');
5
+ export const ParlaProvider = defineComponent({
6
+ name: 'ParlaProvider',
7
+ props: { parla: { type: Object, required: true } },
8
+ setup(props, { slots }) {
9
+ // One instance for life: another may not have loaded what `t` already promised.
10
+ const instance = shallowRef(props.parla);
11
+ // A server, where nothing unsubscribes, never mounts. `onMounted` would wait for a `<Suspense>` above.
12
+ onBeforeMount(() => {
13
+ onScopeDispose(props.parla.subscribe(() => {
14
+ triggerRef(instance);
15
+ }));
16
+ });
17
+ provide(PARLA, instance);
18
+ return () => slots['default']?.();
19
+ },
20
+ });
21
+ export function useInstance() {
22
+ const parla = inject(PARLA, null);
23
+ if (!parla) {
24
+ throw new Error(parlaMessage('wrap your app in <ParlaProvider :parla>'));
25
+ }
26
+ return parla;
27
+ }
@@ -0,0 +1,9 @@
1
+ import type { Locale } from '@parlajs/core';
2
+ import type { ComputedRef } from 'vue';
3
+ /** The current locale, the supported ones, and a switch that loads first. */
4
+ export declare function useLocale(): {
5
+ readonly locale: ComputedRef<Locale>;
6
+ readonly locales: ComputedRef<ReadonlyArray<Locale>>;
7
+ readonly switchLocale: (locale: Locale) => Promise<void>;
8
+ };
9
+ //# sourceMappingURL=useLocale.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useLocale.d.ts","sourceRoot":"","sources":["../src/useLocale.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AAIvC,6EAA6E;AAC7E,wBAAgB,SAAS,IAAI;IAC3B,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACrC,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC;IACrD,QAAQ,CAAC,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1D,CAOA"}
@@ -0,0 +1,11 @@
1
+ import { computed } from 'vue';
2
+ import { useInstance } from './provider.js';
3
+ /** The current locale, the supported ones, and a switch that loads first. */
4
+ export function useLocale() {
5
+ const parla = useInstance();
6
+ return {
7
+ locale: computed(() => parla.value.locale),
8
+ locales: computed(() => parla.value.locales),
9
+ switchLocale: async (locale) => parla.value.switchLocale(locale),
10
+ };
11
+ }
@@ -0,0 +1,11 @@
1
+ import type { Namespace } from '@parlajs/core';
2
+ import type { ComputedRef } from 'vue';
3
+ import type { Loaded } from './parla.ts';
4
+ /**
5
+ * Messages for the namespaces, keyed by namespace: `const { t } = await useParla([ns])` in an async
6
+ * `setup`, which waits in the nearest `<Suspense>`. A locale switch never waits: `switchLocale` loads first.
7
+ */
8
+ export declare function useParla<const L extends ReadonlyArray<Namespace>>(namespaces: L): Promise<{
9
+ readonly t: ComputedRef<Loaded<L>>;
10
+ }>;
11
+ //# sourceMappingURL=useParla.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useParla.d.ts","sourceRoot":"","sources":["../src/useParla.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AAEvC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAGzC;;;GAGG;AACH,wBAAsB,QAAQ,CAAC,KAAK,CAAC,CAAC,SAAS,aAAa,CAAC,SAAS,CAAC,EACrE,UAAU,EAAE,CAAC,GACZ,OAAO,CAAC;IAAE,QAAQ,CAAC,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;CAAE,CAAC,CAMjD"}
@@ -0,0 +1,13 @@
1
+ import { computed } from 'vue';
2
+ import { useInstance } from './provider.js';
3
+ /**
4
+ * Messages for the namespaces, keyed by namespace: `const { t } = await useParla([ns])` in an async
5
+ * `setup`, which waits in the nearest `<Suspense>`. A locale switch never waits: `switchLocale` loads first.
6
+ */
7
+ export async function useParla(namespaces) {
8
+ const parla = useInstance();
9
+ const t = computed(() => parla.value.loaded(namespaces));
10
+ // A switch meanwhile loads them too, so `t` then has them in the new locale.
11
+ await parla.value.load(namespaces);
12
+ return { t: t };
13
+ }
@@ -0,0 +1,8 @@
1
+ import type { Namespace } from '@parlajs/core';
2
+ import type { ComputedRef } from 'vue';
3
+ import type { Loaded } from './parla.ts';
4
+ /** `t` is `undefined` until the namespaces load: for components outside `<Suspense>`. */
5
+ export declare function useParlaLoaded<const L extends ReadonlyArray<Namespace>>(namespaces: L): {
6
+ readonly t: ComputedRef<Loaded<L> | undefined>;
7
+ };
8
+ //# sourceMappingURL=useParlaLoaded.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useParlaLoaded.d.ts","sourceRoot":"","sources":["../src/useParlaLoaded.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AAEvC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAGzC,yFAAyF;AACzF,wBAAgB,cAAc,CAAC,KAAK,CAAC,CAAC,SAAS,aAAa,CAAC,SAAS,CAAC,EACrE,UAAU,EAAE,CAAC,GACZ;IAAE,QAAQ,CAAC,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAA;CAAE,CAmBpD"}
@@ -0,0 +1,20 @@
1
+ import { computed, shallowRef, watch } from 'vue';
2
+ import { useInstance } from './provider.js';
3
+ /** `t` is `undefined` until the namespaces load: for components outside `<Suspense>`. */
4
+ export function useParlaLoaded(namespaces) {
5
+ const parla = useInstance();
6
+ const settled = shallowRef(0);
7
+ const t = computed(() => {
8
+ void settled.value;
9
+ return parla.value.loaded(namespaces);
10
+ });
11
+ watch(
12
+ // The locale too: after a failed download, a switch is the next chance to load.
13
+ [t, () => parla.value.locale], async ([value]) => {
14
+ if (!value) {
15
+ await parla.value.load(namespaces);
16
+ settled.value += 1;
17
+ }
18
+ }, { immediate: true });
19
+ return { t };
20
+ }
package/package.json CHANGED
@@ -1,6 +1,65 @@
1
1
  {
2
2
  "name": "@parlajs/vue",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.0",
4
+ "description": "Vue adapter for Parla.",
5
+ "keywords": [
6
+ "i18n",
7
+ "internationalization",
8
+ "l10n",
9
+ "localization",
10
+ "messageformat",
11
+ "parla",
12
+ "type-safe",
13
+ "typescript",
14
+ "vue",
15
+ "vue-i18n"
16
+ ],
17
+ "homepage": "https://github.com/parlajs/parla",
18
+ "bugs": {
19
+ "url": "https://github.com/parlajs/parla/issues"
20
+ },
21
+ "license": "MIT",
22
+ "author": {
23
+ "name": "Marco Pasqualetti",
24
+ "url": "https://github.com/marcalexiei"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/parlajs/parla.git",
29
+ "directory": "packages/vue"
30
+ },
31
+ "files": [
32
+ "build",
33
+ "src",
34
+ "README.md"
35
+ ],
36
+ "type": "module",
37
+ "exports": {
38
+ ".": {
39
+ "@parlajs/source": "./src/index.ts",
40
+ "default": "./build/index.js"
41
+ }
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "devDependencies": {
47
+ "@parlajs/core": "^0.0.0",
48
+ "happy-dom": "21.0.4",
49
+ "vitest": "5.1.1",
50
+ "vue": "3.5.46"
51
+ },
52
+ "peerDependencies": {
53
+ "@parlajs/core": "^0.0.0",
54
+ "vue": "^3.5.0"
55
+ },
56
+ "engines": {
57
+ "node": "^22.18 || ^24 || >=26"
58
+ },
59
+ "scripts": {
60
+ "build": "tsc -p tsconfig.build.json",
61
+ "lint:publish": "attw --pack . --profile esm-only && publint --strict",
62
+ "test": "vitest",
63
+ "typecheck": "tsc -p tsconfig.typecheck.json"
64
+ }
6
65
  }
package/src/index.ts ADDED
@@ -0,0 +1,37 @@
1
+ // Named rather than `export *`, which would clash with this module's own exports.
2
+ export {
3
+ createRuntime,
4
+ direction,
5
+ matchLocale,
6
+ parseAcceptLanguage,
7
+ type Catalog,
8
+ type FunctionName,
9
+ type FunctionRegistry,
10
+ type Loader,
11
+ type Locale,
12
+ type LocaleRegistry,
13
+ type MarkupName,
14
+ type MarkupRegistry,
15
+ type MessageFactory,
16
+ type MessageFunction,
17
+ type MessageFunctionInput,
18
+ type MessageModule,
19
+ type MessageRuntime,
20
+ type RuntimeOptions,
21
+ type Namespace,
22
+ type NamespaceKind,
23
+ type SelectorValue,
24
+ type Text,
25
+ } from '@parlajs/core';
26
+ export {
27
+ createParla,
28
+ type Loaded,
29
+ type Parla,
30
+ type ParlaOptions,
31
+ type Translations,
32
+ type WrapRich,
33
+ } from './parla.ts';
34
+ export { ParlaProvider } from './provider.ts';
35
+ export { useLocale } from './useLocale.ts';
36
+ export { useParla } from './useParla.ts';
37
+ export { useParlaLoaded } from './useParlaLoaded.ts';
package/src/parla.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { createParla as createCoreParla } from '@parlajs/core';
2
+ import type {
3
+ Loaded as CoreLoaded,
4
+ Namespace,
5
+ Parla as CoreParla,
6
+ ParlaOptions as CoreParlaOptions,
7
+ Translations as CoreTranslations,
8
+ WrapRich as CoreWrapRich,
9
+ } from '@parlajs/core';
10
+ import type { VNodeChild } from 'vue';
11
+
12
+ /* Core's types with rich text as a Vue `VNodeChild`. */
13
+ export type Parla = CoreParla<VNodeChild>;
14
+ export type ParlaOptions = CoreParlaOptions<VNodeChild>;
15
+ export type Translations<NS extends Namespace> = CoreTranslations<NS, VNodeChild>;
16
+ export type Loaded<L extends ReadonlyArray<Namespace>> = CoreLoaded<L, VNodeChild>;
17
+ export type WrapRich = CoreWrapRich<VNodeChild>;
18
+
19
+ /** `createParla` from `@parlajs/core`, with rich text as Vue vnodes. */
20
+ // No `wrapRich`: core's array of parts is a `VNodeChild` already.
21
+ export const createParla: (options: ParlaOptions) => Parla = createCoreParla<VNodeChild>;
@@ -0,0 +1,43 @@
1
+ import { parlaMessage } from '@parlajs/core';
2
+ import {
3
+ defineComponent,
4
+ inject,
5
+ onBeforeMount,
6
+ onScopeDispose,
7
+ provide,
8
+ shallowRef,
9
+ triggerRef,
10
+ } from 'vue';
11
+ import type { InjectionKey, PropType, ShallowRef, VNodeChild } from 'vue';
12
+
13
+ import type { Parla } from './parla.ts';
14
+
15
+ // Triggered on each locale switch.
16
+ const PARLA: InjectionKey<Readonly<ShallowRef<Parla>>> = Symbol('parla');
17
+
18
+ export const ParlaProvider = defineComponent({
19
+ name: 'ParlaProvider',
20
+ props: { parla: { type: Object as PropType<Parla>, required: true } },
21
+ setup(props, { slots }) {
22
+ // One instance for life: another may not have loaded what `t` already promised.
23
+ const instance = shallowRef(props.parla);
24
+ // A server, where nothing unsubscribes, never mounts. `onMounted` would wait for a `<Suspense>` above.
25
+ onBeforeMount(() => {
26
+ onScopeDispose(
27
+ props.parla.subscribe(() => {
28
+ triggerRef(instance);
29
+ }),
30
+ );
31
+ });
32
+ provide(PARLA, instance);
33
+ return (): VNodeChild => slots['default']?.();
34
+ },
35
+ });
36
+
37
+ export function useInstance(): Readonly<ShallowRef<Parla>> {
38
+ const parla = inject(PARLA, null);
39
+ if (!parla) {
40
+ throw new Error(parlaMessage('wrap your app in <ParlaProvider :parla>'));
41
+ }
42
+ return parla;
43
+ }
@@ -0,0 +1,19 @@
1
+ import type { Locale } from '@parlajs/core';
2
+ import { computed } from 'vue';
3
+ import type { ComputedRef } from 'vue';
4
+
5
+ import { useInstance } from './provider.ts';
6
+
7
+ /** The current locale, the supported ones, and a switch that loads first. */
8
+ export function useLocale(): {
9
+ readonly locale: ComputedRef<Locale>;
10
+ readonly locales: ComputedRef<ReadonlyArray<Locale>>;
11
+ readonly switchLocale: (locale: Locale) => Promise<void>;
12
+ } {
13
+ const parla = useInstance();
14
+ return {
15
+ locale: computed(() => parla.value.locale),
16
+ locales: computed(() => parla.value.locales),
17
+ switchLocale: async (locale) => parla.value.switchLocale(locale),
18
+ };
19
+ }
@@ -0,0 +1,20 @@
1
+ import type { Namespace } from '@parlajs/core';
2
+ import { computed } from 'vue';
3
+ import type { ComputedRef } from 'vue';
4
+
5
+ import type { Loaded } from './parla.ts';
6
+ import { useInstance } from './provider.ts';
7
+
8
+ /**
9
+ * Messages for the namespaces, keyed by namespace: `const { t } = await useParla([ns])` in an async
10
+ * `setup`, which waits in the nearest `<Suspense>`. A locale switch never waits: `switchLocale` loads first.
11
+ */
12
+ export async function useParla<const L extends ReadonlyArray<Namespace>>(
13
+ namespaces: L,
14
+ ): Promise<{ readonly t: ComputedRef<Loaded<L>> }> {
15
+ const parla = useInstance();
16
+ const t = computed(() => parla.value.loaded(namespaces));
17
+ // A switch meanwhile loads them too, so `t` then has them in the new locale.
18
+ await parla.value.load(namespaces);
19
+ return { t: t as ComputedRef<Loaded<L>> };
20
+ }
@@ -0,0 +1,30 @@
1
+ import type { Namespace } from '@parlajs/core';
2
+ import { computed, shallowRef, watch } from 'vue';
3
+ import type { ComputedRef } from 'vue';
4
+
5
+ import type { Loaded } from './parla.ts';
6
+ import { useInstance } from './provider.ts';
7
+
8
+ /** `t` is `undefined` until the namespaces load: for components outside `<Suspense>`. */
9
+ export function useParlaLoaded<const L extends ReadonlyArray<Namespace>>(
10
+ namespaces: L,
11
+ ): { readonly t: ComputedRef<Loaded<L> | undefined> } {
12
+ const parla = useInstance();
13
+ const settled = shallowRef(0);
14
+ const t = computed(() => {
15
+ void settled.value;
16
+ return parla.value.loaded(namespaces);
17
+ });
18
+ watch(
19
+ // The locale too: after a failed download, a switch is the next chance to load.
20
+ [t, (): string => parla.value.locale],
21
+ async ([value]) => {
22
+ if (!value) {
23
+ await parla.value.load(namespaces);
24
+ settled.value += 1;
25
+ }
26
+ },
27
+ { immediate: true },
28
+ );
29
+ return { t };
30
+ }