@weglot/translate-react 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/LICENSE +202 -0
- package/NOTICE +6 -0
- package/README.md +190 -2
- package/build/_virtual/rolldown_runtime.cjs +29 -0
- package/build/_virtual/rolldown_runtime.mjs +7 -0
- package/build/index.cjs +0 -0
- package/build/index.d.cts +2 -0
- package/build/index.d.mts +2 -0
- package/build/index.mjs +1 -0
- package/build/locale.d.cts +6 -0
- package/build/locale.d.cts.map +1 -0
- package/build/locale.d.mts +6 -0
- package/build/locale.d.mts.map +1 -0
- package/build/metadata/manager.cjs +119 -0
- package/build/metadata/manager.mjs +114 -0
- package/build/metadata/manager.mjs.map +1 -0
- package/build/plugin/build-translator.cjs +207 -0
- package/build/plugin/build-translator.mjs +205 -0
- package/build/plugin/build-translator.mjs.map +1 -0
- package/build/plugin/cleanup.cjs +20 -0
- package/build/plugin/cleanup.mjs +20 -0
- package/build/plugin/cleanup.mjs.map +1 -0
- package/build/plugin/index.cjs +0 -0
- package/build/plugin/index.d.cts +2 -0
- package/build/plugin/index.d.mts +2 -0
- package/build/plugin/index.mjs +1 -0
- package/build/plugin/next-compiler-loader.cjs +40 -0
- package/build/plugin/next-compiler-loader.d.cts +12 -0
- package/build/plugin/next-compiler-loader.d.cts.map +1 -0
- package/build/plugin/next-compiler-loader.d.mts +13 -0
- package/build/plugin/next-compiler-loader.d.mts.map +1 -0
- package/build/plugin/next-compiler-loader.mjs +41 -0
- package/build/plugin/next-compiler-loader.mjs.map +1 -0
- package/build/plugin/next-config-loader.cjs +13 -0
- package/build/plugin/next-config-loader.d.cts +8 -0
- package/build/plugin/next-config-loader.d.cts.map +1 -0
- package/build/plugin/next-config-loader.d.mts +9 -0
- package/build/plugin/next-config-loader.d.mts.map +1 -0
- package/build/plugin/next-config-loader.mjs +14 -0
- package/build/plugin/next-config-loader.mjs.map +1 -0
- package/build/plugin/next-locale-client-loader.cjs +9 -0
- package/build/plugin/next-locale-client-loader.d.cts +8 -0
- package/build/plugin/next-locale-client-loader.d.cts.map +1 -0
- package/build/plugin/next-locale-client-loader.d.mts +9 -0
- package/build/plugin/next-locale-client-loader.d.mts.map +1 -0
- package/build/plugin/next-locale-client-loader.mjs +10 -0
- package/build/plugin/next-locale-client-loader.mjs.map +1 -0
- package/build/plugin/next-locale-server-loader.cjs +9 -0
- package/build/plugin/next-locale-server-loader.d.cts +8 -0
- package/build/plugin/next-locale-server-loader.d.cts.map +1 -0
- package/build/plugin/next-locale-server-loader.d.mts +9 -0
- package/build/plugin/next-locale-server-loader.d.mts.map +1 -0
- package/build/plugin/next-locale-server-loader.mjs +10 -0
- package/build/plugin/next-locale-server-loader.mjs.map +1 -0
- package/build/plugin/next.cjs +226 -0
- package/build/plugin/next.d.cts +9 -0
- package/build/plugin/next.d.cts.map +1 -0
- package/build/plugin/next.d.mts +9 -0
- package/build/plugin/next.d.mts.map +1 -0
- package/build/plugin/next.mjs +228 -0
- package/build/plugin/next.mjs.map +1 -0
- package/build/plugin/transform/babel-compat.cjs +13 -0
- package/build/plugin/transform/babel-compat.mjs +10 -0
- package/build/plugin/transform/babel-compat.mjs.map +1 -0
- package/build/plugin/transform/expression-strings.cjs +145 -0
- package/build/plugin/transform/expression-strings.mjs +140 -0
- package/build/plugin/transform/expression-strings.mjs.map +1 -0
- package/build/plugin/transform/index.cjs +45 -0
- package/build/plugin/transform/index.mjs +43 -0
- package/build/plugin/transform/index.mjs.map +1 -0
- package/build/plugin/transform/metadata.cjs +142 -0
- package/build/plugin/transform/metadata.mjs +141 -0
- package/build/plugin/transform/metadata.mjs.map +1 -0
- package/build/plugin/transform/process-file.cjs +525 -0
- package/build/plugin/transform/process-file.mjs +524 -0
- package/build/plugin/transform/process-file.mjs.map +1 -0
- package/build/plugin/transform/use-i18n.cjs +8 -0
- package/build/plugin/transform/use-i18n.mjs +7 -0
- package/build/plugin/transform/use-i18n.mjs.map +1 -0
- package/build/plugin/transform/utils.cjs +237 -0
- package/build/plugin/transform/utils.mjs +219 -0
- package/build/plugin/transform/utils.mjs.map +1 -0
- package/build/plugin/unplugin.cjs +188 -0
- package/build/plugin/unplugin.d.cts +8 -0
- package/build/plugin/unplugin.d.cts.map +1 -0
- package/build/plugin/unplugin.d.mts +8 -0
- package/build/plugin/unplugin.d.mts.map +1 -0
- package/build/plugin/unplugin.mjs +188 -0
- package/build/plugin/unplugin.mjs.map +1 -0
- package/build/plugin/vite.cjs +27 -0
- package/build/plugin/vite.d.cts +9 -0
- package/build/plugin/vite.d.cts.map +1 -0
- package/build/plugin/vite.d.mts +9 -0
- package/build/plugin/vite.d.mts.map +1 -0
- package/build/plugin/vite.mjs +28 -0
- package/build/plugin/vite.mjs.map +1 -0
- package/build/plugin/webpack.cjs +26 -0
- package/build/plugin/webpack.d.cts +8 -0
- package/build/plugin/webpack.d.cts.map +1 -0
- package/build/plugin/webpack.d.mts +8 -0
- package/build/plugin/webpack.d.mts.map +1 -0
- package/build/plugin/webpack.mjs +27 -0
- package/build/plugin/webpack.mjs.map +1 -0
- package/build/react/client/index.cjs +9 -0
- package/build/react/client/index.d.cts +5 -0
- package/build/react/client/index.d.mts +5 -0
- package/build/react/client/index.mjs +6 -0
- package/build/react/client/useTranslation.cjs +71 -0
- package/build/react/client/useTranslation.d.cts +42 -0
- package/build/react/client/useTranslation.d.cts.map +1 -0
- package/build/react/client/useTranslation.d.mts +42 -0
- package/build/react/client/useTranslation.d.mts.map +1 -0
- package/build/react/client/useTranslation.mjs +71 -0
- package/build/react/client/useTranslation.mjs.map +1 -0
- package/build/react/next/client.cjs +25 -0
- package/build/react/next/client.d.cts +9 -0
- package/build/react/next/client.d.cts.map +1 -0
- package/build/react/next/client.d.mts +9 -0
- package/build/react/next/client.d.mts.map +1 -0
- package/build/react/next/client.mjs +24 -0
- package/build/react/next/client.mjs.map +1 -0
- package/build/react/next/cookie-locale-resolver.cjs +29 -0
- package/build/react/next/cookie-locale-resolver.d.cts +33 -0
- package/build/react/next/cookie-locale-resolver.d.cts.map +1 -0
- package/build/react/next/cookie-locale-resolver.d.mts +33 -0
- package/build/react/next/cookie-locale-resolver.d.mts.map +1 -0
- package/build/react/next/cookie-locale-resolver.mjs +29 -0
- package/build/react/next/cookie-locale-resolver.mjs.map +1 -0
- package/build/react/next/server.cjs +21 -0
- package/build/react/next/server.d.cts +13 -0
- package/build/react/next/server.d.cts.map +1 -0
- package/build/react/next/server.d.mts +14 -0
- package/build/react/next/server.d.mts.map +1 -0
- package/build/react/next/server.mjs +20 -0
- package/build/react/next/server.mjs.map +1 -0
- package/build/react/server/ServerWeglotProvider.cjs +19 -0
- package/build/react/server/ServerWeglotProvider.d.cts +12 -0
- package/build/react/server/ServerWeglotProvider.d.cts.map +1 -0
- package/build/react/server/ServerWeglotProvider.d.mts +12 -0
- package/build/react/server/ServerWeglotProvider.d.mts.map +1 -0
- package/build/react/server/ServerWeglotProvider.mjs +19 -0
- package/build/react/server/ServerWeglotProvider.mjs.map +1 -0
- package/build/react/server/index.cjs +7 -0
- package/build/react/server/index.d.cts +4 -0
- package/build/react/server/index.d.mts +4 -0
- package/build/react/server/index.mjs +5 -0
- package/build/react/server/useTranslation.cjs +60 -0
- package/build/react/server/useTranslation.d.cts +36 -0
- package/build/react/server/useTranslation.d.cts.map +1 -0
- package/build/react/server/useTranslation.d.mts +36 -0
- package/build/react/server/useTranslation.d.mts.map +1 -0
- package/build/react/server/useTranslation.mjs +60 -0
- package/build/react/server/useTranslation.mjs.map +1 -0
- package/build/react/server-only/index.cjs +42 -0
- package/build/react/server-only/index.d.cts +38 -0
- package/build/react/server-only/index.d.cts.map +1 -0
- package/build/react/server-only/index.d.mts +38 -0
- package/build/react/server-only/index.d.mts.map +1 -0
- package/build/react/server-only/index.mjs +42 -0
- package/build/react/server-only/index.mjs.map +1 -0
- package/build/react/server-only/translations.cjs +86 -0
- package/build/react/server-only/translations.mjs +86 -0
- package/build/react/server-only/translations.mjs.map +1 -0
- package/build/react/shared/LocaleSwitcher.cjs +61 -0
- package/build/react/shared/LocaleSwitcher.d.cts +71 -0
- package/build/react/shared/LocaleSwitcher.d.cts.map +1 -0
- package/build/react/shared/LocaleSwitcher.d.mts +71 -0
- package/build/react/shared/LocaleSwitcher.d.mts.map +1 -0
- package/build/react/shared/LocaleSwitcher.mjs +61 -0
- package/build/react/shared/LocaleSwitcher.mjs.map +1 -0
- package/build/react/shared/WeglotContext.cjs +14 -0
- package/build/react/shared/WeglotContext.d.cts +34 -0
- package/build/react/shared/WeglotContext.d.cts.map +1 -0
- package/build/react/shared/WeglotContext.d.mts +34 -0
- package/build/react/shared/WeglotContext.d.mts.map +1 -0
- package/build/react/shared/WeglotContext.mjs +13 -0
- package/build/react/shared/WeglotContext.mjs.map +1 -0
- package/build/react/shared/WeglotProvider.cjs +250 -0
- package/build/react/shared/WeglotProvider.d.cts +60 -0
- package/build/react/shared/WeglotProvider.d.cts.map +1 -0
- package/build/react/shared/WeglotProvider.d.mts +60 -0
- package/build/react/shared/WeglotProvider.d.mts.map +1 -0
- package/build/react/shared/WeglotProvider.mjs +250 -0
- package/build/react/shared/WeglotProvider.mjs.map +1 -0
- package/build/react/shared/render-rich-text.cjs +56 -0
- package/build/react/shared/render-rich-text.d.cts +16 -0
- package/build/react/shared/render-rich-text.d.cts.map +1 -0
- package/build/react/shared/render-rich-text.d.mts +16 -0
- package/build/react/shared/render-rich-text.d.mts.map +1 -0
- package/build/react/shared/render-rich-text.mjs +55 -0
- package/build/react/shared/render-rich-text.mjs.map +1 -0
- package/build/react/shared/utils.cjs +34 -0
- package/build/react/shared/utils.mjs +35 -0
- package/build/react/shared/utils.mjs.map +1 -0
- package/build/react/types.d.cts +16 -0
- package/build/react/types.d.cts.map +1 -0
- package/build/react/types.d.mts +16 -0
- package/build/react/types.d.mts.map +1 -0
- package/build/runtime/html/segmenter.cjs +226 -0
- package/build/runtime/html/segmenter.mjs +225 -0
- package/build/runtime/html/segmenter.mjs.map +1 -0
- package/build/runtime/index.cjs +6 -0
- package/build/runtime/index.d.cts +2 -0
- package/build/runtime/index.d.mts +2 -0
- package/build/runtime/index.mjs +3 -0
- package/build/runtime/translate-dynamic.cjs +142 -0
- package/build/runtime/translate-dynamic.d.cts +29 -0
- package/build/runtime/translate-dynamic.d.cts.map +1 -0
- package/build/runtime/translate-dynamic.d.mts +29 -0
- package/build/runtime/translate-dynamic.d.mts.map +1 -0
- package/build/runtime/translate-dynamic.mjs +139 -0
- package/build/runtime/translate-dynamic.mjs.map +1 -0
- package/build/translation-server/logger.cjs +37 -0
- package/build/translation-server/logger.mjs +37 -0
- package/build/translation-server/logger.mjs.map +1 -0
- package/build/translation-server/translation-server.cjs +451 -0
- package/build/translation-server/translation-server.mjs +448 -0
- package/build/translation-server/translation-server.mjs.map +1 -0
- package/build/translators/api.cjs +12 -0
- package/build/translators/api.mjs +12 -0
- package/build/translators/api.mjs.map +1 -0
- package/build/translators/cache-factory.cjs +26 -0
- package/build/translators/cache-factory.mjs +27 -0
- package/build/translators/cache-factory.mjs.map +1 -0
- package/build/translators/local-cache.cjs +132 -0
- package/build/translators/local-cache.mjs +130 -0
- package/build/translators/local-cache.mjs.map +1 -0
- package/build/translators/translation-service.cjs +137 -0
- package/build/translators/translation-service.mjs +138 -0
- package/build/translators/translation-service.mjs.map +1 -0
- package/build/translators/weglot/api-client.cjs +79 -0
- package/build/translators/weglot/api-client.mjs +79 -0
- package/build/translators/weglot/api-client.mjs.map +1 -0
- package/build/translators/weglot/definitions/definitions.generated.cjs +53 -0
- package/build/translators/weglot/definitions/definitions.generated.mjs +52 -0
- package/build/translators/weglot/definitions/definitions.generated.mjs.map +1 -0
- package/build/translators/weglot/engine.cjs +46 -0
- package/build/translators/weglot/engine.mjs +43 -0
- package/build/translators/weglot/engine.mjs.map +1 -0
- package/build/translators/weglot/translator.cjs +61 -0
- package/build/translators/weglot/translator.mjs +62 -0
- package/build/translators/weglot/translator.mjs.map +1 -0
- package/build/translators/weglot/wire-format.cjs +128 -0
- package/build/translators/weglot/wire-format.mjs +129 -0
- package/build/translators/weglot/wire-format.mjs.map +1 -0
- package/build/types.d.cts +143 -0
- package/build/types.d.cts.map +1 -0
- package/build/types.d.mts +143 -0
- package/build/types.d.mts.map +1 -0
- package/build/utils/config-factory.cjs +86 -0
- package/build/utils/config-factory.mjs +83 -0
- package/build/utils/config-factory.mjs.map +1 -0
- package/build/utils/hash.cjs +17 -0
- package/build/utils/hash.mjs +16 -0
- package/build/utils/hash.mjs.map +1 -0
- package/build/utils/is-valid-locale.cjs +13 -0
- package/build/utils/is-valid-locale.mjs +13 -0
- package/build/utils/is-valid-locale.mjs.map +1 -0
- package/build/utils/logger.cjs +51 -0
- package/build/utils/logger.mjs +50 -0
- package/build/utils/logger.mjs.map +1 -0
- package/build/utils/path-helpers.cjs +49 -0
- package/build/utils/path-helpers.mjs +47 -0
- package/build/utils/path-helpers.mjs.map +1 -0
- package/build/utils/timeout.cjs +42 -0
- package/build/utils/timeout.mjs +41 -0
- package/build/utils/timeout.mjs.map +1 -0
- package/build/virtual/code-generator.cjs +56 -0
- package/build/virtual/code-generator.mjs +55 -0
- package/build/virtual/code-generator.mjs.map +1 -0
- package/build/virtual/config.cjs +16 -0
- package/build/virtual/config.d.cts +13 -0
- package/build/virtual/config.d.cts.map +1 -0
- package/build/virtual/config.d.mts +13 -0
- package/build/virtual/config.d.mts.map +1 -0
- package/build/virtual/config.mjs +12 -0
- package/build/virtual/config.mjs.map +1 -0
- package/build/virtual/locale/client.cjs +23 -0
- package/build/virtual/locale/client.d.cts +19 -0
- package/build/virtual/locale/client.d.cts.map +1 -0
- package/build/virtual/locale/client.d.mts +19 -0
- package/build/virtual/locale/client.d.mts.map +1 -0
- package/build/virtual/locale/client.mjs +22 -0
- package/build/virtual/locale/client.mjs.map +1 -0
- package/build/virtual/locale/server.cjs +13 -0
- package/build/virtual/locale/server.d.cts +13 -0
- package/build/virtual/locale/server.d.cts.map +1 -0
- package/build/virtual/locale/server.d.mts +13 -0
- package/build/virtual/locale/server.d.mts.map +1 -0
- package/build/virtual/locale/server.mjs +13 -0
- package/build/virtual/locale/server.mjs.map +1 -0
- package/package.json +260 -4
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { LocaleCode } from "../locale.cjs";
|
|
2
|
+
import { RichTextParams } from "./shared/render-rich-text.cjs";
|
|
3
|
+
import { ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
//#region src/react/types.d.ts
|
|
6
|
+
type TranslationHook = (hashes: string[]) => {
|
|
7
|
+
t: TranslationFunction;
|
|
8
|
+
locale: LocaleCode;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Translation function type
|
|
12
|
+
*/
|
|
13
|
+
type TranslationFunction = (hash: string, source: string, params?: RichTextParams) => string | ReactNode;
|
|
14
|
+
//#endregion
|
|
15
|
+
export { TranslationHook };
|
|
16
|
+
//# sourceMappingURL=types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.cts","names":[],"sources":["../../src/react/types.ts"],"sourcesContent":[],"mappings":";;;;;KAIY,eAAA;KACP;EADO,MAAA,EAEF,UAFiB;AAQ3B,CAAA;;;;KAAY,mBAAA,2CAGD,4BACG"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { LocaleCode } from "../locale.mjs";
|
|
2
|
+
import { RichTextParams } from "./shared/render-rich-text.mjs";
|
|
3
|
+
import { ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
//#region src/react/types.d.ts
|
|
6
|
+
type TranslationHook = (hashes: string[]) => {
|
|
7
|
+
t: TranslationFunction;
|
|
8
|
+
locale: LocaleCode;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Translation function type
|
|
12
|
+
*/
|
|
13
|
+
type TranslationFunction = (hash: string, source: string, params?: RichTextParams) => string | ReactNode;
|
|
14
|
+
//#endregion
|
|
15
|
+
export { TranslationHook };
|
|
16
|
+
//# sourceMappingURL=types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.mts","names":[],"sources":["../../src/react/types.ts"],"sourcesContent":[],"mappings":";;;;;KAIY,eAAA;KACP;EADO,MAAA,EAEF,UAFiB;AAQ3B,CAAA;;;;KAAY,mBAAA,2CAGD,4BACG"}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_definitions_generated = require('../../translators/weglot/definitions/definitions.generated.cjs');
|
|
3
|
+
const require_engine = require('../../translators/weglot/engine.cjs');
|
|
4
|
+
let htmlparser2 = require("htmlparser2");
|
|
5
|
+
let domhandler = require("domhandler");
|
|
6
|
+
let dom_serializer = require("dom-serializer");
|
|
7
|
+
dom_serializer = require_rolldown_runtime.__toESM(dom_serializer);
|
|
8
|
+
|
|
9
|
+
//#region src/runtime/html/segmenter.ts
|
|
10
|
+
/**
|
|
11
|
+
* Splits an HTML fragment into the segments the API translates and puts them back,
|
|
12
|
+
* porting connect-edge's HTMLResource rules so a sentence travels whole.
|
|
13
|
+
*/
|
|
14
|
+
/** Text inside these never travels, whatever the merging rules say. */
|
|
15
|
+
const FORBIDDEN_PARENTS = new Set([
|
|
16
|
+
"script",
|
|
17
|
+
"style",
|
|
18
|
+
"textarea",
|
|
19
|
+
"title",
|
|
20
|
+
"pre",
|
|
21
|
+
"code"
|
|
22
|
+
]);
|
|
23
|
+
const EXCLUDE_ATTRIBUTES = ["data-wg-notranslate", "data-weglot-skip"];
|
|
24
|
+
/** Attributes carrying copy and the word type each travels under. Order is the
|
|
25
|
+
* order segments are sent in. */
|
|
26
|
+
const ATTRIBUTE_DEFINITIONS = [
|
|
27
|
+
{
|
|
28
|
+
attribute: "title",
|
|
29
|
+
type: require_definitions_generated.WORD_TYPE.TEXT
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
attribute: "alt",
|
|
33
|
+
type: require_definitions_generated.WORD_TYPE.IMG_ALT,
|
|
34
|
+
applies: (element) => element.name === "img"
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
attribute: "src",
|
|
38
|
+
type: require_definitions_generated.WORD_TYPE.IMG_SRC,
|
|
39
|
+
applies: (element) => element.name === "img"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
attribute: "value",
|
|
43
|
+
type: require_definitions_generated.WORD_TYPE.VALUE,
|
|
44
|
+
applies: (element) => element.name === "button" || element.name === "input" && [
|
|
45
|
+
"submit",
|
|
46
|
+
"button",
|
|
47
|
+
"reset"
|
|
48
|
+
].includes(element.attribs.type ?? "")
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
attribute: "placeholder",
|
|
52
|
+
type: require_definitions_generated.WORD_TYPE.PLACEHOLDER,
|
|
53
|
+
applies: (element) => element.name === "input" || element.name === "textarea"
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
attribute: "content",
|
|
57
|
+
type: require_definitions_generated.WORD_TYPE.META_CONTENT,
|
|
58
|
+
applies: (element) => element.name === "meta" && ["description", "keywords"].includes(element.attribs.name ?? "")
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
attribute: "href",
|
|
62
|
+
type: require_definitions_generated.WORD_TYPE.PDF_HREF,
|
|
63
|
+
applies: (element) => /\.pdf(\?|#|$)/i.test(element.attribs.href ?? "")
|
|
64
|
+
}
|
|
65
|
+
];
|
|
66
|
+
const MARKER = /(?: ?)wg-(\d+)(="")?/gi;
|
|
67
|
+
/** HTML, not XHTML: `<br>` stays `<br>`, and a marker keeps the `=""` the API matches on. */
|
|
68
|
+
const RENDER = {
|
|
69
|
+
decodeEntities: false,
|
|
70
|
+
emptyAttrs: true,
|
|
71
|
+
selfClosingTags: false
|
|
72
|
+
};
|
|
73
|
+
/** Whitespace, a zero-width space or a bare nbsp is not a sentence. */
|
|
74
|
+
function isValidText(text) {
|
|
75
|
+
const trimmed = text.trim();
|
|
76
|
+
return trimmed.length > 0 && trimmed !== "" && trimmed !== "\xA0";
|
|
77
|
+
}
|
|
78
|
+
function isExcluded(node) {
|
|
79
|
+
if (FORBIDDEN_PARENTS.has(node.name)) return true;
|
|
80
|
+
if (node.attribs?.translate === "no") return true;
|
|
81
|
+
return EXCLUDE_ATTRIBUTES.some((name) => name in (node.attribs ?? {}));
|
|
82
|
+
}
|
|
83
|
+
function markUp(node, key) {
|
|
84
|
+
let current = node;
|
|
85
|
+
while (current && !current[key]) {
|
|
86
|
+
current[key] = true;
|
|
87
|
+
current = current.parent;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** Marks exclusions and which nodes carry translatable text. */
|
|
91
|
+
function annotate(node, inExcluded) {
|
|
92
|
+
if ((0, domhandler.isTag)(node)) {
|
|
93
|
+
const excluded = inExcluded || isExcluded(node);
|
|
94
|
+
node.excluded = excluded;
|
|
95
|
+
for (const child of node.children) annotate(child, excluded);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if ((0, domhandler.isText)(node) && !inExcluded && isValidText(node.data)) {
|
|
99
|
+
const parent = node.parent;
|
|
100
|
+
if (parent && (0, domhandler.isTag)(parent) && FORBIDDEN_PARENTS.has(parent.name)) return;
|
|
101
|
+
markUp(node, "hasValidText");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
var HtmlSegmenter = class {
|
|
105
|
+
mergeable;
|
|
106
|
+
document;
|
|
107
|
+
setters = [];
|
|
108
|
+
segments = [];
|
|
109
|
+
markerIndex = 1;
|
|
110
|
+
constructor(html, engine = 3) {
|
|
111
|
+
this.mergeable = require_engine.mergeableTagsFor(engine);
|
|
112
|
+
this.document = (0, htmlparser2.parseDocument)(`<div>${html}</div>`, { decodeEntities: false });
|
|
113
|
+
}
|
|
114
|
+
/** The segments to send, in payload order. */
|
|
115
|
+
extract() {
|
|
116
|
+
const root = this.document.children[0];
|
|
117
|
+
annotate(root, false);
|
|
118
|
+
this.walk(root);
|
|
119
|
+
return this.segments;
|
|
120
|
+
}
|
|
121
|
+
/** Applies translations positionally and returns the rebuilt fragment. */
|
|
122
|
+
apply(translations) {
|
|
123
|
+
translations.forEach((translation, index) => {
|
|
124
|
+
this.setters[index]?.(translation);
|
|
125
|
+
});
|
|
126
|
+
const root = this.document.children[0];
|
|
127
|
+
return (0, dom_serializer.default)(root.children, RENDER);
|
|
128
|
+
}
|
|
129
|
+
/** Post-order, so a parent knows whether it holds an unmergeable descendant. */
|
|
130
|
+
walk(node) {
|
|
131
|
+
this.registerAttributes(node);
|
|
132
|
+
for (const child of node.children) if ((0, domhandler.isTag)(child)) this.walk(child);
|
|
133
|
+
if (this.canMerge(node)) return;
|
|
134
|
+
if (node.parent) markUp(node.parent, "hasUnmergeableChild");
|
|
135
|
+
this.extractFrom(node);
|
|
136
|
+
}
|
|
137
|
+
canMerge(node) {
|
|
138
|
+
if (node.excluded) return false;
|
|
139
|
+
return this.mergeable.has(node.name);
|
|
140
|
+
}
|
|
141
|
+
extractFrom(node) {
|
|
142
|
+
if (!node.hasValidText) return;
|
|
143
|
+
const childrenWithText = node.children.filter((child) => child.hasValidText);
|
|
144
|
+
if (!node.hasUnmergeableChild && childrenWithText.length > 1) {
|
|
145
|
+
this.markerIndex = 1;
|
|
146
|
+
this.renameAttributes(node, node);
|
|
147
|
+
this.registerNode(node);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
for (const child of node.children) {
|
|
151
|
+
if ((0, domhandler.isText)(child) && child.hasValidText) {
|
|
152
|
+
this.registerText(child);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if ((0, domhandler.isTag)(child) && this.canMerge(child)) this.extractFrom(child);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/** Indexed rather than stripped: any non-marker attribute makes the API return
|
|
159
|
+
* the segment verbatim. Originals are parked on the segment root. */
|
|
160
|
+
renameAttributes(root, node) {
|
|
161
|
+
for (const child of node.children) {
|
|
162
|
+
if (!(0, domhandler.isTag)(child)) continue;
|
|
163
|
+
root.originalNodes ??= [];
|
|
164
|
+
root.originalNodes.push(child);
|
|
165
|
+
child.originalAttributes = { ...child.attribs };
|
|
166
|
+
child.attribs = { [`wg-${this.markerIndex++}`]: "" };
|
|
167
|
+
this.renameAttributes(root, child);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
registerNode(element) {
|
|
171
|
+
for (const comment of element.children.filter(domhandler.isComment)) removeChild(comment);
|
|
172
|
+
this.segments.push({
|
|
173
|
+
t: require_definitions_generated.WORD_TYPE.TEXT,
|
|
174
|
+
w: (0, dom_serializer.default)(element.children, RENDER)
|
|
175
|
+
});
|
|
176
|
+
this.setters.push((translation) => {
|
|
177
|
+
element.children = (0, htmlparser2.parseDocument)(this.restoreAttributes(element, translation), { decodeEntities: false }).children;
|
|
178
|
+
for (const child of element.children) child.parent = element;
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
registerAttributes(element) {
|
|
182
|
+
if (element.excluded) return;
|
|
183
|
+
for (const definition of ATTRIBUTE_DEFINITIONS) {
|
|
184
|
+
const value = element.attribs?.[definition.attribute];
|
|
185
|
+
if (value === void 0 || !isValidText(value)) continue;
|
|
186
|
+
if (definition.applies && !definition.applies(element)) continue;
|
|
187
|
+
this.segments.push({
|
|
188
|
+
t: definition.type,
|
|
189
|
+
w: value
|
|
190
|
+
});
|
|
191
|
+
this.setters.push((translation) => {
|
|
192
|
+
element.attribs[definition.attribute] = translation;
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
registerText(node) {
|
|
197
|
+
this.segments.push({
|
|
198
|
+
t: require_definitions_generated.WORD_TYPE.TEXT,
|
|
199
|
+
w: node.data
|
|
200
|
+
});
|
|
201
|
+
this.setters.push((translation) => {
|
|
202
|
+
node.data = translation;
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/** Puts each element's own attributes back in place of its marker. */
|
|
206
|
+
restoreAttributes(root, translation) {
|
|
207
|
+
const seen = /* @__PURE__ */ new Set();
|
|
208
|
+
return translation.replace(MARKER, (_match, index) => {
|
|
209
|
+
const child = root.originalNodes?.[Number(index) - 1];
|
|
210
|
+
if (!child) return "";
|
|
211
|
+
const attributes = child.originalAttributes ?? {};
|
|
212
|
+
const serialized = Object.entries(attributes).map(([key, value]) => `${key}="${child.attribs[key] ?? value}"`).join(" ");
|
|
213
|
+
const split = seen.has(index) ? " data-wg-splitted" : "";
|
|
214
|
+
seen.add(index);
|
|
215
|
+
return serialized ? ` ${serialized}${split}` : split;
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
function removeChild(node) {
|
|
220
|
+
const parent = node.parent;
|
|
221
|
+
if (!parent) return;
|
|
222
|
+
parent.children = parent.children.filter((child) => child !== node);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
//#endregion
|
|
226
|
+
exports.HtmlSegmenter = HtmlSegmenter;
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { WORD_TYPE } from "../../translators/weglot/definitions/definitions.generated.mjs";
|
|
2
|
+
import { mergeableTagsFor } from "../../translators/weglot/engine.mjs";
|
|
3
|
+
import { parseDocument } from "htmlparser2";
|
|
4
|
+
import { isComment, isTag, isText } from "domhandler";
|
|
5
|
+
import render from "dom-serializer";
|
|
6
|
+
|
|
7
|
+
//#region src/runtime/html/segmenter.ts
|
|
8
|
+
/**
|
|
9
|
+
* Splits an HTML fragment into the segments the API translates and puts them back,
|
|
10
|
+
* porting connect-edge's HTMLResource rules so a sentence travels whole.
|
|
11
|
+
*/
|
|
12
|
+
/** Text inside these never travels, whatever the merging rules say. */
|
|
13
|
+
const FORBIDDEN_PARENTS = new Set([
|
|
14
|
+
"script",
|
|
15
|
+
"style",
|
|
16
|
+
"textarea",
|
|
17
|
+
"title",
|
|
18
|
+
"pre",
|
|
19
|
+
"code"
|
|
20
|
+
]);
|
|
21
|
+
const EXCLUDE_ATTRIBUTES = ["data-wg-notranslate", "data-weglot-skip"];
|
|
22
|
+
/** Attributes carrying copy and the word type each travels under. Order is the
|
|
23
|
+
* order segments are sent in. */
|
|
24
|
+
const ATTRIBUTE_DEFINITIONS = [
|
|
25
|
+
{
|
|
26
|
+
attribute: "title",
|
|
27
|
+
type: WORD_TYPE.TEXT
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
attribute: "alt",
|
|
31
|
+
type: WORD_TYPE.IMG_ALT,
|
|
32
|
+
applies: (element) => element.name === "img"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
attribute: "src",
|
|
36
|
+
type: WORD_TYPE.IMG_SRC,
|
|
37
|
+
applies: (element) => element.name === "img"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
attribute: "value",
|
|
41
|
+
type: WORD_TYPE.VALUE,
|
|
42
|
+
applies: (element) => element.name === "button" || element.name === "input" && [
|
|
43
|
+
"submit",
|
|
44
|
+
"button",
|
|
45
|
+
"reset"
|
|
46
|
+
].includes(element.attribs.type ?? "")
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
attribute: "placeholder",
|
|
50
|
+
type: WORD_TYPE.PLACEHOLDER,
|
|
51
|
+
applies: (element) => element.name === "input" || element.name === "textarea"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
attribute: "content",
|
|
55
|
+
type: WORD_TYPE.META_CONTENT,
|
|
56
|
+
applies: (element) => element.name === "meta" && ["description", "keywords"].includes(element.attribs.name ?? "")
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
attribute: "href",
|
|
60
|
+
type: WORD_TYPE.PDF_HREF,
|
|
61
|
+
applies: (element) => /\.pdf(\?|#|$)/i.test(element.attribs.href ?? "")
|
|
62
|
+
}
|
|
63
|
+
];
|
|
64
|
+
const MARKER = /(?: ?)wg-(\d+)(="")?/gi;
|
|
65
|
+
/** HTML, not XHTML: `<br>` stays `<br>`, and a marker keeps the `=""` the API matches on. */
|
|
66
|
+
const RENDER = {
|
|
67
|
+
decodeEntities: false,
|
|
68
|
+
emptyAttrs: true,
|
|
69
|
+
selfClosingTags: false
|
|
70
|
+
};
|
|
71
|
+
/** Whitespace, a zero-width space or a bare nbsp is not a sentence. */
|
|
72
|
+
function isValidText(text) {
|
|
73
|
+
const trimmed = text.trim();
|
|
74
|
+
return trimmed.length > 0 && trimmed !== "" && trimmed !== "\xA0";
|
|
75
|
+
}
|
|
76
|
+
function isExcluded(node) {
|
|
77
|
+
if (FORBIDDEN_PARENTS.has(node.name)) return true;
|
|
78
|
+
if (node.attribs?.translate === "no") return true;
|
|
79
|
+
return EXCLUDE_ATTRIBUTES.some((name) => name in (node.attribs ?? {}));
|
|
80
|
+
}
|
|
81
|
+
function markUp(node, key) {
|
|
82
|
+
let current = node;
|
|
83
|
+
while (current && !current[key]) {
|
|
84
|
+
current[key] = true;
|
|
85
|
+
current = current.parent;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** Marks exclusions and which nodes carry translatable text. */
|
|
89
|
+
function annotate(node, inExcluded) {
|
|
90
|
+
if (isTag(node)) {
|
|
91
|
+
const excluded = inExcluded || isExcluded(node);
|
|
92
|
+
node.excluded = excluded;
|
|
93
|
+
for (const child of node.children) annotate(child, excluded);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (isText(node) && !inExcluded && isValidText(node.data)) {
|
|
97
|
+
const parent = node.parent;
|
|
98
|
+
if (parent && isTag(parent) && FORBIDDEN_PARENTS.has(parent.name)) return;
|
|
99
|
+
markUp(node, "hasValidText");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
var HtmlSegmenter = class {
|
|
103
|
+
mergeable;
|
|
104
|
+
document;
|
|
105
|
+
setters = [];
|
|
106
|
+
segments = [];
|
|
107
|
+
markerIndex = 1;
|
|
108
|
+
constructor(html, engine = 3) {
|
|
109
|
+
this.mergeable = mergeableTagsFor(engine);
|
|
110
|
+
this.document = parseDocument(`<div>${html}</div>`, { decodeEntities: false });
|
|
111
|
+
}
|
|
112
|
+
/** The segments to send, in payload order. */
|
|
113
|
+
extract() {
|
|
114
|
+
const root = this.document.children[0];
|
|
115
|
+
annotate(root, false);
|
|
116
|
+
this.walk(root);
|
|
117
|
+
return this.segments;
|
|
118
|
+
}
|
|
119
|
+
/** Applies translations positionally and returns the rebuilt fragment. */
|
|
120
|
+
apply(translations) {
|
|
121
|
+
translations.forEach((translation, index) => {
|
|
122
|
+
this.setters[index]?.(translation);
|
|
123
|
+
});
|
|
124
|
+
const root = this.document.children[0];
|
|
125
|
+
return render(root.children, RENDER);
|
|
126
|
+
}
|
|
127
|
+
/** Post-order, so a parent knows whether it holds an unmergeable descendant. */
|
|
128
|
+
walk(node) {
|
|
129
|
+
this.registerAttributes(node);
|
|
130
|
+
for (const child of node.children) if (isTag(child)) this.walk(child);
|
|
131
|
+
if (this.canMerge(node)) return;
|
|
132
|
+
if (node.parent) markUp(node.parent, "hasUnmergeableChild");
|
|
133
|
+
this.extractFrom(node);
|
|
134
|
+
}
|
|
135
|
+
canMerge(node) {
|
|
136
|
+
if (node.excluded) return false;
|
|
137
|
+
return this.mergeable.has(node.name);
|
|
138
|
+
}
|
|
139
|
+
extractFrom(node) {
|
|
140
|
+
if (!node.hasValidText) return;
|
|
141
|
+
const childrenWithText = node.children.filter((child) => child.hasValidText);
|
|
142
|
+
if (!node.hasUnmergeableChild && childrenWithText.length > 1) {
|
|
143
|
+
this.markerIndex = 1;
|
|
144
|
+
this.renameAttributes(node, node);
|
|
145
|
+
this.registerNode(node);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
for (const child of node.children) {
|
|
149
|
+
if (isText(child) && child.hasValidText) {
|
|
150
|
+
this.registerText(child);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (isTag(child) && this.canMerge(child)) this.extractFrom(child);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/** Indexed rather than stripped: any non-marker attribute makes the API return
|
|
157
|
+
* the segment verbatim. Originals are parked on the segment root. */
|
|
158
|
+
renameAttributes(root, node) {
|
|
159
|
+
for (const child of node.children) {
|
|
160
|
+
if (!isTag(child)) continue;
|
|
161
|
+
root.originalNodes ??= [];
|
|
162
|
+
root.originalNodes.push(child);
|
|
163
|
+
child.originalAttributes = { ...child.attribs };
|
|
164
|
+
child.attribs = { [`wg-${this.markerIndex++}`]: "" };
|
|
165
|
+
this.renameAttributes(root, child);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
registerNode(element) {
|
|
169
|
+
for (const comment of element.children.filter(isComment)) removeChild(comment);
|
|
170
|
+
this.segments.push({
|
|
171
|
+
t: WORD_TYPE.TEXT,
|
|
172
|
+
w: render(element.children, RENDER)
|
|
173
|
+
});
|
|
174
|
+
this.setters.push((translation) => {
|
|
175
|
+
element.children = parseDocument(this.restoreAttributes(element, translation), { decodeEntities: false }).children;
|
|
176
|
+
for (const child of element.children) child.parent = element;
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
registerAttributes(element) {
|
|
180
|
+
if (element.excluded) return;
|
|
181
|
+
for (const definition of ATTRIBUTE_DEFINITIONS) {
|
|
182
|
+
const value = element.attribs?.[definition.attribute];
|
|
183
|
+
if (value === void 0 || !isValidText(value)) continue;
|
|
184
|
+
if (definition.applies && !definition.applies(element)) continue;
|
|
185
|
+
this.segments.push({
|
|
186
|
+
t: definition.type,
|
|
187
|
+
w: value
|
|
188
|
+
});
|
|
189
|
+
this.setters.push((translation) => {
|
|
190
|
+
element.attribs[definition.attribute] = translation;
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
registerText(node) {
|
|
195
|
+
this.segments.push({
|
|
196
|
+
t: WORD_TYPE.TEXT,
|
|
197
|
+
w: node.data
|
|
198
|
+
});
|
|
199
|
+
this.setters.push((translation) => {
|
|
200
|
+
node.data = translation;
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
/** Puts each element's own attributes back in place of its marker. */
|
|
204
|
+
restoreAttributes(root, translation) {
|
|
205
|
+
const seen = /* @__PURE__ */ new Set();
|
|
206
|
+
return translation.replace(MARKER, (_match, index) => {
|
|
207
|
+
const child = root.originalNodes?.[Number(index) - 1];
|
|
208
|
+
if (!child) return "";
|
|
209
|
+
const attributes = child.originalAttributes ?? {};
|
|
210
|
+
const serialized = Object.entries(attributes).map(([key, value]) => `${key}="${child.attribs[key] ?? value}"`).join(" ");
|
|
211
|
+
const split = seen.has(index) ? " data-wg-splitted" : "";
|
|
212
|
+
seen.add(index);
|
|
213
|
+
return serialized ? ` ${serialized}${split}` : split;
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
function removeChild(node) {
|
|
218
|
+
const parent = node.parent;
|
|
219
|
+
if (!parent) return;
|
|
220
|
+
parent.children = parent.children.filter((child) => child !== node);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
//#endregion
|
|
224
|
+
export { HtmlSegmenter };
|
|
225
|
+
//# sourceMappingURL=segmenter.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"segmenter.mjs","names":["ATTRIBUTE_DEFINITIONS: AttributeDefinition[]"],"sources":["../../../src/runtime/html/segmenter.ts"],"sourcesContent":["/**\n * Splits an HTML fragment into the segments the API translates and puts them back,\n * porting connect-edge's HTMLResource rules so a sentence travels whole.\n */\n\nimport { parseDocument } from \"htmlparser2\";\nimport {\n Element,\n Text,\n type AnyNode,\n type Document,\n isTag,\n isText,\n isComment,\n} from \"domhandler\";\nimport render from \"dom-serializer\";\nimport { mergeableTagsFor } from \"../../translators/weglot/engine\";\nimport { WORD_TYPE } from \"../../translators/weglot/definitions/definitions.generated\";\n\n/** Text inside these never travels, whatever the merging rules say. */\nconst FORBIDDEN_PARENTS = new Set([\n \"script\",\n \"style\",\n \"textarea\",\n \"title\",\n \"pre\",\n \"code\",\n]);\n\nconst EXCLUDE_ATTRIBUTES = [\"data-wg-notranslate\", \"data-weglot-skip\"];\n\ninterface AttributeDefinition {\n attribute: string;\n type: number;\n applies?: (element: Element) => boolean;\n}\n\n/** Attributes carrying copy and the word type each travels under. Order is the\n * order segments are sent in. */\nconst ATTRIBUTE_DEFINITIONS: AttributeDefinition[] = [\n { attribute: \"title\", type: WORD_TYPE.TEXT },\n {\n attribute: \"alt\",\n type: WORD_TYPE.IMG_ALT,\n applies: (element) => element.name === \"img\",\n },\n {\n attribute: \"src\",\n type: WORD_TYPE.IMG_SRC,\n applies: (element) => element.name === \"img\",\n },\n {\n attribute: \"value\",\n type: WORD_TYPE.VALUE,\n applies: (element) =>\n element.name === \"button\" ||\n (element.name === \"input\" &&\n [\"submit\", \"button\", \"reset\"].includes(element.attribs.type ?? \"\")),\n },\n {\n attribute: \"placeholder\",\n type: WORD_TYPE.PLACEHOLDER,\n applies: (element) => element.name === \"input\" || element.name === \"textarea\",\n },\n {\n attribute: \"content\",\n type: WORD_TYPE.META_CONTENT,\n applies: (element) =>\n element.name === \"meta\" &&\n [\"description\", \"keywords\"].includes(element.attribs.name ?? \"\"),\n },\n {\n attribute: \"href\",\n type: WORD_TYPE.PDF_HREF,\n applies: (element) => /\\.pdf(\\?|#|$)/i.test(element.attribs.href ?? \"\"),\n },\n];\n\nconst MARKER = /(?: ?)wg-(\\d+)(=\"\")?/gi;\n\n/** HTML, not XHTML: `<br>` stays `<br>`, and a marker keeps the `=\"\"` the API matches on. */\nconst RENDER = {\n decodeEntities: false,\n emptyAttrs: true,\n selfClosingTags: false,\n} as const;\n\nexport interface Segment {\n /** Weglot word type; body text is 1. */\n t: number;\n /** The text as it goes on the wire. */\n w: string;\n}\n\ninterface Annotated {\n hasValidText?: boolean;\n hasUnmergeableChild?: boolean;\n excluded?: boolean;\n originalNodes?: Element[];\n originalAttributes?: Record<string, string>;\n}\n\ntype Ann<T> = T & Annotated;\n\n/** Whitespace, a zero-width space or a bare nbsp is not a sentence. */\nfunction isValidText(text: string): boolean {\n const trimmed = text.trim();\n return trimmed.length > 0 && trimmed !== \"\" && trimmed !== \" \";\n}\n\nfunction isExcluded(node: Element): boolean {\n if (FORBIDDEN_PARENTS.has(node.name)) return true;\n if (node.attribs?.translate === \"no\") return true;\n return EXCLUDE_ATTRIBUTES.some((name) => name in (node.attribs ?? {}));\n}\n\nfunction markUp(node: AnyNode | null, key: \"hasValidText\" | \"hasUnmergeableChild\"): void {\n let current = node as Ann<AnyNode> | null;\n while (current && !current[key]) {\n current[key] = true;\n current = current.parent as Ann<AnyNode> | null;\n }\n}\n\n/** Marks exclusions and which nodes carry translatable text. */\nfunction annotate(node: Ann<AnyNode>, inExcluded: boolean): void {\n if (isTag(node)) {\n const excluded = inExcluded || isExcluded(node);\n (node as Ann<Element>).excluded = excluded;\n for (const child of node.children) {\n annotate(child as Ann<AnyNode>, excluded);\n }\n return;\n }\n\n if (isText(node) && !inExcluded && isValidText(node.data)) {\n const parent = node.parent;\n if (parent && isTag(parent) && FORBIDDEN_PARENTS.has(parent.name)) return;\n markUp(node, \"hasValidText\");\n }\n}\n\nexport class HtmlSegmenter {\n private readonly mergeable: ReadonlySet<string>;\n private readonly document: Document;\n private readonly setters: ((translation: string) => void)[] = [];\n private readonly segments: Segment[] = [];\n private markerIndex = 1;\n\n constructor(html: string, engine = 3) {\n this.mergeable = mergeableTagsFor(engine);\n // A fragment has no single root; the wrapper is removed on the way out.\n this.document = parseDocument(`<div>${html}</div>`, {\n decodeEntities: false,\n });\n }\n\n /** The segments to send, in payload order. */\n extract(): Segment[] {\n const root = this.document.children[0] as Ann<Element>;\n annotate(root, false);\n this.walk(root);\n return this.segments;\n }\n\n /** Applies translations positionally and returns the rebuilt fragment. */\n apply(translations: string[]): string {\n translations.forEach((translation, index) => {\n this.setters[index]?.(translation);\n });\n const root = this.document.children[0] as Element;\n return render(root.children, RENDER);\n }\n\n /** Post-order, so a parent knows whether it holds an unmergeable descendant. */\n private walk(node: Ann<Element>): void {\n // Registered on the way in, so a title is sent before its paragraph's text.\n this.registerAttributes(node);\n\n for (const child of node.children) {\n if (isTag(child)) this.walk(child as Ann<Element>);\n }\n\n if (this.canMerge(node)) return;\n if (node.parent) markUp(node.parent, \"hasUnmergeableChild\");\n this.extractFrom(node);\n }\n\n private canMerge(node: Ann<Element>): boolean {\n if (node.excluded) return false;\n return this.mergeable.has(node.name);\n }\n\n private extractFrom(node: Ann<Element>): void {\n if (!node.hasValidText) return;\n\n const childrenWithText = node.children.filter(\n (child) => (child as Ann<AnyNode>).hasValidText,\n );\n\n if (!node.hasUnmergeableChild && childrenWithText.length > 1) {\n this.markerIndex = 1;\n this.renameAttributes(node, node);\n this.registerNode(node);\n return;\n }\n\n for (const child of node.children) {\n if (isText(child) && (child as Ann<Text>).hasValidText) {\n this.registerText(child);\n continue;\n }\n if (isTag(child) && this.canMerge(child as Ann<Element>)) {\n this.extractFrom(child as Ann<Element>);\n }\n }\n }\n\n /** Indexed rather than stripped: any non-marker attribute makes the API return\n * the segment verbatim. Originals are parked on the segment root. */\n private renameAttributes(root: Ann<Element>, node: Element): void {\n for (const child of node.children) {\n if (!isTag(child)) continue;\n root.originalNodes ??= [];\n root.originalNodes.push(child);\n (child as Ann<Element>).originalAttributes = { ...child.attribs };\n child.attribs = { [`wg-${this.markerIndex++}`]: \"\" };\n this.renameAttributes(root, child);\n }\n }\n\n private registerNode(element: Ann<Element>): void {\n for (const comment of element.children.filter(isComment)) {\n removeChild(comment);\n }\n\n this.segments.push({\n t: WORD_TYPE.TEXT,\n w: render(element.children, RENDER),\n });\n\n this.setters.push((translation) => {\n // Re-parsed so the `<br wg-1=\"\"/>` translation returns normalises to `<br>`.\n const restored = this.restoreAttributes(element, translation);\n const parsed = parseDocument(restored, { decodeEntities: false });\n element.children = parsed.children;\n for (const child of element.children) {\n child.parent = element;\n }\n });\n }\n\n private registerAttributes(element: Ann<Element>): void {\n if (element.excluded) return;\n\n for (const definition of ATTRIBUTE_DEFINITIONS) {\n const value = element.attribs?.[definition.attribute];\n if (value === undefined || !isValidText(value)) continue;\n if (definition.applies && !definition.applies(element)) continue;\n\n this.segments.push({ t: definition.type, w: value });\n this.setters.push((translation) => {\n element.attribs[definition.attribute] = translation;\n });\n }\n }\n\n private registerText(node: Text): void {\n this.segments.push({ t: WORD_TYPE.TEXT, w: node.data });\n this.setters.push((translation) => {\n node.data = translation;\n });\n }\n\n /** Puts each element's own attributes back in place of its marker. */\n private restoreAttributes(root: Ann<Element>, translation: string): string {\n const seen = new Set<string>();\n return translation.replace(MARKER, (_match, index: string) => {\n const child = root.originalNodes?.[Number(index) - 1];\n if (!child) return \"\";\n const attributes = (child as Ann<Element>).originalAttributes ?? {};\n const serialized = Object.entries(attributes)\n .map(([key, value]) => `${key}=\"${child.attribs[key] ?? value}\"`)\n .join(\" \");\n // Translation sometimes splits an element in two; the copy is marked.\n const split = seen.has(index) ? \" data-wg-splitted\" : \"\";\n seen.add(index);\n return serialized ? ` ${serialized}${split}` : split;\n });\n }\n}\n\nfunction removeChild(node: AnyNode): void {\n const parent = node.parent;\n if (!parent) return;\n parent.children = parent.children.filter((child) => child !== node);\n}\n\n/** Convenience wrapper: the segments an HTML fragment yields. */\nexport function extractSegments(html: string, engine = 3): Segment[] {\n return new HtmlSegmenter(html, engine).extract();\n}\n\n/** Convenience wrapper: extract, translate positionally, rebuild. */\nexport function applyTranslations(\n html: string,\n translations: string[],\n engine = 3,\n): string {\n const segmenter = new HtmlSegmenter(html, engine);\n segmenter.extract();\n return segmenter.apply(translations);\n}\n"],"mappings":";;;;;;;;;;;;AAoBA,MAAM,oBAAoB,IAAI,IAAI;CAChC;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;AAEF,MAAM,qBAAqB,CAAC,uBAAuB,mBAAmB;;;AAUtE,MAAMA,wBAA+C;CACnD;EAAE,WAAW;EAAS,MAAM,UAAU;EAAM;CAC5C;EACE,WAAW;EACX,MAAM,UAAU;EAChB,UAAU,YAAY,QAAQ,SAAS;EACxC;CACD;EACE,WAAW;EACX,MAAM,UAAU;EAChB,UAAU,YAAY,QAAQ,SAAS;EACxC;CACD;EACE,WAAW;EACX,MAAM,UAAU;EAChB,UAAU,YACR,QAAQ,SAAS,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,AAAQ,aAAa,MAAkB;AACrC,OAAK,SAAS,KAAK;GAAE,GAAG,UAAU;GAAM,GAAG,KAAK;GAAM,CAAC;AACvD,OAAK,QAAQ,MAAM,gBAAgB;AACjC,QAAK,OAAO;IACZ;;;CAIJ,AAAQ,kBAAkB,MAAoB,aAA6B;EACzE,MAAM,uBAAO,IAAI,KAAa;AAC9B,SAAO,YAAY,QAAQ,SAAS,QAAQ,UAAkB;GAC5D,MAAM,QAAQ,KAAK,gBAAgB,OAAO,MAAM,GAAG;AACnD,OAAI,CAAC,MAAO,QAAO;GACnB,MAAM,aAAc,MAAuB,sBAAsB,EAAE;GACnE,MAAM,aAAa,OAAO,QAAQ,WAAW,CAC1C,KAAK,CAAC,KAAK,WAAW,GAAG,IAAI,IAAI,MAAM,QAAQ,QAAQ,MAAM,GAAG,CAChE,KAAK,IAAI;GAEZ,MAAM,QAAQ,KAAK,IAAI,MAAM,GAAG,sBAAsB;AACtD,QAAK,IAAI,MAAM;AACf,UAAO,aAAa,IAAI,aAAa,UAAU;IAC/C;;;AAIN,SAAS,YAAY,MAAqB;CACxC,MAAM,SAAS,KAAK;AACpB,KAAI,CAAC,OAAQ;AACb,QAAO,WAAW,OAAO,SAAS,QAAQ,UAAU,UAAU,KAAK"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
const require_translate_dynamic = require('./translate-dynamic.cjs');
|
|
2
|
+
|
|
3
|
+
exports.configureDynamicTranslation = require_translate_dynamic.configureDynamicTranslation;
|
|
4
|
+
exports.tDynamic = require_translate_dynamic.tDynamic;
|
|
5
|
+
exports.tDynamicAll = require_translate_dynamic.tDynamicAll;
|
|
6
|
+
exports.tDynamicHtml = require_translate_dynamic.tDynamicHtml;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { DynamicTranslationConfig, DynamicTranslationOptions, configureDynamicTranslation, tDynamic, tDynamicAll, tDynamicHtml } from "./translate-dynamic.cjs";
|
|
2
|
+
export { type DynamicTranslationConfig, type DynamicTranslationOptions, configureDynamicTranslation, tDynamic, tDynamicAll, tDynamicHtml };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { DynamicTranslationConfig, DynamicTranslationOptions, configureDynamicTranslation, tDynamic, tDynamicAll, tDynamicHtml } from "./translate-dynamic.mjs";
|
|
2
|
+
export { type DynamicTranslationConfig, type DynamicTranslationOptions, configureDynamicTranslation, tDynamic, tDynamicAll, tDynamicHtml };
|