@jag-k/fluent-hono 0.0.0-stage → 0.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # @jag-k/fluent-hono
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Initial version
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies:
12
+ - @jag-k/fluent@0.1.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jag_k
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,47 @@
1
- # Temporary Holding Version
1
+ # @jag-k/fluent-hono
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
+ [`hono/jsx`](https://hono.dev/docs/guides/jsx) binding for `@jag-k/fluent`, as `@fluent/react` is for React: elements inside a translated sentence, attributes onto an element, and switching the language in place.
4
+
5
+ Under development and not yet published: APIs may change.
6
+
7
+ ```sh
8
+ npm install @jag-k/fluent @jag-k/fluent-hono hono
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```tsx
14
+ import { createTranslator, L10n, L10nState } from "@jag-k/fluent";
15
+ import { createLocalized, L10nProvider } from "@jag-k/fluent-hono";
16
+ import { render } from "hono/jsx/dom";
17
+
18
+ export const state = new L10nState(new L10n({ locale: "en", sourceLocale: "en" }));
19
+ export const t = createTranslator(state);
20
+ export const Localized = createLocalized(state);
21
+
22
+ function Empty() {
23
+ return (
24
+ <p>
25
+ {/* the link's text comes from the translation */}
26
+ <Localized text="Nothing found. <link>Open the settings</link>" elems={{ link: <a href="/settings" /> }} />
27
+ {/* one child: the message's attributes go onto it, the value becomes its content */}
28
+ <Localized text={"Check\n.title = Look for new data"}>
29
+ <button type="button" />
30
+ </Localized>
31
+ </p>
32
+ );
33
+ }
34
+
35
+ render(<L10nProvider state={state} render={() => <Empty />} />, document.body);
36
+ // later: state.set(new L10n({ locale: "ru", ... })) re-renders in Russian
37
+ ```
38
+
39
+ - `createLocalized(source)`: a `Localized` component bound to an `L10n`, an `L10nState` or a getter. Props: `text` (FTL with named tags), `id`, `context`, `comment`, `elems` (an element per tag) and `args`. A tag the code doesn't give stays text.
40
+ - `L10nProvider({ state, render })`: re-renders its tree when the state switches the language. `useL10n()` inside it returns the current `L10n`.
41
+ - `renderParts(parts, elems)`: formatted parts as `hono/jsx` children, for your own components.
42
+
43
+ To have `l10n extract` find `Localized` texts, export it from your module under a name the config lists: `modules: { "./l10n": { t: ["t"], Localized: ["Localized"] } }`.
44
+
45
+ ## License
46
+
47
+ MIT
@@ -0,0 +1,46 @@
1
+ import type { ArgsFor, FluentVariable, L10nState, Literal, Part, VarsOf } from "@jag-k/fluent";
2
+ import { L10n } from "@jag-k/fluent";
3
+ import { type Child } from "hono/jsx/dom";
4
+ /**
5
+ * hono/jsx binding of `@jag-k/fluent` (as `@fluent/react` is React's).
6
+ *
7
+ * - `Localized`: named elements inside a sentence
8
+ * (`text="Nothing found. <link>Open the connection</link>"`, `elems={{ link: <a href={…} /> }}`):
9
+ * the text inside comes from the translation, a tag the code doesn't give
10
+ * stays text. With one child element, the message's attributes
11
+ * (`.title`, `.aria-label`, `.placeholder`) go onto it and the value
12
+ * becomes its content.
13
+ * - `L10nProvider`: re-renders its tree when the state switches the language
14
+ * (no reload); `t()` called during render reads the new texts.
15
+ */
16
+ type ArgsProp<V extends string> = ArgsFor<V> extends [args: infer A] ? {
17
+ args: A;
18
+ } : {
19
+ args?: Record<string, FluentVariable> | undefined;
20
+ };
21
+ export type LocalizedProps<S extends string> = {
22
+ /** The text in the source language (FTL with named tags). */
23
+ text: Literal<S>;
24
+ /** Explicit id; generated from the text + context when absent. */
25
+ id?: string | undefined;
26
+ context?: string | undefined;
27
+ /** The translator's comment; only `l10n extract` reads it. */
28
+ comment?: string | undefined;
29
+ /** An element per tag name in the text. */
30
+ elems?: Record<string, Child> | undefined;
31
+ children?: Child;
32
+ } & ArgsProp<VarsOf<S>>;
33
+ type Source = L10n | L10nState | (() => L10n);
34
+ /** `Localized` bound to a language source (the app exports it under the name its config gives the extractor). */
35
+ export declare function createLocalized(source: Source): <const S extends string>(props: LocalizedProps<S>) => import("hono/jsx/dom/jsx-dev-runtime").JSX.Element;
36
+ /** Parts as hono/jsx children: each element cloned with its parts inside (or its own children when the message gives none). */
37
+ export declare function renderParts(parts: Part[], elems: Record<string, Child>): Child[];
38
+ /** Re-renders the tree when `state` switches the language, and gives `useL10n` the current one. */
39
+ export declare function L10nProvider({ state, render }: {
40
+ state: L10nState;
41
+ render: () => Child;
42
+ }): import("hono/jsx/dom/jsx-dev-runtime").JSX.Element;
43
+ /** The current language inside `L10nProvider` (a component that reads it re-renders on a switch). */
44
+ export declare const useL10n: () => L10n | undefined;
45
+ export {};
46
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0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AAc,CAAC,CAAC,CAAC,wDAQ3E;AAED,+HAA+H;AAC/H,wBAAgB,WAAW,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,GAAG,KAAK,EAAE,CAOhF;AAID,mGAAmG;AACnG,wBAAgB,YAAY,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;IAAE,KAAK,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,KAAK,CAAA;CAAE,sDAIxF;AAED,qGAAqG;AACrG,eAAO,MAAM,OAAO,QAAO,IAAI,GAAG,SAAoC,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ import { Fragment as _Fragment, jsx as _jsx } from "hono/jsx/dom/jsx-runtime";
2
+ import { L10n, toMessage } from "@jag-k/fluent";
3
+ import { cloneElement, createContext, isValidElement, useContext, useEffect, useState } from "hono/jsx/dom";
4
+ const resolve = (source) => (typeof source === "function" ? source : source instanceof L10n ? () => source : () => source.current);
5
+ /** `Localized` bound to a language source (the app exports it under the name its config gives the extractor). */
6
+ export function createLocalized(source) {
7
+ const current = resolve(source);
8
+ return function Localized(props) {
9
+ const { text, id, context, elems = {}, children } = props;
10
+ const message = toMessage(id === undefined ? { msg: text, context } : { id, msg: text, context });
11
+ const { value, attributes } = current().formatToParts(message, props.args, Object.keys(elems));
12
+ const content = renderParts(value, elems);
13
+ const only = Array.isArray(children) ? (children.length === 1 ? children[0] : undefined) : children;
14
+ return isValidElement(only) ? _jsx(_Fragment, { children: cloneElement(only, attributes, ...content) }) : _jsx(_Fragment, { children: content });
15
+ };
16
+ }
17
+ /** Parts as hono/jsx children: each element cloned with its parts inside (or its own children when the message gives none). */
18
+ export function renderParts(parts, elems) {
19
+ return parts.map((part) => {
20
+ if (typeof part === "string")
21
+ return part;
22
+ const element = elems[part.element];
23
+ if (!isValidElement(element))
24
+ return "";
25
+ return part.children.length ? cloneElement(element, {}, ...renderParts(part.children, elems)) : element;
26
+ });
27
+ }
28
+ const L10nContext = createContext(undefined);
29
+ /** Re-renders the tree when `state` switches the language, and gives `useL10n` the current one. */
30
+ export function L10nProvider({ state, render }) {
31
+ const [, setVersion] = useState(0);
32
+ useEffect(() => state.subscribe(() => setVersion((version) => version + 1)), [state]);
33
+ return _jsx(L10nContext.Provider, { value: state.current, children: render() });
34
+ }
35
+ /** The current language inside `L10nProvider` (a component that reads it re-renders on a switch). */
36
+ export const useL10n = () => useContext(L10nContext);
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/index.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAc,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AA+BxH,MAAM,OAAO,GAAG,CAAC,MAAc,EAAgB,EAAE,CAAC,CAAC,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,YAAY,IAAI,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAEzJ,iHAAiH;AACjH,MAAM,UAAU,eAAe,CAAC,MAAc;IAC5C,MAAM,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAChC,OAAO,SAAS,SAAS,CAAyB,KAAwB;QACxE,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,KAAK,GAAG,EAAE,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC;QAC1D,MAAM,OAAO,GAAG,SAAS,CAAC,EAAE,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,EA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AACL,CAAC;AAED,MAAM,WAAW,GAAG,aAAa,CAAmB,SAAS,CAAC,CAAC;AAE/D,mGAAmG;AACnG,MAAM,UAAU,YAAY,CAAC,EAAE,KAAK,EAAE,MAAM,EAA6C;IACvF,MAAM,CAAC,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IACnC,SAAS,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IACtF,OAAO,KAAC,WAAW,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,CAAC,OAAO,YAAG,MAAM,EAAE,GAAwB,CAAC;AACvF,CAAC;AAED,qGAAqG;AACrG,MAAM,CAAC,MAAM,OAAO,GAAG,GAAqB,EAAE,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "@jag-k/fluent-hono",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "hono/jsx binding of @jag-k/fluent: Localized (elements inside a translated sentence, attributes onto an element) and a provider that switches the language in place.",
5
+ "keywords": [
6
+ "fluent",
7
+ "hono",
8
+ "i18n",
9
+ "jsx",
10
+ "l10n"
11
+ ],
12
+ "homepage": "https://github.com/jag-k/fluent/tree/main/packages/fluent-hono#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/jag-k/fluent/issues"
15
+ },
16
+ "license": "MIT",
17
+ "author": "Jag_k <me@jagk.dev>",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/jag-k/fluent.git",
21
+ "directory": "packages/fluent-hono"
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "src",
26
+ "skills",
27
+ "README.md",
28
+ "CHANGELOG.md",
29
+ "LICENSE"
30
+ ],
31
+ "type": "module",
32
+ "sideEffects": false,
33
+ "exports": {
34
+ ".": {
35
+ "@jag-k/source": "./src/index.tsx",
36
+ "types": "./dist/index.d.ts",
37
+ "import": "./dist/index.js"
38
+ },
39
+ "./package.json": "./package.json"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public",
43
+ "registry": "https://registry.npmjs.org"
44
+ },
45
+ "dependencies": {
46
+ "@jag-k/fluent": "0.1.0"
47
+ },
48
+ "peerDependencies": {
49
+ "hono": "^4"
50
+ },
51
+ "engines": {
52
+ "node": ">=22.18.0"
53
+ },
54
+ "scripts": {
55
+ "build": "tsc -p tsconfig.json"
56
+ }
6
57
  }
@@ -0,0 +1,54 @@
1
+ ---
2
+ name: fluent-hono
3
+ description: Localize hono/jsx UI with @jag-k/fluent-hono (Localized with elements inside a translated sentence, attributes onto an element, L10nProvider and useL10n for switching the language in place). Use when writing or changing hono/jsx components that show translated text with markup, links, keyboard keys or translated title/aria-label/placeholder attributes.
4
+ license: MIT
5
+ ---
6
+
7
+ # hono/jsx with @jag-k/fluent-hono
8
+
9
+ Plain texts go through `t()` (see the `fluent-texts` skill from
10
+ `@jag-k/fluent`). `Localized` is for a sentence with elements inside, and for
11
+ an element whose attributes are translated. Import `Localized` from the
12
+ project's own l10n module (created with `createLocalized(state)`), the name its
13
+ `l10n.config` lists under `modules` so `l10n extract` finds the texts.
14
+
15
+ ## Elements inside a sentence
16
+
17
+ ```tsx
18
+ <Localized text="Nothing found. <link>Open the settings</link>" elems={{ link: <a href="/settings" /> }} />
19
+ <Localized text="Press <key>Esc</key> to close" elems={{ key: <kbd /> }} />
20
+ <Localized text="Hello, <b>{ $name }</b>" args={{ name }} elems={{ b: <strong /> }} />
21
+ ```
22
+
23
+ - The text between the tags comes from the translation; the element (its
24
+ type, `href`, classes) comes from `elems`. A translation may move the tags.
25
+ - Tags are names, not HTML: `<link>`, `<key>`. A tag the code doesn't give in
26
+ `elems` is shown as text (and `l10n check` warns).
27
+ - Never split a sentence into several `t()` calls around an element.
28
+
29
+ ## Attributes onto one child
30
+
31
+ ```tsx
32
+ <Localized text={"Check\n.title = Look for new data"}>
33
+ <button type="button" class="small" />
34
+ </Localized>
35
+ ```
36
+
37
+ With exactly one child element, the message's attributes (`.title`,
38
+ `.aria-label`, `.placeholder`) go onto it and the value becomes its content.
39
+
40
+ ## Props
41
+
42
+ `text` (a literal in the source language), `id` and `context` (as in `t`),
43
+ `comment` (for the translator), `elems`, `args`. `text`, `id`, `context` and
44
+ `comment` must be literals.
45
+
46
+ ## Switching the language
47
+
48
+ ```tsx
49
+ render(<L10nProvider state={state} render={() => <App />} />, root);
50
+ state.set(newL10n); // re-renders in place; t() and Localized read the new language
51
+ ```
52
+
53
+ `useL10n()` inside the provider returns the current `L10n`. Create the
54
+ translator and `Localized` over the same `L10nState`.
package/src/index.tsx ADDED
@@ -0,0 +1,69 @@
1
+ import type { Args, ArgsFor, FluentVariable, L10nState, Literal, Part, VarsOf } from "@jag-k/fluent";
2
+ import { L10n, toMessage } from "@jag-k/fluent";
3
+ import { type Child, cloneElement, createContext, isValidElement, useContext, useEffect, useState } from "hono/jsx/dom";
4
+
5
+ /**
6
+ * hono/jsx binding of `@jag-k/fluent` (as `@fluent/react` is React's).
7
+ *
8
+ * - `Localized`: named elements inside a sentence
9
+ * (`text="Nothing found. <link>Open the connection</link>"`, `elems={{ link: <a href={…} /> }}`):
10
+ * the text inside comes from the translation, a tag the code doesn't give
11
+ * stays text. With one child element, the message's attributes
12
+ * (`.title`, `.aria-label`, `.placeholder`) go onto it and the value
13
+ * becomes its content.
14
+ * - `L10nProvider`: re-renders its tree when the state switches the language
15
+ * (no reload); `t()` called during render reads the new texts.
16
+ */
17
+ type ArgsProp<V extends string> = ArgsFor<V> extends [args: infer A] ? { args: A } : { args?: Record<string, FluentVariable> | undefined };
18
+
19
+ export type LocalizedProps<S extends string> = {
20
+ /** The text in the source language (FTL with named tags). */
21
+ text: Literal<S>;
22
+ /** Explicit id; generated from the text + context when absent. */
23
+ id?: string | undefined;
24
+ context?: string | undefined;
25
+ /** The translator's comment; only `l10n extract` reads it. */
26
+ comment?: string | undefined;
27
+ /** An element per tag name in the text. */
28
+ elems?: Record<string, Child> | undefined;
29
+ children?: Child;
30
+ } & ArgsProp<VarsOf<S>>;
31
+
32
+ type Source = L10n | L10nState | (() => L10n);
33
+
34
+ const resolve = (source: Source): (() => L10n) => (typeof source === "function" ? source : source instanceof L10n ? () => source : () => source.current);
35
+
36
+ /** `Localized` bound to a language source (the app exports it under the name its config gives the extractor). */
37
+ export function createLocalized(source: Source) {
38
+ const current = resolve(source);
39
+ return function Localized<const S extends string>(props: LocalizedProps<S>) {
40
+ const { text, id, context, elems = {}, children } = props;
41
+ const message = toMessage(id === undefined ? { msg: text, context } : { id, msg: text, context });
42
+ const { value, attributes } = current().formatToParts(message, props.args as Args | undefined, Object.keys(elems));
43
+ const content = renderParts(value, elems);
44
+ const only = Array.isArray(children) ? (children.length === 1 ? children[0] : undefined) : children;
45
+ return isValidElement(only) ? <>{cloneElement(only, attributes, ...content)}</> : <>{content}</>;
46
+ };
47
+ }
48
+
49
+ /** Parts as hono/jsx children: each element cloned with its parts inside (or its own children when the message gives none). */
50
+ export function renderParts(parts: Part[], elems: Record<string, Child>): Child[] {
51
+ return parts.map((part) => {
52
+ if (typeof part === "string") return part;
53
+ const element = elems[part.element];
54
+ if (!isValidElement(element)) return "";
55
+ return part.children.length ? cloneElement(element, {}, ...renderParts(part.children, elems)) : element;
56
+ });
57
+ }
58
+
59
+ const L10nContext = createContext<L10n | undefined>(undefined);
60
+
61
+ /** Re-renders the tree when `state` switches the language, and gives `useL10n` the current one. */
62
+ export function L10nProvider({ state, render }: { state: L10nState; render: () => Child }) {
63
+ const [, setVersion] = useState(0);
64
+ useEffect(() => state.subscribe(() => setVersion((version) => version + 1)), [state]);
65
+ return <L10nContext.Provider value={state.current}>{render()}</L10nContext.Provider>;
66
+ }
67
+
68
+ /** The current language inside `L10nProvider` (a component that reads it re-renders on a switch). */
69
+ export const useL10n = (): L10n | undefined => useContext(L10nContext);