@parlajs/react 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,28 @@
1
- # Temporary Holding Version
1
+ # @parlajs/react
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
+ React bindings for [Parla](https://github.com/parlajs/parla). Re-exports
4
+ [`@parlajs/core`](../core), with rich text as React nodes. Requires React 19.
5
+
6
+ ```tsx
7
+ import { useParla } from '@parlajs/react';
8
+
9
+ import { checkout } from './parla/namespaces/checkout.js'; // written by `parla build`
10
+
11
+ function Cart() {
12
+ const { t } = useParla([checkout]); // suspends until loaded
13
+ return (
14
+ <>
15
+ <p>{t.checkout.cart.items({ count: 3 })}</p>
16
+ <p>{t.checkout.terms({ link: (c) => <a href="/terms">{c}</a> })}</p>
17
+ </>
18
+ );
19
+ }
20
+ ```
21
+
22
+ Wrap the app in `<ParlaProvider parla={createParla({ catalog })}>` and a `<Suspense>`.
23
+
24
+ Docs: [`@parlajs/react`](https://parlajs.github.io/parla/adapters/react/).
25
+
26
+ ## License
27
+
28
+ [MIT](./LICENSE)
@@ -0,0 +1,6 @@
1
+ export * from './server.ts';
2
+ export { ParlaProvider } from './provider.ts';
3
+ export { useLocale } from './useLocale.ts';
4
+ export { useParla } from './useParla.ts';
5
+ export { useParlaLoaded } from './useParlaLoaded.ts';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,aAAa,CAAC;AAC5B,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,6 @@
1
+ // The server entry's exports, then the hooks it cannot have.
2
+ export * from './server.js';
3
+ export { ParlaProvider } from './provider.js';
4
+ export { useLocale } from './useLocale.js';
5
+ export { useParla } from './useParla.js';
6
+ export { useParlaLoaded } from './useParlaLoaded.js';
@@ -0,0 +1,9 @@
1
+ import type { ReactNode } from 'react';
2
+ import type { Parla } from './server.ts';
3
+ export declare function ParlaProvider({ parla, children, }: {
4
+ parla: Parla;
5
+ children?: ReactNode;
6
+ }): ReactNode;
7
+ /** The instance, re-rendering the component when the locale switches. */
8
+ export declare function useParlaInstance(): Parla;
9
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAEvC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAKzC,wBAAgB,aAAa,CAAC,EAC5B,KAAK,EACL,QAAQ,GACT,EAAE;IACD,KAAK,EAAE,KAAK,CAAC;IACb,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB,GAAG,SAAS,CAEZ;AAED,yEAAyE;AACzE,wBAAgB,gBAAgB,IAAI,KAAK,CAQxC"}
@@ -0,0 +1,17 @@
1
+ import { parlaMessage } from '@parlajs/core';
2
+ import { createContext, createElement, useContext, useSyncExternalStore } from 'react';
3
+ const ParlaContext = createContext(null);
4
+ ParlaContext.displayName = 'ParlaContext';
5
+ export function ParlaProvider({ parla, children, }) {
6
+ return createElement(ParlaContext.Provider, { value: parla }, children);
7
+ }
8
+ /** The instance, re-rendering the component when the locale switches. */
9
+ export function useParlaInstance() {
10
+ const parla = useContext(ParlaContext);
11
+ if (!parla) {
12
+ throw new Error(parlaMessage('wrap your app in <ParlaProvider parla={…}>'));
13
+ }
14
+ const locale = () => parla.locale;
15
+ useSyncExternalStore(parla.subscribe, locale, locale);
16
+ return parla;
17
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @file The `react-server` entry: React's server build has no `createContext`, so Server Components
3
+ * get `createParla` and the types without the hooks.
4
+ */
5
+ import type { Loaded as CoreLoaded, Namespace, Parla as CoreParla, ParlaOptions as CoreParlaOptions, Translations as CoreTranslations, WrapRich as CoreWrapRich } from '@parlajs/core';
6
+ import type { ReactNode } from 'react';
7
+ 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';
8
+ export type Parla = CoreParla<ReactNode>;
9
+ export type ParlaOptions = CoreParlaOptions<ReactNode>;
10
+ export type Translations<NS extends Namespace> = CoreTranslations<NS, ReactNode>;
11
+ export type Loaded<L extends ReadonlyArray<Namespace>> = CoreLoaded<L, ReactNode>;
12
+ export type WrapRich = CoreWrapRich<ReactNode>;
13
+ /** `createParla` from `@parlajs/core`, with rich text rendered as React nodes. */
14
+ export declare function createParla(options: ParlaOptions): Parla;
15
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,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;AAEvB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGvC,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;AAGvB,MAAM,MAAM,KAAK,GAAG,SAAS,CAAC,SAAS,CAAC,CAAC;AACzC,MAAM,MAAM,YAAY,GAAG,gBAAgB,CAAC,SAAS,CAAC,CAAC;AACvD,MAAM,MAAM,YAAY,CAAC,EAAE,SAAS,SAAS,IAAI,gBAAgB,CAAC,EAAE,EAAE,SAAS,CAAC,CAAC;AACjF,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,aAAa,CAAC,SAAS,CAAC,IAAI,UAAU,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;AAClF,MAAM,MAAM,QAAQ,GAAG,YAAY,CAAC,SAAS,CAAC,CAAC;AAK/C,kFAAkF;AAClF,wBAAgB,WAAW,CAAC,OAAO,EAAE,YAAY,GAAG,KAAK,CAExD"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @file The `react-server` entry: React's server build has no `createContext`, so Server Components
3
+ * get `createParla` and the types without the hooks.
4
+ */
5
+ import { createParla as createCoreParla } from '@parlajs/core';
6
+ import { createElement, Fragment } from 'react';
7
+ // Named rather than `export *`, which would clash with this module's own exports.
8
+ export { createRuntime, direction, matchLocale, parseAcceptLanguage, } from '@parlajs/core';
9
+ /** Children passed as arguments, so React does not ask for keys. */
10
+ const wrapRich = (parts) => createElement(Fragment, null, ...parts);
11
+ /** `createParla` from `@parlajs/core`, with rich text rendered as React nodes. */
12
+ export function createParla(options) {
13
+ return createCoreParla({ wrapRich, ...options });
14
+ }
@@ -0,0 +1,8 @@
1
+ import type { Locale } from '@parlajs/core';
2
+ /** The current locale, the supported ones, and a switch that loads first. */
3
+ export declare function useLocale(): {
4
+ readonly locale: Locale;
5
+ readonly locales: ReadonlyArray<Locale>;
6
+ readonly switchLocale: (locale: Locale) => Promise<void>;
7
+ };
8
+ //# 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;AAI5C,6EAA6E;AAC7E,wBAAgB,SAAS,IAAI;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IACxC,QAAQ,CAAC,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1D,CAGA"}
@@ -0,0 +1,6 @@
1
+ import { useParlaInstance } from './provider.js';
2
+ /** The current locale, the supported ones, and a switch that loads first. */
3
+ export function useLocale() {
4
+ const { locale, locales, switchLocale } = useParlaInstance();
5
+ return { locale, locales, switchLocale };
6
+ }
@@ -0,0 +1,10 @@
1
+ import type { Namespace } from '@parlajs/core';
2
+ import type { Loaded } from './server.ts';
3
+ /**
4
+ * Messages for the namespaces, keyed by namespace. Suspends until they load for the current
5
+ * locale; loaded ones never suspend, and neither does a locale switch: `switchLocale` loads first.
6
+ */
7
+ export declare function useParla<const L extends ReadonlyArray<Namespace>>(namespaces: L): {
8
+ readonly t: Loaded<L>;
9
+ };
10
+ //# 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;AAI/C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,KAAK,CAAC,CAAC,SAAS,aAAa,CAAC,SAAS,CAAC,EAC/D,UAAU,EAAE,CAAC,GACZ;IAAE,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAA;CAAE,CAI3B"}
@@ -0,0 +1,11 @@
1
+ import { use } from 'react';
2
+ import { useParlaInstance } from './provider.js';
3
+ /**
4
+ * Messages for the namespaces, keyed by namespace. Suspends until they load for the current
5
+ * locale; loaded ones never suspend, and neither does a locale switch: `switchLocale` loads first.
6
+ */
7
+ export function useParla(namespaces) {
8
+ const parla = useParlaInstance();
9
+ // No `use` once loaded: in a synchronous `act`, it stops React before updates from effects.
10
+ return { t: parla.loaded(namespaces) ?? use(parla.load(namespaces)) };
11
+ }
@@ -0,0 +1,10 @@
1
+ import type { Namespace } from '@parlajs/core';
2
+ import type { Loaded } from './server.ts';
3
+ /**
4
+ * `{ t }` once the namespaces load for the current locale, `{ t: undefined }` until then: it never
5
+ * suspends. A failed download reaches the error boundary, as with `useParla`.
6
+ */
7
+ export declare function useParlaLoaded<const L extends ReadonlyArray<Namespace>>(namespaces: L): {
8
+ readonly t: Loaded<L> | undefined;
9
+ };
10
+ //# 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;AAI/C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,CAAC,CAAC,SAAS,aAAa,CAAC,SAAS,CAAC,EACrE,UAAU,EAAE,CAAC,GACZ;IAAE,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,GAAG,SAAS,CAAA;CAAE,CAsBvC"}
@@ -0,0 +1,30 @@
1
+ import { useEffect, useReducer, useState } from 'react';
2
+ import { useParlaInstance } from './provider.js';
3
+ /**
4
+ * `{ t }` once the namespaces load for the current locale, `{ t: undefined }` until then: it never
5
+ * suspends. A failed download reaches the error boundary, as with `useParla`.
6
+ */
7
+ export function useParlaLoaded(namespaces) {
8
+ const parla = useParlaInstance();
9
+ const t = parla.loaded(namespaces);
10
+ const [, rerender] = useReducer((n) => n + 1, 0);
11
+ const [failure, setFailure] = useState();
12
+ // No deps: `namespaces` is a new array each render, and `load` returns the cached promise.
13
+ useEffect(() => {
14
+ if (!t) {
15
+ void (async () => {
16
+ try {
17
+ await parla.load(namespaces);
18
+ rerender();
19
+ }
20
+ catch (error) {
21
+ setFailure({ error });
22
+ }
23
+ })();
24
+ }
25
+ });
26
+ if (failure) {
27
+ throw failure.error;
28
+ }
29
+ return { t };
30
+ }
package/package.json CHANGED
@@ -1,6 +1,74 @@
1
1
  {
2
2
  "name": "@parlajs/react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.0",
4
+ "description": "React adapter for Parla.",
5
+ "keywords": [
6
+ "i18n",
7
+ "internationalization",
8
+ "l10n",
9
+ "localization",
10
+ "messageformat",
11
+ "parla",
12
+ "react",
13
+ "react-i18n",
14
+ "type-safe",
15
+ "typescript"
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/react"
30
+ },
31
+ "files": [
32
+ "build",
33
+ "src",
34
+ "README.md"
35
+ ],
36
+ "type": "module",
37
+ "exports": {
38
+ ".": {
39
+ "react-server": {
40
+ "@parlajs/source": "./src/server.ts",
41
+ "default": "./build/server.js"
42
+ },
43
+ "@parlajs/source": "./src/index.ts",
44
+ "default": "./build/index.js"
45
+ }
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "devDependencies": {
51
+ "@parlajs/core": "^0.0.0",
52
+ "@testing-library/dom": "10.4.2",
53
+ "@testing-library/react": "16.3.2",
54
+ "@types/react": "19.3.0",
55
+ "@types/react-dom": "19.3.0",
56
+ "happy-dom": "21.0.4",
57
+ "react": "19.3.1",
58
+ "react-dom": "19.3.1",
59
+ "vitest": "5.1.1"
60
+ },
61
+ "peerDependencies": {
62
+ "@parlajs/core": "^0.0.0",
63
+ "react": "^19.0.0"
64
+ },
65
+ "engines": {
66
+ "node": "^22.18 || ^24 || >=26"
67
+ },
68
+ "scripts": {
69
+ "build": "tsc -p tsconfig.build.json",
70
+ "lint:publish": "attw --pack . --profile esm-only && publint --strict",
71
+ "test": "vitest",
72
+ "typecheck": "tsc -p tsconfig.typecheck.json"
73
+ }
6
74
  }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ // The server entry's exports, then the hooks it cannot have.
2
+ export * from './server.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';
@@ -0,0 +1,30 @@
1
+ import { parlaMessage } from '@parlajs/core';
2
+ import type { Locale } from '@parlajs/core';
3
+ import { createContext, createElement, useContext, useSyncExternalStore } from 'react';
4
+ import type { ReactNode } from 'react';
5
+
6
+ import type { Parla } from './server.ts';
7
+
8
+ const ParlaContext = createContext<Parla | null>(null);
9
+ ParlaContext.displayName = 'ParlaContext';
10
+
11
+ export function ParlaProvider({
12
+ parla,
13
+ children,
14
+ }: {
15
+ parla: Parla;
16
+ children?: ReactNode;
17
+ }): ReactNode {
18
+ return createElement(ParlaContext.Provider, { value: parla }, children);
19
+ }
20
+
21
+ /** The instance, re-rendering the component when the locale switches. */
22
+ export function useParlaInstance(): Parla {
23
+ const parla = useContext(ParlaContext);
24
+ if (!parla) {
25
+ throw new Error(parlaMessage('wrap your app in <ParlaProvider parla={…}>'));
26
+ }
27
+ const locale = (): Locale => parla.locale;
28
+ useSyncExternalStore(parla.subscribe, locale, locale);
29
+ return parla;
30
+ }
package/src/server.ts ADDED
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @file The `react-server` entry: React's server build has no `createContext`, so Server Components
3
+ * get `createParla` and the types without the hooks.
4
+ */
5
+
6
+ import { createParla as createCoreParla } from '@parlajs/core';
7
+ import type {
8
+ Loaded as CoreLoaded,
9
+ Namespace,
10
+ Parla as CoreParla,
11
+ ParlaOptions as CoreParlaOptions,
12
+ Translations as CoreTranslations,
13
+ WrapRich as CoreWrapRich,
14
+ } from '@parlajs/core';
15
+ import { createElement, Fragment } from 'react';
16
+ import type { ReactNode } from 'react';
17
+
18
+ // Named rather than `export *`, which would clash with this module's own exports.
19
+ export {
20
+ createRuntime,
21
+ direction,
22
+ matchLocale,
23
+ parseAcceptLanguage,
24
+ type Catalog,
25
+ type FunctionName,
26
+ type FunctionRegistry,
27
+ type Loader,
28
+ type Locale,
29
+ type LocaleRegistry,
30
+ type MarkupName,
31
+ type MarkupRegistry,
32
+ type MessageFactory,
33
+ type MessageFunction,
34
+ type MessageFunctionInput,
35
+ type MessageModule,
36
+ type MessageRuntime,
37
+ type RuntimeOptions,
38
+ type Namespace,
39
+ type NamespaceKind,
40
+ type SelectorValue,
41
+ type Text,
42
+ } from '@parlajs/core';
43
+
44
+ /* Core's types with rich text as a `ReactNode`. */
45
+ export type Parla = CoreParla<ReactNode>;
46
+ export type ParlaOptions = CoreParlaOptions<ReactNode>;
47
+ export type Translations<NS extends Namespace> = CoreTranslations<NS, ReactNode>;
48
+ export type Loaded<L extends ReadonlyArray<Namespace>> = CoreLoaded<L, ReactNode>;
49
+ export type WrapRich = CoreWrapRich<ReactNode>;
50
+
51
+ /** Children passed as arguments, so React does not ask for keys. */
52
+ const wrapRich: WrapRich = (parts) => createElement(Fragment, null, ...parts);
53
+
54
+ /** `createParla` from `@parlajs/core`, with rich text rendered as React nodes. */
55
+ export function createParla(options: ParlaOptions): Parla {
56
+ return createCoreParla<ReactNode>({ wrapRich, ...options });
57
+ }
@@ -0,0 +1,13 @@
1
+ import type { Locale } from '@parlajs/core';
2
+
3
+ import { useParlaInstance } from './provider.ts';
4
+
5
+ /** The current locale, the supported ones, and a switch that loads first. */
6
+ export function useLocale(): {
7
+ readonly locale: Locale;
8
+ readonly locales: ReadonlyArray<Locale>;
9
+ readonly switchLocale: (locale: Locale) => Promise<void>;
10
+ } {
11
+ const { locale, locales, switchLocale } = useParlaInstance();
12
+ return { locale, locales, switchLocale };
13
+ }
@@ -0,0 +1,17 @@
1
+ import type { Namespace } from '@parlajs/core';
2
+ import { use } from 'react';
3
+
4
+ import { useParlaInstance } from './provider.ts';
5
+ import type { Loaded } from './server.ts';
6
+
7
+ /**
8
+ * Messages for the namespaces, keyed by namespace. Suspends until they load for the current
9
+ * locale; loaded ones never suspend, and neither does a locale switch: `switchLocale` loads first.
10
+ */
11
+ export function useParla<const L extends ReadonlyArray<Namespace>>(
12
+ namespaces: L,
13
+ ): { readonly t: Loaded<L> } {
14
+ const parla = useParlaInstance();
15
+ // No `use` once loaded: in a synchronous `act`, it stops React before updates from effects.
16
+ return { t: parla.loaded(namespaces) ?? use(parla.load(namespaces)) };
17
+ }
@@ -0,0 +1,35 @@
1
+ import type { Namespace } from '@parlajs/core';
2
+ import { useEffect, useReducer, useState } from 'react';
3
+
4
+ import { useParlaInstance } from './provider.ts';
5
+ import type { Loaded } from './server.ts';
6
+
7
+ /**
8
+ * `{ t }` once the namespaces load for the current locale, `{ t: undefined }` until then: it never
9
+ * suspends. A failed download reaches the error boundary, as with `useParla`.
10
+ */
11
+ export function useParlaLoaded<const L extends ReadonlyArray<Namespace>>(
12
+ namespaces: L,
13
+ ): { readonly t: Loaded<L> | undefined } {
14
+ const parla = useParlaInstance();
15
+ const t = parla.loaded(namespaces);
16
+ const [, rerender] = useReducer((n: number) => n + 1, 0);
17
+ const [failure, setFailure] = useState<{ readonly error: unknown }>();
18
+ // No deps: `namespaces` is a new array each render, and `load` returns the cached promise.
19
+ useEffect(() => {
20
+ if (!t) {
21
+ void (async (): Promise<void> => {
22
+ try {
23
+ await parla.load(namespaces);
24
+ rerender();
25
+ } catch (error) {
26
+ setFailure({ error });
27
+ }
28
+ })();
29
+ }
30
+ });
31
+ if (failure) {
32
+ throw failure.error;
33
+ }
34
+ return { t };
35
+ }