@wads.dev/i18n-html 0.0.1-alpha.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 Wads.dev
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 ADDED
@@ -0,0 +1,50 @@
1
+ # @wads.dev/i18n-html
2
+
3
+ Typed DOM bindings and static HTML usage discovery for catalogs built with [`@wads.dev/i18n-ts`](https://github.com/wads-dev/i18n-ts).
4
+
5
+ > Status: `0.0.1-alpha.0`. The API may change before `1.0.0`.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install @wads.dev/i18n-ts@alpha @wads.dev/i18n-html@alpha
11
+ ```
12
+
13
+ ## Translate a document
14
+
15
+ Use `data-i18n` for element text and `data-i18n-{attribute}` for attributes.
16
+
17
+ ```html
18
+ <h1 data-i18n="page.title">Loading…</h1>
19
+ <input data-i18n-placeholder="search.placeholder" placeholder="Loading…">
20
+ ```
21
+
22
+ ```ts
23
+ import { translateHtml } from '@wads.dev/i18n-html'
24
+
25
+ translateHtml(document, Lang)
26
+ ```
27
+
28
+ `translateHtml` is explicit. It does not watch the DOM or keep its own language state; call it after loading or changing the language.
29
+
30
+ ## Discover static HTML usage
31
+
32
+ Tooling can recognize the same contract through the Node-only entry point:
33
+
34
+ ```ts
35
+ import { collectHtmlI18nReferences } from '@wads.dev/i18n-html/usage'
36
+
37
+ const references = collectHtmlI18nReferences({
38
+ directory: process.cwd(),
39
+ ignoredDirectories: ['src/i18n'],
40
+ })
41
+ ```
42
+
43
+ Each reference contains the translation key plus the relative file, line and column.
44
+
45
+ ## Ecosystem
46
+
47
+ - [`@wads.dev/i18n-ts`](https://github.com/wads-dev/i18n-ts): typed contracts, language loading, bundles and configuration.
48
+ - [`@wads.dev/i18n-react`](https://github.com/wads-dev/i18n-react): React adapter.
49
+ - [`@wads.dev/i18n-html`](https://github.com/wads-dev/i18n-html): DOM/HTML adapter.
50
+ - [`@wads.dev/i18n-editor`](https://github.com/wads-dev/i18n-editor): local bundle editor and project exporter.
@@ -0,0 +1,4 @@
1
+ import type { DeepReadonly, Translation } from '@wads.dev/i18n-ts';
2
+ export type HtmlTranslationTree = DeepReadonly<Translation>;
3
+ export declare function readHtmlTranslation(translations: HtmlTranslationTree, key: string): string;
4
+ export declare function translateHtml(document: Document, translations: HtmlTranslationTree): void;
@@ -0,0 +1,27 @@
1
+ function readTranslationValue(translations, key) {
2
+ return key.split('.').reduce((current, segment) => {
3
+ return current && typeof current === 'object' ? current[segment] : undefined;
4
+ }, translations);
5
+ }
6
+ export function readHtmlTranslation(translations, key) {
7
+ const value = readTranslationValue(translations, key);
8
+ if (typeof value !== 'string')
9
+ throw new Error(`HTML translation must resolve to a string: ${key}`);
10
+ return value;
11
+ }
12
+ export function translateHtml(document, translations) {
13
+ document.querySelectorAll('[data-i18n]').forEach((element) => {
14
+ const key = element.dataset.i18n;
15
+ if (key)
16
+ element.textContent = readHtmlTranslation(translations, key);
17
+ });
18
+ document.querySelectorAll('*').forEach((element) => {
19
+ Array.from(element.attributes).forEach((attribute) => {
20
+ if (!attribute.name.startsWith('data-i18n-'))
21
+ return;
22
+ const targetAttribute = attribute.name.slice('data-i18n-'.length);
23
+ if (targetAttribute)
24
+ element.setAttribute(targetAttribute, readHtmlTranslation(translations, attribute.value));
25
+ });
26
+ });
27
+ }
@@ -0,0 +1,11 @@
1
+ export type HtmlI18nReference = {
2
+ key: string;
3
+ file: string;
4
+ line: number;
5
+ column: number;
6
+ };
7
+ export type CollectHtmlI18nReferencesOptions = {
8
+ directory: string;
9
+ ignoredDirectories?: string[];
10
+ };
11
+ export declare function collectHtmlI18nReferences({ directory, ignoredDirectories, }: CollectHtmlI18nReferencesOptions): HtmlI18nReference[];
@@ -0,0 +1,46 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ function isInside(filePath, directory) {
4
+ const relative = path.relative(directory, filePath);
5
+ return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
6
+ }
7
+ function collectHtmlFiles(directory, ignoredDirectories, result = []) {
8
+ for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
9
+ const filePath = path.join(directory, entry.name);
10
+ if (entry.isDirectory()) {
11
+ if (entry.name === '.git' || entry.name === 'dist' || entry.name === 'node_modules')
12
+ continue;
13
+ if (ignoredDirectories.some((ignoredDirectory) => isInside(filePath, ignoredDirectory)))
14
+ continue;
15
+ collectHtmlFiles(filePath, ignoredDirectories, result);
16
+ }
17
+ else if (entry.isFile() && entry.name.endsWith('.html')) {
18
+ result.push(filePath);
19
+ }
20
+ }
21
+ return result;
22
+ }
23
+ function lineAndColumn(content, offset) {
24
+ const before = content.slice(0, offset);
25
+ const line = before.split('\n').length;
26
+ return { line, column: offset - before.lastIndexOf('\n') };
27
+ }
28
+ export function collectHtmlI18nReferences({ directory, ignoredDirectories = [], }) {
29
+ const resolvedDirectory = path.resolve(directory);
30
+ const resolvedIgnoredDirectories = ignoredDirectories.map((ignoredDirectory) => path.resolve(ignoredDirectory));
31
+ const attributePattern = /\bdata-i18n(?:-[\w-]+)?\s*=\s*(["'])([^"']+)\1/g;
32
+ return collectHtmlFiles(resolvedDirectory, resolvedIgnoredDirectories).flatMap((filePath) => {
33
+ const content = fs.readFileSync(filePath, 'utf8');
34
+ return [...content.matchAll(attributePattern)].map((match) => {
35
+ const key = match[2];
36
+ const keyOffset = (match.index ?? 0) + match[0].lastIndexOf(key);
37
+ const position = lineAndColumn(content, keyOffset);
38
+ return {
39
+ key,
40
+ file: path.relative(resolvedDirectory, filePath).replaceAll(path.sep, '/'),
41
+ line: position.line,
42
+ column: position.column,
43
+ };
44
+ });
45
+ });
46
+ }
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@wads.dev/i18n-html",
3
+ "version": "0.0.1-alpha.0",
4
+ "description": "Typed HTML bindings and static usage discovery for @wads.dev/i18n-ts catalogs.",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "Wads.dev",
8
+ "url": "https://github.com/wads-dev"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/wads-dev/i18n-html.git"
13
+ },
14
+ "homepage": "https://github.com/wads-dev/i18n-html#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/wads-dev/i18n-html/issues"
17
+ },
18
+ "keywords": [
19
+ "i18n",
20
+ "internationalization",
21
+ "html",
22
+ "typescript",
23
+ "translations"
24
+ ],
25
+ "type": "module",
26
+ "files": [
27
+ "dist",
28
+ "README.md",
29
+ "LICENSE"
30
+ ],
31
+ "engines": {
32
+ "node": ">=20"
33
+ },
34
+ "scripts": {
35
+ "clean": "node ./scripts/clean-dist.mjs",
36
+ "check": "tsc --project tsconfig.check.json --noEmit",
37
+ "build": "npm run clean && tsc --project tsconfig.build.json",
38
+ "prepack": "npm run check && npm run build"
39
+ },
40
+ "exports": {
41
+ ".": {
42
+ "types": "./dist/runtime/index.d.ts",
43
+ "import": "./dist/runtime/index.js"
44
+ },
45
+ "./usage": {
46
+ "types": "./dist/usage/index.d.ts",
47
+ "import": "./dist/usage/index.js"
48
+ }
49
+ },
50
+ "dependencies": {
51
+ "@wads.dev/i18n-ts": "^0.0.1-alpha.1"
52
+ },
53
+ "devDependencies": {
54
+ "@types/node": "^24.0.0",
55
+ "typescript": "^6.0.3"
56
+ },
57
+ "publishConfig": {
58
+ "access": "public"
59
+ }
60
+ }