@hyperscript-tools/i18n 2.4.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 +20 -0
- package/README.md +104 -0
- package/dist/chunk-XIEA5H7Z.js +37 -0
- package/dist/cli.d.ts +15 -0
- package/dist/cli.js +130 -0
- package/dist/eleventy.d.ts +44 -0
- package/dist/eleventy.js +57 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +15 -0
- package/package.json +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 LokaScript Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
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, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# @hyperscript-tools/i18n
|
|
2
|
+
|
|
3
|
+
Build-time translation for [\_hyperscript](https://hyperscript.org) code
|
|
4
|
+
samples. Ship a single canonical English source; generate `_="..."`
|
|
5
|
+
attributes in any of 24 languages at build time.
|
|
6
|
+
|
|
7
|
+
Pairs with [`@hyperscript-tools/multilingual`](https://www.npmjs.com/package/@hyperscript-tools/multilingual)
|
|
8
|
+
(runtime translation), so a docs site can choose between _served
|
|
9
|
+
pre-translated_ and _translate on the fly_.
|
|
10
|
+
|
|
11
|
+
## CLI
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npx @hyperscript-tools/i18n translate src/page.html --langs ja,es,ko --out dist/
|
|
15
|
+
# Wrote 3 files (1 input × 3 langs) to dist/
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Produces `dist/page.ja.html`, `dist/page.es.html`, `dist/page.ko.html` with
|
|
19
|
+
every `_="..."` attribute translated to the target language. The original
|
|
20
|
+
HTML structure is preserved.
|
|
21
|
+
|
|
22
|
+
### Flags
|
|
23
|
+
|
|
24
|
+
| Flag | Description |
|
|
25
|
+
| --------------- | ---------------------------------------------------------------------------------- |
|
|
26
|
+
| `--langs`, `-l` | Comma-separated target language codes (`ja,es,ko`). |
|
|
27
|
+
| `--out`, `-o` | Output directory. Defaults to `.`. |
|
|
28
|
+
| `--from`, `-f` | Source locale of the input. Defaults to `en`. |
|
|
29
|
+
| `--strict` | Fail the run on any single attribute that can't be translated. Default is lenient. |
|
|
30
|
+
|
|
31
|
+
Inputs may be individual files or directories — every `.html` in a directory
|
|
32
|
+
is processed.
|
|
33
|
+
|
|
34
|
+
## Eleventy plugin
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
// eleventy.config.js
|
|
38
|
+
import hyperscriptI18n from '@hyperscript-tools/i18n/eleventy';
|
|
39
|
+
|
|
40
|
+
export default function (eleventyConfig) {
|
|
41
|
+
eleventyConfig.addPlugin(hyperscriptI18n);
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Adds three filters:
|
|
46
|
+
|
|
47
|
+
```njk
|
|
48
|
+
{# Translate a single hyperscript snippet #}
|
|
49
|
+
{{ "toggle .active" | translateHs("ja") }}
|
|
50
|
+
{# → ".active を 切り替え" #}
|
|
51
|
+
|
|
52
|
+
{# Get every translation at once #}
|
|
53
|
+
{% set variants = "toggle .active" | translateHsAll(["ja","es","ko"]) %}
|
|
54
|
+
{% for lang, code in variants %}
|
|
55
|
+
<code data-hyperscript-lang="{{ lang }}">{{ code }}</code>
|
|
56
|
+
{% endfor %}
|
|
57
|
+
|
|
58
|
+
{# Rewrite every _="..." attribute in an HTML fragment #}
|
|
59
|
+
{{ patternHtml | translateHsHtml("ja") | safe }}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Filter names can be overridden:
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
eleventyConfig.addPlugin(hyperscriptI18n, {
|
|
66
|
+
filterNames: { snippet: 'toLang', html: 'toLangHtml' },
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Programmatic API
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { translate, translateHtml, translateHtmlToManyLangs } from '@hyperscript-tools/i18n';
|
|
74
|
+
|
|
75
|
+
// Snippet → snippet
|
|
76
|
+
translate('toggle .active', 'en', 'ja');
|
|
77
|
+
// → '.active を 切り替え'
|
|
78
|
+
|
|
79
|
+
// HTML → HTML (rewrites every _="..." attribute)
|
|
80
|
+
const ja = translateHtml(html, 'ja');
|
|
81
|
+
|
|
82
|
+
// HTML → Map of HTML
|
|
83
|
+
const variants = translateHtmlToManyLangs(html, ['ja', 'es', 'ko']);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Supported languages
|
|
87
|
+
|
|
88
|
+
All 24 languages supported by `@lokascript/i18n`:
|
|
89
|
+
`ar, bn, de, en, es, fr, he, hi, id, it, ja, ko, ms, pl, pt, qu, ru, sw, th, tl, tr, uk, vi, zh`.
|
|
90
|
+
|
|
91
|
+
Word order is handled automatically: SVO (Spanish, Chinese), SOV (Japanese,
|
|
92
|
+
Korean, Turkish), VSO (Arabic). RTL languages preserve direction.
|
|
93
|
+
|
|
94
|
+
## When to use this vs. `@hyperscript-tools/multilingual`
|
|
95
|
+
|
|
96
|
+
| Need | Reach for |
|
|
97
|
+
| ---------------------------------------------------------------- | --------------------------------------------- |
|
|
98
|
+
| Visitors type hyperscript in their native language at runtime | `@hyperscript-tools/multilingual` |
|
|
99
|
+
| Author once in English; ship pre-translated docs in 24 languages | `@hyperscript-tools/i18n` (this) |
|
|
100
|
+
| Both | Use both. They share no runtime dependencies. |
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
MIT.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/html.ts
|
|
2
|
+
import { translate } from "@lokascript/i18n";
|
|
3
|
+
var ATTR_PATTERNS = [
|
|
4
|
+
/(_\s*=\s*")([^"]+)(")/g,
|
|
5
|
+
/(_\s*=\s*')([^']+)(')/g,
|
|
6
|
+
/(_\s*=\s*`)([^`]+)(`)/g
|
|
7
|
+
];
|
|
8
|
+
function translateHtml(html, to, options = {}) {
|
|
9
|
+
const from = options.from ?? "en";
|
|
10
|
+
const lenient = options.lenient ?? true;
|
|
11
|
+
if (from === to) return html;
|
|
12
|
+
let out = html;
|
|
13
|
+
for (const pattern of ATTR_PATTERNS) {
|
|
14
|
+
out = out.replace(pattern, (_match, before, body, after) => {
|
|
15
|
+
try {
|
|
16
|
+
const translated = translate(body, from, to);
|
|
17
|
+
return `${before}${translated}${after}`;
|
|
18
|
+
} catch (err) {
|
|
19
|
+
if (lenient) return `${before}${body}${after}`;
|
|
20
|
+
throw err;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
function translateHtmlToManyLangs(html, langs, options = {}) {
|
|
27
|
+
const out = /* @__PURE__ */ new Map();
|
|
28
|
+
for (const lang of langs) {
|
|
29
|
+
out.set(lang, translateHtml(html, lang, options));
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export {
|
|
35
|
+
translateHtml,
|
|
36
|
+
translateHtmlToManyLangs
|
|
37
|
+
};
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* CLI: hyperscript-i18n
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* hyperscript-i18n translate <input.html> --langs ja,es,ko --out dist/
|
|
7
|
+
* hyperscript-i18n translate "src/**\/*.html" --langs ja,es,ko --out dist/ --from en
|
|
8
|
+
*
|
|
9
|
+
* For each input file and each requested target language, emits a sibling
|
|
10
|
+
* file in --out named `<basename>.<lang>.html` containing the translated
|
|
11
|
+
* `_=` attributes. The original English remains the canonical source.
|
|
12
|
+
*/
|
|
13
|
+
declare function run(argv: string[]): Promise<number>;
|
|
14
|
+
|
|
15
|
+
export { run };
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
translateHtml
|
|
4
|
+
} from "./chunk-XIEA5H7Z.js";
|
|
5
|
+
|
|
6
|
+
// src/cli.ts
|
|
7
|
+
import { mkdirSync, readFileSync, writeFileSync, existsSync, statSync, readdirSync } from "fs";
|
|
8
|
+
import { dirname, join, basename, extname, resolve } from "path";
|
|
9
|
+
function parseArgs(argv) {
|
|
10
|
+
const args = {
|
|
11
|
+
command: "help",
|
|
12
|
+
inputs: [],
|
|
13
|
+
langs: [],
|
|
14
|
+
from: "en",
|
|
15
|
+
out: ".",
|
|
16
|
+
strict: false
|
|
17
|
+
};
|
|
18
|
+
if (argv.length === 0 || argv[0] === "--help" || argv[0] === "-h") return args;
|
|
19
|
+
args.command = argv[0] === "translate" ? "translate" : "help";
|
|
20
|
+
if (args.command === "help") return args;
|
|
21
|
+
for (let i = 1; i < argv.length; i++) {
|
|
22
|
+
const a = argv[i];
|
|
23
|
+
if (a === "--langs" || a === "-l") {
|
|
24
|
+
args.langs = (argv[++i] ?? "").split(",").map((s) => s.trim()).filter(Boolean);
|
|
25
|
+
} else if (a === "--out" || a === "-o") {
|
|
26
|
+
args.out = argv[++i] ?? ".";
|
|
27
|
+
} else if (a === "--from" || a === "-f") {
|
|
28
|
+
args.from = argv[++i] ?? "en";
|
|
29
|
+
} else if (a === "--strict") {
|
|
30
|
+
args.strict = true;
|
|
31
|
+
} else if (a.startsWith("-")) {
|
|
32
|
+
throw new Error(`Unknown flag: ${a}`);
|
|
33
|
+
} else {
|
|
34
|
+
args.inputs.push(a);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return args;
|
|
38
|
+
}
|
|
39
|
+
function expandInputs(patterns) {
|
|
40
|
+
const out = [];
|
|
41
|
+
for (const pattern of patterns) {
|
|
42
|
+
const abs = resolve(pattern);
|
|
43
|
+
if (!existsSync(abs)) {
|
|
44
|
+
console.warn(`Skipping missing input: ${pattern}`);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const stat = statSync(abs);
|
|
48
|
+
if (stat.isDirectory()) {
|
|
49
|
+
for (const name of readdirSync(abs)) {
|
|
50
|
+
if (name.endsWith(".html")) out.push(join(abs, name));
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
out.push(abs);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
function printHelp() {
|
|
59
|
+
console.log(`hyperscript-i18n \u2014 translate _="..." attributes in HTML
|
|
60
|
+
|
|
61
|
+
Usage:
|
|
62
|
+
hyperscript-i18n translate <input> [<input> ...] --langs <codes> --out <dir> [--from <code>] [--strict]
|
|
63
|
+
|
|
64
|
+
Options:
|
|
65
|
+
--langs, -l Comma-separated target language codes (e.g. ja,es,ko).
|
|
66
|
+
--out, -o Output directory. Defaults to the current directory.
|
|
67
|
+
--from, -f Source locale of the input. Defaults to 'en'.
|
|
68
|
+
--strict Fail the run if any single attribute fails to translate.
|
|
69
|
+
Default is lenient: untranslatable snippets are left in place.
|
|
70
|
+
|
|
71
|
+
Output:
|
|
72
|
+
For each input file foo.html and each lang, writes <out>/foo.<lang>.html.
|
|
73
|
+
|
|
74
|
+
Example:
|
|
75
|
+
hyperscript-i18n translate src/patterns.html --langs ja,es,ko --out dist/
|
|
76
|
+
`);
|
|
77
|
+
}
|
|
78
|
+
async function run(argv) {
|
|
79
|
+
let args;
|
|
80
|
+
try {
|
|
81
|
+
args = parseArgs(argv);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
console.error(err.message);
|
|
84
|
+
return 2;
|
|
85
|
+
}
|
|
86
|
+
if (args.command === "help") {
|
|
87
|
+
printHelp();
|
|
88
|
+
return 0;
|
|
89
|
+
}
|
|
90
|
+
if (args.langs.length === 0) {
|
|
91
|
+
console.error("No --langs provided.");
|
|
92
|
+
return 2;
|
|
93
|
+
}
|
|
94
|
+
if (args.inputs.length === 0) {
|
|
95
|
+
console.error("No input files provided.");
|
|
96
|
+
return 2;
|
|
97
|
+
}
|
|
98
|
+
const files = expandInputs(args.inputs);
|
|
99
|
+
if (files.length === 0) {
|
|
100
|
+
console.error("No HTML inputs matched.");
|
|
101
|
+
return 1;
|
|
102
|
+
}
|
|
103
|
+
mkdirSync(args.out, { recursive: true });
|
|
104
|
+
let written = 0;
|
|
105
|
+
for (const file of files) {
|
|
106
|
+
const html = readFileSync(file, "utf8");
|
|
107
|
+
const stem = basename(file, extname(file));
|
|
108
|
+
for (const lang of args.langs) {
|
|
109
|
+
const translated = translateHtml(html, lang, {
|
|
110
|
+
from: args.from,
|
|
111
|
+
lenient: !args.strict
|
|
112
|
+
});
|
|
113
|
+
const outPath = join(args.out, `${stem}.${lang}.html`);
|
|
114
|
+
mkdirSync(dirname(outPath), { recursive: true });
|
|
115
|
+
writeFileSync(outPath, translated, "utf8");
|
|
116
|
+
written++;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
console.log(
|
|
120
|
+
`Wrote ${written} files (${files.length} input \xD7 ${args.langs.length} langs) to ${args.out}`
|
|
121
|
+
);
|
|
122
|
+
return 0;
|
|
123
|
+
}
|
|
124
|
+
var isMain = import.meta.url === `file://${process.argv[1]}`;
|
|
125
|
+
if (isMain) {
|
|
126
|
+
run(process.argv.slice(2)).then((code) => process.exit(code));
|
|
127
|
+
}
|
|
128
|
+
export {
|
|
129
|
+
run
|
|
130
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { LangCode } from './index.js';
|
|
2
|
+
export { TranslateHtmlOptions, translateHtml, translateHtmlToManyLangs } from './index.js';
|
|
3
|
+
import '@lokascript/i18n';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Eleventy plugin entry: `@hyperscript-tools/i18n/eleventy`
|
|
7
|
+
*
|
|
8
|
+
* Adds:
|
|
9
|
+
* - `translateHs` filter: translate a hyperscript snippet to a target lang.
|
|
10
|
+
* {{ snippet | translateHs("ja") }}
|
|
11
|
+
* {{ snippet | translateHs("ja", "en") }} // explicit source
|
|
12
|
+
*
|
|
13
|
+
* - `translateHsAll` filter: produce { lang: translated } for an array of
|
|
14
|
+
* target langs. Useful for emitting per-language code blocks side by side.
|
|
15
|
+
* {% set variants = snippet | translateHsAll(["ja","es","ko"]) %}
|
|
16
|
+
*
|
|
17
|
+
* - `translateHsHtml` filter: rewrite every `_="..."` attribute in an HTML
|
|
18
|
+
* fragment. Useful for `{{ patternHtml | translateHsHtml("ja") | safe }}`.
|
|
19
|
+
*
|
|
20
|
+
* Usage:
|
|
21
|
+
* import hyperscriptI18n from '@hyperscript-tools/i18n/eleventy';
|
|
22
|
+
* export default function (eleventyConfig) {
|
|
23
|
+
* eleventyConfig.addPlugin(hyperscriptI18n);
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
interface EleventyPluginOptions {
|
|
28
|
+
/** Default source locale for filters. Defaults to 'en'. */
|
|
29
|
+
defaultFrom?: LangCode;
|
|
30
|
+
/** Default lenient flag for HTML translation. Defaults to true. */
|
|
31
|
+
lenient?: boolean;
|
|
32
|
+
/** Override filter names (in case of collisions in your config). */
|
|
33
|
+
filterNames?: {
|
|
34
|
+
snippet?: string;
|
|
35
|
+
snippetMany?: string;
|
|
36
|
+
html?: string;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
interface EleventyConfig {
|
|
40
|
+
addFilter(name: string, fn: (...args: unknown[]) => unknown): void;
|
|
41
|
+
}
|
|
42
|
+
declare function hyperscriptI18nPlugin(eleventyConfig: EleventyConfig, options?: EleventyPluginOptions): void;
|
|
43
|
+
|
|
44
|
+
export { type EleventyPluginOptions, LangCode, hyperscriptI18nPlugin as default };
|
package/dist/eleventy.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
translateHtml,
|
|
3
|
+
translateHtmlToManyLangs
|
|
4
|
+
} from "./chunk-XIEA5H7Z.js";
|
|
5
|
+
|
|
6
|
+
// src/eleventy.ts
|
|
7
|
+
import { translate } from "@lokascript/i18n";
|
|
8
|
+
function hyperscriptI18nPlugin(eleventyConfig, options = {}) {
|
|
9
|
+
const defaultFrom = options.defaultFrom ?? "en";
|
|
10
|
+
const lenient = options.lenient ?? true;
|
|
11
|
+
const names = {
|
|
12
|
+
snippet: options.filterNames?.snippet ?? "translateHs",
|
|
13
|
+
snippetMany: options.filterNames?.snippetMany ?? "translateHsAll",
|
|
14
|
+
html: options.filterNames?.html ?? "translateHsHtml"
|
|
15
|
+
};
|
|
16
|
+
eleventyConfig.addFilter(names.snippet, (input, to, from) => {
|
|
17
|
+
if (typeof input !== "string" || typeof to !== "string") return input;
|
|
18
|
+
const source = typeof from === "string" ? from : defaultFrom;
|
|
19
|
+
if (source === to) return input;
|
|
20
|
+
try {
|
|
21
|
+
return translate(input, source, to);
|
|
22
|
+
} catch {
|
|
23
|
+
return lenient ? input : "";
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
eleventyConfig.addFilter(names.snippetMany, (input, langs, from) => {
|
|
27
|
+
if (typeof input !== "string" || !Array.isArray(langs)) return {};
|
|
28
|
+
const source = typeof from === "string" ? from : defaultFrom;
|
|
29
|
+
const out = {};
|
|
30
|
+
for (const lang of langs) {
|
|
31
|
+
if (typeof lang !== "string") continue;
|
|
32
|
+
if (lang === source) {
|
|
33
|
+
out[lang] = input;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
out[lang] = translate(input, source, lang);
|
|
38
|
+
} catch {
|
|
39
|
+
out[lang] = lenient ? input : "";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
});
|
|
44
|
+
eleventyConfig.addFilter(names.html, (html, to, from) => {
|
|
45
|
+
if (typeof html !== "string" || typeof to !== "string") return html;
|
|
46
|
+
const opts = {
|
|
47
|
+
from: typeof from === "string" ? from : defaultFrom,
|
|
48
|
+
lenient
|
|
49
|
+
};
|
|
50
|
+
return translateHtml(html, to, opts);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
hyperscriptI18nPlugin as default,
|
|
55
|
+
translateHtml,
|
|
56
|
+
translateHtmlToManyLangs
|
|
57
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export { GrammarTransformer, toEnglish, toLocale, translate } from '@lokascript/i18n';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* HTML attribute scanner + translator.
|
|
5
|
+
*
|
|
6
|
+
* Walks an HTML string, finds `_="..."` (and single-quoted / backtick variants)
|
|
7
|
+
* attributes, translates each from a source locale to a target locale, and
|
|
8
|
+
* returns the rewritten HTML. The grammar transformer doesn't know about HTML;
|
|
9
|
+
* this module is the glue.
|
|
10
|
+
*/
|
|
11
|
+
type LangCode = string;
|
|
12
|
+
interface TranslateHtmlOptions {
|
|
13
|
+
/** Source locale of the input HTML's `_=` attributes. Defaults to 'en'. */
|
|
14
|
+
from?: LangCode;
|
|
15
|
+
/**
|
|
16
|
+
* If true, swallow per-attribute translation errors and leave the original
|
|
17
|
+
* text in place. Defaults to true so a single bad sample doesn't fail the
|
|
18
|
+
* whole page build.
|
|
19
|
+
*/
|
|
20
|
+
lenient?: boolean;
|
|
21
|
+
}
|
|
22
|
+
declare function translateHtml(html: string, to: LangCode, options?: TranslateHtmlOptions): string;
|
|
23
|
+
declare function translateHtmlToManyLangs(html: string, langs: ReadonlyArray<LangCode>, options?: TranslateHtmlOptions): Map<LangCode, string>;
|
|
24
|
+
|
|
25
|
+
export { type LangCode, type TranslateHtmlOptions, translateHtml, translateHtmlToManyLangs };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
translateHtml,
|
|
3
|
+
translateHtmlToManyLangs
|
|
4
|
+
} from "./chunk-XIEA5H7Z.js";
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
import { translate, toLocale, toEnglish, GrammarTransformer } from "@lokascript/i18n";
|
|
8
|
+
export {
|
|
9
|
+
GrammarTransformer,
|
|
10
|
+
toEnglish,
|
|
11
|
+
toLocale,
|
|
12
|
+
translate,
|
|
13
|
+
translateHtml,
|
|
14
|
+
translateHtmlToManyLangs
|
|
15
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hyperscript-tools/i18n",
|
|
3
|
+
"version": "2.4.0",
|
|
4
|
+
"description": "Build-time translation for hyperscript code samples — generate per-language HTML from a canonical English source",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"hyperscript-i18n": "dist/cli.js"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./eleventy": {
|
|
17
|
+
"types": "./dist/eleventy.d.ts",
|
|
18
|
+
"import": "./dist/eleventy.js"
|
|
19
|
+
},
|
|
20
|
+
"./cli": {
|
|
21
|
+
"default": "./dist/cli.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup src/index.ts src/cli.ts src/eleventy.ts --format esm --dts --clean",
|
|
26
|
+
"dev": "tsup src/index.ts src/cli.ts src/eleventy.ts --format esm --dts --watch",
|
|
27
|
+
"typecheck": "tsc --noEmit",
|
|
28
|
+
"pretest": "../../scripts/ensure-fresh.sh ../i18n . && npm run build --silent",
|
|
29
|
+
"test": "node --test test/*.test.mjs",
|
|
30
|
+
"test:check": "node --test --test-reporter=spec test/*.test.mjs 2>&1 | tail -10"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"hyperscript",
|
|
34
|
+
"_hyperscript",
|
|
35
|
+
"i18n",
|
|
36
|
+
"internationalization",
|
|
37
|
+
"translation",
|
|
38
|
+
"build-time",
|
|
39
|
+
"eleventy",
|
|
40
|
+
"11ty",
|
|
41
|
+
"cli",
|
|
42
|
+
"static-site"
|
|
43
|
+
],
|
|
44
|
+
"author": "HyperFixi Contributors",
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@lokascript/i18n": "^2.3.1"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/node": "^20.0.0",
|
|
51
|
+
"tsup": "^8.0.0",
|
|
52
|
+
"typescript": "^5.0.0"
|
|
53
|
+
},
|
|
54
|
+
"files": [
|
|
55
|
+
"dist",
|
|
56
|
+
"README.md",
|
|
57
|
+
"LICENSE"
|
|
58
|
+
],
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public"
|
|
61
|
+
},
|
|
62
|
+
"repository": {
|
|
63
|
+
"type": "git",
|
|
64
|
+
"url": "git+https://github.com/codetalcott/hyperfixi.git",
|
|
65
|
+
"directory": "packages/hyperscript-tools-i18n"
|
|
66
|
+
},
|
|
67
|
+
"bugs": {
|
|
68
|
+
"url": "https://github.com/codetalcott/hyperfixi/issues"
|
|
69
|
+
},
|
|
70
|
+
"homepage": "https://github.com/codetalcott/hyperfixi/tree/main/packages/hyperscript-tools-i18n#readme",
|
|
71
|
+
"engines": {
|
|
72
|
+
"node": ">=18.0.0"
|
|
73
|
+
}
|
|
74
|
+
}
|