@parlajs/solid 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 +21 -0
- package/README.md +27 -2
- package/build/index.d.ts +6 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +6 -0
- package/build/parla.d.ts +10 -0
- package/build/parla.d.ts.map +1 -0
- package/build/parla.js +4 -0
- package/build/provider.d.ts +13 -0
- package/build/provider.d.ts.map +1 -0
- package/build/provider.js +28 -0
- package/build/useLocale.d.ts +9 -0
- package/build/useLocale.d.ts.map +1 -0
- package/build/useLocale.js +10 -0
- package/build/useParla.d.ts +11 -0
- package/build/useParla.d.ts.map +1 -0
- package/build/useParla.js +31 -0
- package/package.json +65 -3
- package/src/index.ts +36 -0
- package/src/parla.ts +21 -0
- package/src/provider.ts +52 -0
- package/src/useLocale.ts +18 -0
- package/src/useParla.ts +39 -0
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
|
-
#
|
|
1
|
+
# @parlajs/solid
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Solid bindings for [Parla](https://github.com/parlajs/parla). Re-exports
|
|
4
|
+
[`@parlajs/core`](../core), with rich text as Solid's `Element`. Requires Solid 2.
|
|
5
|
+
|
|
6
|
+
```tsx
|
|
7
|
+
import { useParla } from '@parlajs/solid';
|
|
8
|
+
|
|
9
|
+
import { checkout } from './parla/namespaces/checkout.js'; // written by `parla build`
|
|
10
|
+
|
|
11
|
+
function Cart() {
|
|
12
|
+
const { t } = useParla([checkout]); // an accessor: reading it waits in <Loading> 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 `<Loading>`.
|
|
23
|
+
|
|
24
|
+
Docs: [`@parlajs/solid`](https://parlajs.github.io/parla/adapters/solid/).
|
|
25
|
+
|
|
26
|
+
## License
|
|
27
|
+
|
|
28
|
+
[MIT](./LICENSE)
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
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
|
+
//# 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"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
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';
|
package/build/parla.d.ts
ADDED
|
@@ -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 { Element } from 'solid-js';
|
|
3
|
+
export type Parla = CoreParla<Element>;
|
|
4
|
+
export type ParlaOptions = CoreParlaOptions<Element>;
|
|
5
|
+
export type Translations<NS extends Namespace> = CoreTranslations<NS, Element>;
|
|
6
|
+
export type Loaded<L extends ReadonlyArray<Namespace>> = CoreLoaded<L, Element>;
|
|
7
|
+
export type WrapRich = CoreWrapRich<Element>;
|
|
8
|
+
/** `createParla` from `@parlajs/core`, with rich text as Solid elements. */
|
|
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,OAAO,EAAE,MAAM,UAAU,CAAC;AAGxC,MAAM,MAAM,KAAK,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;AACvC,MAAM,MAAM,YAAY,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;AACrD,MAAM,MAAM,YAAY,CAAC,EAAE,SAAS,SAAS,IAAI,gBAAgB,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;AAC/E,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,aAAa,CAAC,SAAS,CAAC,IAAI,UAAU,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;AAChF,MAAM,MAAM,QAAQ,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;AAE7C,4EAA4E;AAE5E,eAAO,MAAM,WAAW,EAAE,CAAC,OAAO,EAAE,YAAY,KAAK,KAAgC,CAAC"}
|
package/build/parla.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Locale } from '@parlajs/core';
|
|
2
|
+
import type { Accessor, Element } from 'solid-js';
|
|
3
|
+
import type { Parla } from './parla.ts';
|
|
4
|
+
export interface Source {
|
|
5
|
+
readonly parla: Parla;
|
|
6
|
+
readonly locale: Accessor<Locale>;
|
|
7
|
+
}
|
|
8
|
+
export declare function ParlaProvider(props: {
|
|
9
|
+
readonly parla: Parla;
|
|
10
|
+
readonly children?: Element;
|
|
11
|
+
}): Element;
|
|
12
|
+
export declare function useSource(): Accessor<Source>;
|
|
13
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAS5C,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAElD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,MAAM,WAAW,MAAM;IACrB,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;CACnC;AAID,wBAAgB,aAAa,CAAC,KAAK,EAAE;IACnC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;CAC7B,GAAG,OAAO,CAkBV;AAGD,wBAAgB,SAAS,IAAI,QAAQ,CAAC,MAAM,CAAC,CAM5C"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { parlaMessage } from '@parlajs/core';
|
|
2
|
+
import { createComponent, createContext, createMemo, createSignal, onCleanup, useContext, } from 'solid-js';
|
|
3
|
+
const ParlaContext = createContext(null);
|
|
4
|
+
export function ParlaProvider(props) {
|
|
5
|
+
const source = createMemo(() => {
|
|
6
|
+
const { parla } = props;
|
|
7
|
+
// A signal, as Solid holds a switch back while data loads: `parla.locale` is ahead of the screen.
|
|
8
|
+
const [locale, setLocale] = createSignal(parla.locale);
|
|
9
|
+
onCleanup(parla.subscribe(() => {
|
|
10
|
+
setLocale(parla.locale);
|
|
11
|
+
}));
|
|
12
|
+
return { parla, locale };
|
|
13
|
+
});
|
|
14
|
+
return createComponent(ParlaContext, {
|
|
15
|
+
value: source,
|
|
16
|
+
get children() {
|
|
17
|
+
return props.children;
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
// Solid's own error for a missing provider names neither the context nor Parla.
|
|
22
|
+
export function useSource() {
|
|
23
|
+
const source = useContext(ParlaContext);
|
|
24
|
+
if (!source) {
|
|
25
|
+
throw new Error(parlaMessage('wrap your app in <ParlaProvider parla={…}>'));
|
|
26
|
+
}
|
|
27
|
+
return source;
|
|
28
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Locale } from '@parlajs/core';
|
|
2
|
+
import type { Accessor } from 'solid-js';
|
|
3
|
+
/** The locale on screen, the supported ones, and a switch that loads first. */
|
|
4
|
+
export declare function useLocale(): {
|
|
5
|
+
readonly locale: Accessor<Locale>;
|
|
6
|
+
readonly locales: Accessor<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;AAC5C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAIzC,+EAA+E;AAC/E,wBAAgB,SAAS,IAAI;IAC3B,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IAClC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC;IAClD,QAAQ,CAAC,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1D,CAOA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { useSource } from './provider.js';
|
|
2
|
+
/** The locale on screen, the supported ones, and a switch that loads first. */
|
|
3
|
+
export function useLocale() {
|
|
4
|
+
const source = useSource();
|
|
5
|
+
return {
|
|
6
|
+
locale: () => source().locale(),
|
|
7
|
+
locales: () => source().parla.locales,
|
|
8
|
+
switchLocale: async (locale) => source().parla.switchLocale(locale),
|
|
9
|
+
};
|
|
10
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Namespace } from '@parlajs/core';
|
|
2
|
+
import type { Accessor } from 'solid-js';
|
|
3
|
+
import type { Loaded } from './parla.ts';
|
|
4
|
+
/**
|
|
5
|
+
* Messages for the namespaces, keyed by namespace. Reading `t()` waits, in the nearest `<Loading>`,
|
|
6
|
+
* until they load for the current locale; loaded ones never wait, and neither does a locale switch.
|
|
7
|
+
*/
|
|
8
|
+
export declare function useParla<const L extends ReadonlyArray<Namespace>>(namespaces: L): {
|
|
9
|
+
readonly t: Accessor<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,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEzC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAQzC;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,KAAK,CAAC,CAAC,SAAS,aAAa,CAAC,SAAS,CAAC,EAC/D,UAAU,EAAE,CAAC,GACZ;IAAE,QAAQ,CAAC,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;CAAE,CAoBrC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createMemo, untrack } from 'solid-js';
|
|
2
|
+
import { useSource } from './provider.js';
|
|
3
|
+
async function settled(promise) {
|
|
4
|
+
await promise;
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Messages for the namespaces, keyed by namespace. Reading `t()` waits, in the nearest `<Loading>`,
|
|
9
|
+
* until they load for the current locale; loaded ones never wait, and neither does a locale switch.
|
|
10
|
+
*/
|
|
11
|
+
export function useParla(namespaces) {
|
|
12
|
+
const source = useSource();
|
|
13
|
+
// Before the memos: hydration runs those it adopts with a fake `Promise`, which core would cache.
|
|
14
|
+
untrack(() => {
|
|
15
|
+
const { parla, locale } = source();
|
|
16
|
+
void parla.load(namespaces, locale());
|
|
17
|
+
});
|
|
18
|
+
// Solid serializes what an async memo resolves to: a flag, as it cannot serialize `t`'s functions.
|
|
19
|
+
const ready = createMemo(() => {
|
|
20
|
+
const { parla, locale } = source();
|
|
21
|
+
const t = parla.loaded(namespaces, locale());
|
|
22
|
+
return t ? true : settled(parla.load(namespaces, locale()));
|
|
23
|
+
});
|
|
24
|
+
return {
|
|
25
|
+
t: createMemo(() => {
|
|
26
|
+
const { parla, locale } = source();
|
|
27
|
+
ready();
|
|
28
|
+
return parla.loaded(namespaces, locale()) ?? parla.load(namespaces, locale());
|
|
29
|
+
}),
|
|
30
|
+
};
|
|
31
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parlajs/solid",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Solid adapter for Parla.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"i18n",
|
|
7
|
+
"internationalization",
|
|
8
|
+
"l10n",
|
|
9
|
+
"localization",
|
|
10
|
+
"messageformat",
|
|
11
|
+
"parla",
|
|
12
|
+
"solid",
|
|
13
|
+
"solid-i18n",
|
|
14
|
+
"solidjs",
|
|
15
|
+
"type-safe",
|
|
16
|
+
"typescript"
|
|
17
|
+
],
|
|
18
|
+
"homepage": "https://github.com/parlajs/parla",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/parlajs/parla/issues"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "Marco Pasqualetti",
|
|
25
|
+
"url": "https://github.com/marcalexiei"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/parlajs/parla.git",
|
|
30
|
+
"directory": "packages/solid"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"build",
|
|
34
|
+
"src",
|
|
35
|
+
"README.md"
|
|
36
|
+
],
|
|
37
|
+
"type": "module",
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"@parlajs/source": "./src/index.ts",
|
|
41
|
+
"default": "./build/index.js"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@parlajs/core": "^0.0.0",
|
|
49
|
+
"@solidjs/vite-plugin": "3.0.1",
|
|
50
|
+
"@solidjs/web": "2.0.4",
|
|
51
|
+
"happy-dom": "21.0.4",
|
|
52
|
+
"solid-js": "2.0.4",
|
|
53
|
+
"vitest": "5.1.1"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@parlajs/core": "^0.0.0",
|
|
57
|
+
"solid-js": "^2.0.0"
|
|
58
|
+
},
|
|
59
|
+
"engines": {
|
|
60
|
+
"node": "^22.18 || ^24 || >=26"
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "tsc -p tsconfig.build.json",
|
|
64
|
+
"lint:publish": "attw --pack . --profile esm-only && publint --strict",
|
|
65
|
+
"test": "vitest",
|
|
66
|
+
"typecheck": "tsc -p tsconfig.typecheck.json"
|
|
67
|
+
}
|
|
6
68
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
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';
|
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 { Element } from 'solid-js';
|
|
11
|
+
|
|
12
|
+
/* Core's types with rich text as a Solid `Element`. */
|
|
13
|
+
export type Parla = CoreParla<Element>;
|
|
14
|
+
export type ParlaOptions = CoreParlaOptions<Element>;
|
|
15
|
+
export type Translations<NS extends Namespace> = CoreTranslations<NS, Element>;
|
|
16
|
+
export type Loaded<L extends ReadonlyArray<Namespace>> = CoreLoaded<L, Element>;
|
|
17
|
+
export type WrapRich = CoreWrapRich<Element>;
|
|
18
|
+
|
|
19
|
+
/** `createParla` from `@parlajs/core`, with rich text as Solid elements. */
|
|
20
|
+
// No `wrapRich`: core's array of parts is an `Element` already.
|
|
21
|
+
export const createParla: (options: ParlaOptions) => Parla = createCoreParla<Element>;
|
package/src/provider.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { parlaMessage } from '@parlajs/core';
|
|
2
|
+
import type { Locale } from '@parlajs/core';
|
|
3
|
+
import {
|
|
4
|
+
createComponent,
|
|
5
|
+
createContext,
|
|
6
|
+
createMemo,
|
|
7
|
+
createSignal,
|
|
8
|
+
onCleanup,
|
|
9
|
+
useContext,
|
|
10
|
+
} from 'solid-js';
|
|
11
|
+
import type { Accessor, Element } from 'solid-js';
|
|
12
|
+
|
|
13
|
+
import type { Parla } from './parla.ts';
|
|
14
|
+
|
|
15
|
+
export interface Source {
|
|
16
|
+
readonly parla: Parla;
|
|
17
|
+
readonly locale: Accessor<Locale>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const ParlaContext = createContext<Accessor<Source> | null>(null);
|
|
21
|
+
|
|
22
|
+
export function ParlaProvider(props: {
|
|
23
|
+
readonly parla: Parla;
|
|
24
|
+
readonly children?: Element;
|
|
25
|
+
}): Element {
|
|
26
|
+
const source = createMemo(() => {
|
|
27
|
+
const { parla } = props;
|
|
28
|
+
// A signal, as Solid holds a switch back while data loads: `parla.locale` is ahead of the screen.
|
|
29
|
+
const [locale, setLocale] = createSignal<Locale>(parla.locale);
|
|
30
|
+
onCleanup(
|
|
31
|
+
parla.subscribe(() => {
|
|
32
|
+
setLocale(parla.locale);
|
|
33
|
+
}),
|
|
34
|
+
);
|
|
35
|
+
return { parla, locale };
|
|
36
|
+
});
|
|
37
|
+
return createComponent(ParlaContext, {
|
|
38
|
+
value: source,
|
|
39
|
+
get children() {
|
|
40
|
+
return props.children;
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Solid's own error for a missing provider names neither the context nor Parla.
|
|
46
|
+
export function useSource(): Accessor<Source> {
|
|
47
|
+
const source = useContext(ParlaContext);
|
|
48
|
+
if (!source) {
|
|
49
|
+
throw new Error(parlaMessage('wrap your app in <ParlaProvider parla={…}>'));
|
|
50
|
+
}
|
|
51
|
+
return source;
|
|
52
|
+
}
|
package/src/useLocale.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Locale } from '@parlajs/core';
|
|
2
|
+
import type { Accessor } from 'solid-js';
|
|
3
|
+
|
|
4
|
+
import { useSource } from './provider.ts';
|
|
5
|
+
|
|
6
|
+
/** The locale on screen, the supported ones, and a switch that loads first. */
|
|
7
|
+
export function useLocale(): {
|
|
8
|
+
readonly locale: Accessor<Locale>;
|
|
9
|
+
readonly locales: Accessor<ReadonlyArray<Locale>>;
|
|
10
|
+
readonly switchLocale: (locale: Locale) => Promise<void>;
|
|
11
|
+
} {
|
|
12
|
+
const source = useSource();
|
|
13
|
+
return {
|
|
14
|
+
locale: () => source().locale(),
|
|
15
|
+
locales: () => source().parla.locales,
|
|
16
|
+
switchLocale: async (locale) => source().parla.switchLocale(locale),
|
|
17
|
+
};
|
|
18
|
+
}
|
package/src/useParla.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Namespace } from '@parlajs/core';
|
|
2
|
+
import { createMemo, untrack } from 'solid-js';
|
|
3
|
+
import type { Accessor } from 'solid-js';
|
|
4
|
+
|
|
5
|
+
import type { Loaded } from './parla.ts';
|
|
6
|
+
import { useSource } from './provider.ts';
|
|
7
|
+
|
|
8
|
+
async function settled(promise: Promise<unknown>): Promise<boolean> {
|
|
9
|
+
await promise;
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Messages for the namespaces, keyed by namespace. Reading `t()` waits, in the nearest `<Loading>`,
|
|
15
|
+
* until they load for the current locale; loaded ones never wait, and neither does a locale switch.
|
|
16
|
+
*/
|
|
17
|
+
export function useParla<const L extends ReadonlyArray<Namespace>>(
|
|
18
|
+
namespaces: L,
|
|
19
|
+
): { readonly t: Accessor<Loaded<L>> } {
|
|
20
|
+
const source = useSource();
|
|
21
|
+
// Before the memos: hydration runs those it adopts with a fake `Promise`, which core would cache.
|
|
22
|
+
untrack(() => {
|
|
23
|
+
const { parla, locale } = source();
|
|
24
|
+
void parla.load(namespaces, locale());
|
|
25
|
+
});
|
|
26
|
+
// Solid serializes what an async memo resolves to: a flag, as it cannot serialize `t`'s functions.
|
|
27
|
+
const ready = createMemo((): boolean | Promise<boolean> => {
|
|
28
|
+
const { parla, locale } = source();
|
|
29
|
+
const t = parla.loaded(namespaces, locale());
|
|
30
|
+
return t ? true : settled(parla.load(namespaces, locale()));
|
|
31
|
+
});
|
|
32
|
+
return {
|
|
33
|
+
t: createMemo((): Loaded<L> | Promise<Loaded<L>> => {
|
|
34
|
+
const { parla, locale } = source();
|
|
35
|
+
ready();
|
|
36
|
+
return parla.loaded(namespaces, locale()) ?? parla.load(namespaces, locale());
|
|
37
|
+
}),
|
|
38
|
+
};
|
|
39
|
+
}
|