@weglot/translate-react 0.1.0 → 0.2.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/README.md +13 -4
- package/build/plugin/build-translator.cjs +4 -1
- package/build/plugin/build-translator.mjs +4 -1
- package/build/plugin/build-translator.mjs.map +1 -1
- package/build/plugin/next.cjs +1 -0
- package/build/plugin/next.d.cts.map +1 -1
- package/build/plugin/next.d.mts.map +1 -1
- package/build/plugin/next.mjs +1 -0
- package/build/plugin/next.mjs.map +1 -1
- package/build/plugin/vite.cjs +1 -1
- package/build/plugin/vite.mjs +1 -1
- package/build/plugin/vite.mjs.map +1 -1
- package/build/plugin/webpack.cjs +1 -1
- package/build/plugin/webpack.mjs +1 -1
- package/build/plugin/webpack.mjs.map +1 -1
- package/build/react/server/ServerWeglotProvider.d.cts +2 -2
- package/build/react/shared/WeglotProvider.d.cts +2 -2
- package/build/runtime/translate-dynamic.cjs +5 -3
- package/build/runtime/translate-dynamic.d.cts.map +1 -1
- package/build/runtime/translate-dynamic.d.mts.map +1 -1
- package/build/runtime/translate-dynamic.mjs +6 -4
- package/build/runtime/translate-dynamic.mjs.map +1 -1
- package/build/translators/translation-service.cjs +4 -2
- package/build/translators/translation-service.mjs +4 -2
- package/build/translators/translation-service.mjs.map +1 -1
- package/build/translators/weglot/api-client.cjs +75 -23
- package/build/translators/weglot/api-client.mjs +75 -24
- package/build/translators/weglot/api-client.mjs.map +1 -1
- package/build/translators/weglot/engine.cjs +2 -5
- package/build/translators/weglot/engine.mjs +2 -5
- package/build/translators/weglot/engine.mjs.map +1 -1
- package/build/translators/weglot/translator.cjs +31 -25
- package/build/translators/weglot/translator.mjs +31 -25
- package/build/translators/weglot/translator.mjs.map +1 -1
- package/build/types.d.cts +13 -3
- package/build/types.d.cts.map +1 -1
- package/build/types.d.mts +13 -3
- package/build/types.d.mts.map +1 -1
- package/build/utils/config-factory.cjs +18 -4
- package/build/utils/config-factory.mjs +19 -5
- package/build/utils/config-factory.mjs.map +1 -1
- package/build/virtual/code-generator.cjs +2 -0
- package/build/virtual/code-generator.mjs +2 -0
- package/build/virtual/code-generator.mjs.map +1 -1
- package/build/virtual/config.cjs +3 -0
- package/build/virtual/config.d.cts +3 -1
- package/build/virtual/config.d.cts.map +1 -1
- package/build/virtual/config.d.mts +3 -1
- package/build/virtual/config.d.mts.map +1 -1
- package/build/virtual/config.mjs +3 -1
- package/build/virtual/config.mjs.map +1 -1
- package/package.json +1 -1
package/build/types.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.cts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";;;;AA2BA;AAMA;
|
|
1
|
+
{"version":3,"file":"types.d.cts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";;;;AA2BA;AAMA;AAaA;AACO,UAtCU,YAAA,CAsCV;EAAc;;;;EADa,IAAA,EAAA,MAAA;EAAO;AAMzC;AAKA;;EA6CgB,MAAA,EAAA,MAAA;;;;;;AAkE4B,KA7IhC,uBAAA,GA6IgC;;UA7IoB;;;;;;KAMpD,oBAAA;;;;KAaA,mBAAA,GAAsB,QAChC,KAAK,cAAc;OACZ,QAAQ;;KAIL,iBAAA;;;;KAKA,YAAA;;;;;;;;;;;;;;;;;;;;;;;eA0BG;;;;;;;;;;;;;;;;;gBAmBC;;;;;;;;iBASC;;;cAIH,eAAe;;;;;;;;;;;;;;;;;;;;;;0BA2BH;;;;;;;;;;;;;;;;;;;;;;;qBA0BL"}
|
package/build/types.d.mts
CHANGED
|
@@ -29,7 +29,7 @@ type LocalePersistenceConfig = {
|
|
|
29
29
|
* Fields that are resolved internally (from the Weglot project settings or the
|
|
30
30
|
* environment) rather than provided by users.
|
|
31
31
|
*/
|
|
32
|
-
type WeglotInternalFields = "environment" | "cacheType" | "sourceLocale" | "targetLocales" | "apiLocales" | "translationEngine" | "weglotDir";
|
|
32
|
+
type WeglotInternalFields = "environment" | "cacheType" | "sourceLocale" | "targetLocales" | "apiLocales" | "translationEngine" | "apiBaseUrl" | "weglotDir";
|
|
33
33
|
/**
|
|
34
34
|
* Configuration for @weglot/translate-react
|
|
35
35
|
*/
|
|
@@ -92,9 +92,19 @@ type WeglotConfig = {
|
|
|
92
92
|
/** Public locale to the code the translate API expects, for languages with a
|
|
93
93
|
* custom code. Resolved from the project settings. @internal */
|
|
94
94
|
apiLocales: Record<string, LocaleCode>;
|
|
95
|
-
/** Inline elements allowed inside a segment, from the project settings
|
|
96
|
-
*
|
|
95
|
+
/** Inline elements allowed inside a segment, from the project settings.
|
|
96
|
+
* @internal */
|
|
97
97
|
translationEngine: number;
|
|
98
|
+
/** The project's regional API host, from the project settings. @internal */
|
|
99
|
+
apiBaseUrl: string;
|
|
100
|
+
/**
|
|
101
|
+
* Seconds to wait for translations Weglot is still generating. What is still
|
|
102
|
+
* missing then fails a `translate` build; in dev it shows up on a reload.
|
|
103
|
+
* Can be overridden by WEGLOT_TRANSLATION_TIMEOUT environment variable.
|
|
104
|
+
*
|
|
105
|
+
* @default 300 in a build, 10 in dev
|
|
106
|
+
*/
|
|
107
|
+
translationTimeout: number;
|
|
98
108
|
/**
|
|
99
109
|
* Whether to require 'use i18n' directive
|
|
100
110
|
*/
|
package/build/types.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";;;;AA2BA;AAMA;
|
|
1
|
+
{"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";;;;AA2BA;AAMA;AAaA;AACO,UAtCU,YAAA,CAsCV;EAAc;;;;EADa,IAAA,EAAA,MAAA;EAAO;AAMzC;AAKA;;EA6CgB,MAAA,EAAA,MAAA;;;;;;AAkE4B,KA7IhC,uBAAA,GA6IgC;;UA7IoB;;;;;;KAMpD,oBAAA;;;;KAaA,mBAAA,GAAsB,QAChC,KAAK,cAAc;OACZ,QAAQ;;KAIL,iBAAA;;;;KAKA,YAAA;;;;;;;;;;;;;;;;;;;;;;;eA0BG;;;;;;;;;;;;;;;;;gBAmBC;;;;;;;;iBASC;;;cAIH,eAAe;;;;;;;;;;;;;;;;;;;;;;0BA2BH;;;;;;;;;;;;;;;;;;;;;;;qBA0BL"}
|
|
@@ -24,6 +24,16 @@ const DEFAULT_CONFIG = {
|
|
|
24
24
|
},
|
|
25
25
|
buildMode: "translate"
|
|
26
26
|
};
|
|
27
|
+
const DEFAULT_TRANSLATION_TIMEOUT = {
|
|
28
|
+
production: 300,
|
|
29
|
+
development: 10
|
|
30
|
+
};
|
|
31
|
+
function timeoutFromEnv() {
|
|
32
|
+
const raw = process.env.WEGLOT_TRANSLATION_TIMEOUT;
|
|
33
|
+
if (!raw) return void 0;
|
|
34
|
+
const value = Number(raw);
|
|
35
|
+
return Number.isFinite(value) && value >= 0 ? value : void 0;
|
|
36
|
+
}
|
|
27
37
|
/**
|
|
28
38
|
* Create a LoaderConfig with defaults applied
|
|
29
39
|
*
|
|
@@ -43,6 +53,7 @@ function createWeglotConfig(options) {
|
|
|
43
53
|
cleanObject(cleanOptions);
|
|
44
54
|
const apiKey = cleanOptions.apiKey ?? process.env.WEGLOT_API_KEY;
|
|
45
55
|
if (!apiKey) throw new Error("Weglot project key is missing. Pass `apiKey` in the plugin options or set the WEGLOT_API_KEY environment variable.");
|
|
56
|
+
const environment = cleanOptions.environment ?? (process.env.NODE_ENV === "development" ? "development" : "production");
|
|
46
57
|
return {
|
|
47
58
|
...DEFAULT_CONFIG,
|
|
48
59
|
...cleanOptions,
|
|
@@ -50,8 +61,10 @@ function createWeglotConfig(options) {
|
|
|
50
61
|
sourceLocale: cleanOptions.sourceLocale ?? "",
|
|
51
62
|
targetLocales: cleanOptions.targetLocales ?? [],
|
|
52
63
|
apiLocales: cleanOptions.apiLocales ?? {},
|
|
53
|
-
translationEngine:
|
|
54
|
-
|
|
64
|
+
translationEngine: require_engine.resolveTranslationEngine(cleanOptions.translationEngine),
|
|
65
|
+
apiBaseUrl: cleanOptions.apiBaseUrl ?? "",
|
|
66
|
+
translationTimeout: cleanOptions.translationTimeout ?? timeoutFromEnv() ?? DEFAULT_TRANSLATION_TIMEOUT[environment],
|
|
67
|
+
environment,
|
|
55
68
|
cacheType: cleanOptions.cacheType ?? "local",
|
|
56
69
|
dev: {
|
|
57
70
|
...DEFAULT_CONFIG.dev,
|
|
@@ -73,12 +86,13 @@ function createWeglotConfig(options) {
|
|
|
73
86
|
* through Next.js loader options).
|
|
74
87
|
*/
|
|
75
88
|
async function resolveProjectLanguages(config, client = new require_api_client.WeglotApiClient(config.apiKey)) {
|
|
76
|
-
if (config.sourceLocale && config.targetLocales.length > 0) return;
|
|
89
|
+
if (config.sourceLocale && config.targetLocales.length > 0 && config.apiBaseUrl) return;
|
|
77
90
|
const settings = await client.fetchSettings();
|
|
78
91
|
config.sourceLocale = settings.sourceLocale;
|
|
79
92
|
config.targetLocales = settings.targetLocales;
|
|
80
93
|
config.apiLocales = settings.apiLocales;
|
|
81
|
-
config.
|
|
94
|
+
config.apiBaseUrl = settings.apiBaseUrl || require_api_client.DEFAULT_API_BASE_URL;
|
|
95
|
+
config.translationEngine = require_engine.resolveTranslationEngine(settings.translationEngine);
|
|
82
96
|
}
|
|
83
97
|
|
|
84
98
|
//#endregion
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { WeglotApiClient } from "../translators/weglot/api-client.mjs";
|
|
1
|
+
import { DEFAULT_API_BASE_URL, WeglotApiClient } from "../translators/weglot/api-client.mjs";
|
|
2
2
|
import { resolveTranslationEngine } from "../translators/weglot/engine.mjs";
|
|
3
3
|
import "dotenv/config";
|
|
4
4
|
|
|
@@ -21,6 +21,16 @@ const DEFAULT_CONFIG = {
|
|
|
21
21
|
},
|
|
22
22
|
buildMode: "translate"
|
|
23
23
|
};
|
|
24
|
+
const DEFAULT_TRANSLATION_TIMEOUT = {
|
|
25
|
+
production: 300,
|
|
26
|
+
development: 10
|
|
27
|
+
};
|
|
28
|
+
function timeoutFromEnv() {
|
|
29
|
+
const raw = process.env.WEGLOT_TRANSLATION_TIMEOUT;
|
|
30
|
+
if (!raw) return void 0;
|
|
31
|
+
const value = Number(raw);
|
|
32
|
+
return Number.isFinite(value) && value >= 0 ? value : void 0;
|
|
33
|
+
}
|
|
24
34
|
/**
|
|
25
35
|
* Create a LoaderConfig with defaults applied
|
|
26
36
|
*
|
|
@@ -40,6 +50,7 @@ function createWeglotConfig(options) {
|
|
|
40
50
|
cleanObject(cleanOptions);
|
|
41
51
|
const apiKey = cleanOptions.apiKey ?? process.env.WEGLOT_API_KEY;
|
|
42
52
|
if (!apiKey) throw new Error("Weglot project key is missing. Pass `apiKey` in the plugin options or set the WEGLOT_API_KEY environment variable.");
|
|
53
|
+
const environment = cleanOptions.environment ?? (process.env.NODE_ENV === "development" ? "development" : "production");
|
|
43
54
|
return {
|
|
44
55
|
...DEFAULT_CONFIG,
|
|
45
56
|
...cleanOptions,
|
|
@@ -47,8 +58,10 @@ function createWeglotConfig(options) {
|
|
|
47
58
|
sourceLocale: cleanOptions.sourceLocale ?? "",
|
|
48
59
|
targetLocales: cleanOptions.targetLocales ?? [],
|
|
49
60
|
apiLocales: cleanOptions.apiLocales ?? {},
|
|
50
|
-
translationEngine: cleanOptions.translationEngine
|
|
51
|
-
|
|
61
|
+
translationEngine: resolveTranslationEngine(cleanOptions.translationEngine),
|
|
62
|
+
apiBaseUrl: cleanOptions.apiBaseUrl ?? "",
|
|
63
|
+
translationTimeout: cleanOptions.translationTimeout ?? timeoutFromEnv() ?? DEFAULT_TRANSLATION_TIMEOUT[environment],
|
|
64
|
+
environment,
|
|
52
65
|
cacheType: cleanOptions.cacheType ?? "local",
|
|
53
66
|
dev: {
|
|
54
67
|
...DEFAULT_CONFIG.dev,
|
|
@@ -70,12 +83,13 @@ function createWeglotConfig(options) {
|
|
|
70
83
|
* through Next.js loader options).
|
|
71
84
|
*/
|
|
72
85
|
async function resolveProjectLanguages(config, client = new WeglotApiClient(config.apiKey)) {
|
|
73
|
-
if (config.sourceLocale && config.targetLocales.length > 0) return;
|
|
86
|
+
if (config.sourceLocale && config.targetLocales.length > 0 && config.apiBaseUrl) return;
|
|
74
87
|
const settings = await client.fetchSettings();
|
|
75
88
|
config.sourceLocale = settings.sourceLocale;
|
|
76
89
|
config.targetLocales = settings.targetLocales;
|
|
77
90
|
config.apiLocales = settings.apiLocales;
|
|
78
|
-
config.
|
|
91
|
+
config.apiBaseUrl = settings.apiBaseUrl || DEFAULT_API_BASE_URL;
|
|
92
|
+
config.translationEngine = resolveTranslationEngine(settings.translationEngine);
|
|
79
93
|
}
|
|
80
94
|
|
|
81
95
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config-factory.mjs","names":[],"sources":["../../src/utils/config-factory.ts"],"sourcesContent":["/**\n * Config factory for creating LoaderConfig instances\n */\nimport \"dotenv/config\";\nimport type {\n WeglotConfig,\n WeglotInternalFields,\n PartialWeglotConfig,\n} from \"../types\";\nimport {
|
|
1
|
+
{"version":3,"file":"config-factory.mjs","names":[],"sources":["../../src/utils/config-factory.ts"],"sourcesContent":["/**\n * Config factory for creating LoaderConfig instances\n */\nimport \"dotenv/config\";\nimport type {\n WeglotConfig,\n WeglotInternalFields,\n PartialWeglotConfig,\n} from \"../types\";\nimport {\n DEFAULT_API_BASE_URL,\n WeglotApiClient,\n} from \"../translators/weglot/api-client\";\nimport { resolveTranslationEngine } from \"../translators/weglot/engine\";\n\n/**\n * Default configuration values\n */\nexport const DEFAULT_CONFIG = {\n sourceRoot: \"src\",\n weglotDir: \"weglot\",\n useDirective: false,\n translatableAttributes: {},\n dev: {\n translationServerStartPort: 60000,\n },\n localePersistence: {\n type: \"cookie\" as const,\n config: {\n name: \"locale\",\n maxAge: 31536000,\n },\n },\n buildMode: \"translate\",\n} satisfies Omit<\n WeglotConfig,\n // weglotDir is an internal field for public-config purposes, but (unlike\n // the other internal fields) it still has a static default here rather\n // than being resolved at runtime, so it stays out of this Omit.\n \"apiKey\" | \"translationTimeout\" | Exclude<WeglotInternalFields, \"weglotDir\">\n>;\n\n// Dev stays short: a server component waits on it, and the client gives up at 30s.\nconst DEFAULT_TRANSLATION_TIMEOUT = { production: 300, development: 10 };\n\nfunction timeoutFromEnv(): number | undefined {\n const raw = process.env.WEGLOT_TRANSLATION_TIMEOUT;\n if (!raw) return undefined;\n const value = Number(raw);\n return Number.isFinite(value) && value >= 0 ? value : undefined;\n}\n\n/**\n * Create a LoaderConfig with defaults applied\n *\n * @param options - Partial config to override defaults\n * @returns Complete LoaderConfig with all defaults applied\n *\n */\nexport function createWeglotConfig(\n options: PartialWeglotConfig & Partial<Pick<WeglotConfig, WeglotInternalFields>>,\n): WeglotConfig {\n const cleanOptions = { ...options };\n\n const cleanObject = (obj: any) => {\n if (!obj || typeof obj !== \"object\") return;\n Object.keys(obj).forEach((key) => {\n if (obj[key] === undefined) {\n delete obj[key];\n } else if (\n obj[key] &&\n typeof obj[key] === \"object\" &&\n !Array.isArray(obj[key])\n ) {\n cleanObject(obj[key]);\n }\n });\n };\n\n cleanObject(cleanOptions);\n\n const apiKey = cleanOptions.apiKey ?? process.env.WEGLOT_API_KEY;\n if (!apiKey) {\n throw new Error(\n \"Weglot project key is missing. Pass `apiKey` in the plugin options or set the WEGLOT_API_KEY environment variable.\",\n );\n }\n\n const environment =\n cleanOptions.environment ??\n (process.env.NODE_ENV === \"development\" ? \"development\" : \"production\");\n\n return {\n ...DEFAULT_CONFIG,\n ...cleanOptions,\n apiKey,\n sourceLocale: cleanOptions.sourceLocale ?? \"\",\n targetLocales: cleanOptions.targetLocales ?? [],\n apiLocales: cleanOptions.apiLocales ?? {},\n translationEngine: resolveTranslationEngine(cleanOptions.translationEngine),\n apiBaseUrl: cleanOptions.apiBaseUrl ?? \"\",\n translationTimeout:\n cleanOptions.translationTimeout ??\n timeoutFromEnv() ??\n DEFAULT_TRANSLATION_TIMEOUT[environment],\n environment,\n cacheType: cleanOptions.cacheType ?? \"local\",\n dev: {\n ...DEFAULT_CONFIG.dev,\n ...cleanOptions.dev,\n },\n localePersistence: {\n ...DEFAULT_CONFIG.localePersistence,\n ...cleanOptions.localePersistence,\n config: {\n ...DEFAULT_CONFIG.localePersistence.config,\n ...cleanOptions.localePersistence?.config,\n },\n },\n };\n}\n\n/**\n * Fetches source/target locales from the Weglot project settings.\n * Idempotent: no-ops when locales are already resolved (e.g. passed\n * through Next.js loader options).\n */\nexport async function resolveProjectLanguages(\n config: WeglotConfig,\n client: WeglotApiClient = new WeglotApiClient(config.apiKey),\n): Promise<void> {\n if (\n config.sourceLocale &&\n config.targetLocales.length > 0 &&\n config.apiBaseUrl\n ) {\n return;\n }\n const settings = await client.fetchSettings();\n config.sourceLocale = settings.sourceLocale;\n config.targetLocales = settings.targetLocales;\n config.apiLocales = settings.apiLocales;\n config.apiBaseUrl = settings.apiBaseUrl || DEFAULT_API_BASE_URL;\n config.translationEngine = resolveTranslationEngine(\n settings.translationEngine,\n );\n}\n"],"mappings":";;;;;;;;AAkBA,MAAa,iBAAiB;CAC5B,YAAY;CACZ,WAAW;CACX,cAAc;CACd,wBAAwB,EAAE;CAC1B,KAAK,EACH,4BAA4B,KAC7B;CACD,mBAAmB;EACjB,MAAM;EACN,QAAQ;GACN,MAAM;GACN,QAAQ;GACT;EACF;CACD,WAAW;CACZ;AASD,MAAM,8BAA8B;CAAE,YAAY;CAAK,aAAa;CAAI;AAExE,SAAS,iBAAqC;CAC5C,MAAM,MAAM,QAAQ,IAAI;AACxB,KAAI,CAAC,IAAK,QAAO;CACjB,MAAM,QAAQ,OAAO,IAAI;AACzB,QAAO,OAAO,SAAS,MAAM,IAAI,SAAS,IAAI,QAAQ;;;;;;;;;AAUxD,SAAgB,mBACd,SACc;CACd,MAAM,eAAe,EAAE,GAAG,SAAS;CAEnC,MAAM,eAAe,QAAa;AAChC,MAAI,CAAC,OAAO,OAAO,QAAQ,SAAU;AACrC,SAAO,KAAK,IAAI,CAAC,SAAS,QAAQ;AAChC,OAAI,IAAI,SAAS,OACf,QAAO,IAAI;YAEX,IAAI,QACJ,OAAO,IAAI,SAAS,YACpB,CAAC,MAAM,QAAQ,IAAI,KAAK,CAExB,aAAY,IAAI,KAAK;IAEvB;;AAGJ,aAAY,aAAa;CAEzB,MAAM,SAAS,aAAa,UAAU,QAAQ,IAAI;AAClD,KAAI,CAAC,OACH,OAAM,IAAI,MACR,qHACD;CAGH,MAAM,cACJ,aAAa,gBACZ,QAAQ,IAAI,aAAa,gBAAgB,gBAAgB;AAE5D,QAAO;EACL,GAAG;EACH,GAAG;EACH;EACA,cAAc,aAAa,gBAAgB;EAC3C,eAAe,aAAa,iBAAiB,EAAE;EAC/C,YAAY,aAAa,cAAc,EAAE;EACzC,mBAAmB,yBAAyB,aAAa,kBAAkB;EAC3E,YAAY,aAAa,cAAc;EACvC,oBACE,aAAa,sBACb,gBAAgB,IAChB,4BAA4B;EAC9B;EACA,WAAW,aAAa,aAAa;EACrC,KAAK;GACH,GAAG,eAAe;GAClB,GAAG,aAAa;GACjB;EACD,mBAAmB;GACjB,GAAG,eAAe;GAClB,GAAG,aAAa;GAChB,QAAQ;IACN,GAAG,eAAe,kBAAkB;IACpC,GAAG,aAAa,mBAAmB;IACpC;GACF;EACF;;;;;;;AAQH,eAAsB,wBACpB,QACA,SAA0B,IAAI,gBAAgB,OAAO,OAAO,EAC7C;AACf,KACE,OAAO,gBACP,OAAO,cAAc,SAAS,KAC9B,OAAO,WAEP;CAEF,MAAM,WAAW,MAAM,OAAO,eAAe;AAC7C,QAAO,eAAe,SAAS;AAC/B,QAAO,gBAAgB,SAAS;AAChC,QAAO,aAAa,SAAS;AAC7B,QAAO,aAAa,SAAS,cAAc;AAC3C,QAAO,oBAAoB,yBACzB,SAAS,kBACV"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const require_api_client = require('../translators/weglot/api-client.cjs');
|
|
1
2
|
const require_path_helpers = require('../utils/path-helpers.cjs');
|
|
2
3
|
|
|
3
4
|
//#region src/virtual/code-generator.ts
|
|
@@ -13,6 +14,7 @@ export const cacheDir = ${JSON.stringify(cacheDir)};
|
|
|
13
14
|
export const sourceLocale = ${JSON.stringify(config.sourceLocale)};
|
|
14
15
|
export const apiLocales = ${JSON.stringify(config.apiLocales ?? {})};
|
|
15
16
|
export const translationEngine = ${JSON.stringify(config.translationEngine ?? 3)};
|
|
17
|
+
export const apiBaseUrl = ${JSON.stringify(config.apiBaseUrl || require_api_client.DEFAULT_API_BASE_URL)};
|
|
16
18
|
`;
|
|
17
19
|
}
|
|
18
20
|
/**
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DEFAULT_API_BASE_URL } from "../translators/weglot/api-client.mjs";
|
|
1
2
|
import { getCacheDir } from "../utils/path-helpers.mjs";
|
|
2
3
|
|
|
3
4
|
//#region src/virtual/code-generator.ts
|
|
@@ -13,6 +14,7 @@ export const cacheDir = ${JSON.stringify(cacheDir)};
|
|
|
13
14
|
export const sourceLocale = ${JSON.stringify(config.sourceLocale)};
|
|
14
15
|
export const apiLocales = ${JSON.stringify(config.apiLocales ?? {})};
|
|
15
16
|
export const translationEngine = ${JSON.stringify(config.translationEngine ?? 3)};
|
|
17
|
+
export const apiBaseUrl = ${JSON.stringify(config.apiBaseUrl || DEFAULT_API_BASE_URL)};
|
|
16
18
|
`;
|
|
17
19
|
}
|
|
18
20
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code-generator.mjs","names":[],"sources":["../../src/virtual/code-generator.ts"],"sourcesContent":["/**\n * Shared code generation for virtual modules\n * Used by both unplugin (virtual modules) and Turbopack loaders\n *\n * This module provides FULL module generators, not just function bodies.\n * Both loaders and unplugin use these to generate complete modules.\n */\n\nimport type { WeglotConfig } from \"../types\";\nimport { getCacheDir } from \"../utils/path-helpers\";\n\n/**\n * Generate complete config module\n * Exports serverUrl and cacheDir constants\n */\nexport function generateConfigModule(config: WeglotConfig): string {\n const serverUrl = config.dev.translationServerUrl;\n const cacheDir = getCacheDir(config);\n\n return `export const serverUrl = ${JSON.stringify(serverUrl)};\nexport const cacheDir = ${JSON.stringify(cacheDir)};\nexport const sourceLocale = ${JSON.stringify(config.sourceLocale)};\nexport const apiLocales = ${JSON.stringify(config.apiLocales ?? {})};\nexport const translationEngine = ${JSON.stringify(config.translationEngine ?? 3)};\n`;\n}\n\n/**\n * Generate complete locale/server module\n * Exports async getServerLocale() function\n */\nexport function generateServerLocaleModule(config: WeglotConfig): string {\n return `\nimport { createNextCookieLocaleResolver } from '@weglot/translate-react/react/next';\nexport const getServerLocale = createNextCookieLocaleResolver({ cookieConfig: ${JSON.stringify(config.localePersistence.config)}, defaultLocale: ${JSON.stringify(config.sourceLocale)} });\n`;\n}\n\n/**\n * Generate complete locale/client module\n * Exports getClientLocale() and persistLocale() functions\n */\nexport function generateClientLocaleModule(config: WeglotConfig): string {\n const cookieName = config.localePersistence.config.name;\n const maxAge = config.localePersistence.config.maxAge;\n\n return `\nexport function getClientLocale() {\n if (typeof document !== 'undefined') {\n const match = document.cookie.match(/${cookieName}=([^;]+)/);\n if (match) return match[1];\n }\n // Fallback to source locale\n return ${JSON.stringify(config.sourceLocale)};\n}\n\nexport function persistLocale(locale) {\n if (typeof document !== 'undefined') {\n document.cookie = \\`${cookieName}=\\${locale}; path=/; max-age=${maxAge}\\`;\n }\n}\n`;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"code-generator.mjs","names":[],"sources":["../../src/virtual/code-generator.ts"],"sourcesContent":["/**\n * Shared code generation for virtual modules\n * Used by both unplugin (virtual modules) and Turbopack loaders\n *\n * This module provides FULL module generators, not just function bodies.\n * Both loaders and unplugin use these to generate complete modules.\n */\n\nimport type { WeglotConfig } from \"../types\";\nimport { getCacheDir } from \"../utils/path-helpers\";\nimport { DEFAULT_API_BASE_URL } from \"../translators/weglot/api-client\";\n\n/**\n * Generate complete config module\n * Exports serverUrl and cacheDir constants\n */\nexport function generateConfigModule(config: WeglotConfig): string {\n const serverUrl = config.dev.translationServerUrl;\n const cacheDir = getCacheDir(config);\n\n return `export const serverUrl = ${JSON.stringify(serverUrl)};\nexport const cacheDir = ${JSON.stringify(cacheDir)};\nexport const sourceLocale = ${JSON.stringify(config.sourceLocale)};\nexport const apiLocales = ${JSON.stringify(config.apiLocales ?? {})};\nexport const translationEngine = ${JSON.stringify(config.translationEngine ?? 3)};\nexport const apiBaseUrl = ${JSON.stringify(config.apiBaseUrl || DEFAULT_API_BASE_URL)};\n`;\n}\n\n/**\n * Generate complete locale/server module\n * Exports async getServerLocale() function\n */\nexport function generateServerLocaleModule(config: WeglotConfig): string {\n return `\nimport { createNextCookieLocaleResolver } from '@weglot/translate-react/react/next';\nexport const getServerLocale = createNextCookieLocaleResolver({ cookieConfig: ${JSON.stringify(config.localePersistence.config)}, defaultLocale: ${JSON.stringify(config.sourceLocale)} });\n`;\n}\n\n/**\n * Generate complete locale/client module\n * Exports getClientLocale() and persistLocale() functions\n */\nexport function generateClientLocaleModule(config: WeglotConfig): string {\n const cookieName = config.localePersistence.config.name;\n const maxAge = config.localePersistence.config.maxAge;\n\n return `\nexport function getClientLocale() {\n if (typeof document !== 'undefined') {\n const match = document.cookie.match(/${cookieName}=([^;]+)/);\n if (match) return match[1];\n }\n // Fallback to source locale\n return ${JSON.stringify(config.sourceLocale)};\n}\n\nexport function persistLocale(locale) {\n if (typeof document !== 'undefined') {\n document.cookie = \\`${cookieName}=\\${locale}; path=/; max-age=${maxAge}\\`;\n }\n}\n`;\n}\n"],"mappings":";;;;;;;;AAgBA,SAAgB,qBAAqB,QAA8B;CACjE,MAAM,YAAY,OAAO,IAAI;CAC7B,MAAM,WAAW,YAAY,OAAO;AAEpC,QAAO,4BAA4B,KAAK,UAAU,UAAU,CAAC;0BACrC,KAAK,UAAU,SAAS,CAAC;8BACrB,KAAK,UAAU,OAAO,aAAa,CAAC;4BACtC,KAAK,UAAU,OAAO,cAAc,EAAE,CAAC,CAAC;mCACjC,KAAK,UAAU,OAAO,qBAAqB,EAAE,CAAC;4BACrD,KAAK,UAAU,OAAO,cAAc,qBAAqB,CAAC;;;;;;;AAQtF,SAAgB,2BAA2B,QAA8B;AACvE,QAAO;;gFAEuE,KAAK,UAAU,OAAO,kBAAkB,OAAO,CAAC,mBAAmB,KAAK,UAAU,OAAO,aAAa,CAAC;;;;;;;AAQvL,SAAgB,2BAA2B,QAA8B;CACvE,MAAM,aAAa,OAAO,kBAAkB,OAAO;CACnD,MAAM,SAAS,OAAO,kBAAkB,OAAO;AAE/C,QAAO;;;2CAGkC,WAAW;;;;WAI3C,KAAK,UAAU,OAAO,aAAa,CAAC;;;;;0BAKrB,WAAW,+BAA+B,OAAO"}
|
package/build/virtual/config.cjs
CHANGED
|
@@ -7,8 +7,11 @@ const sourceLocale = "__SOURCE_LOCALE__";
|
|
|
7
7
|
const apiLocales = {};
|
|
8
8
|
/** Which inline elements the project's engine permits inside a segment. */
|
|
9
9
|
const translationEngine = 3;
|
|
10
|
+
/** The project's regional API host. */
|
|
11
|
+
const apiBaseUrl = "https://api.weglot.com";
|
|
10
12
|
|
|
11
13
|
//#endregion
|
|
14
|
+
exports.apiBaseUrl = apiBaseUrl;
|
|
12
15
|
exports.apiLocales = apiLocales;
|
|
13
16
|
exports.cacheDir = cacheDir;
|
|
14
17
|
exports.serverUrl = serverUrl;
|
|
@@ -8,6 +8,8 @@ declare const sourceLocale: LocaleCode;
|
|
|
8
8
|
declare const apiLocales: Record<string, LocaleCode>;
|
|
9
9
|
/** Which inline elements the project's engine permits inside a segment. */
|
|
10
10
|
declare const translationEngine: number;
|
|
11
|
+
/** The project's regional API host. */
|
|
12
|
+
declare const apiBaseUrl: string;
|
|
11
13
|
//#endregion
|
|
12
|
-
export { apiLocales, cacheDir, serverUrl, sourceLocale, translationEngine };
|
|
14
|
+
export { apiBaseUrl, apiLocales, cacheDir, serverUrl, sourceLocale, translationEngine };
|
|
13
15
|
//# sourceMappingURL=config.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.cts","names":[],"sources":["../../src/virtual/config.ts"],"sourcesContent":[],"mappings":";;;cAEa,SAAA;cACA,QAAA;AADA,cAEA,YAFS,EAE6B,UAF7B;AACtB;AACa,cAGA,UAHsC,EAG1B,MAH0B,CAAA,MAAU,EAGrB,UAHqB,CAAA;AAG7D;AAGa,cAAA,iBAA6B,EAAA,MAAA"}
|
|
1
|
+
{"version":3,"file":"config.d.cts","names":[],"sources":["../../src/virtual/config.ts"],"sourcesContent":[],"mappings":";;;cAEa,SAAA;cACA,QAAA;AADA,cAEA,YAFS,EAE6B,UAF7B;AACtB;AACa,cAGA,UAHsC,EAG1B,MAH0B,CAAA,MAAU,EAGrB,UAHqB,CAAA;AAG7D;AAGa,cAAA,iBAA6B,EAAA,MAAA;AAG1C;cAAa"}
|
|
@@ -8,6 +8,8 @@ declare const sourceLocale: LocaleCode;
|
|
|
8
8
|
declare const apiLocales: Record<string, LocaleCode>;
|
|
9
9
|
/** Which inline elements the project's engine permits inside a segment. */
|
|
10
10
|
declare const translationEngine: number;
|
|
11
|
+
/** The project's regional API host. */
|
|
12
|
+
declare const apiBaseUrl: string;
|
|
11
13
|
//#endregion
|
|
12
|
-
export { apiLocales, cacheDir, serverUrl, sourceLocale, translationEngine };
|
|
14
|
+
export { apiBaseUrl, apiLocales, cacheDir, serverUrl, sourceLocale, translationEngine };
|
|
13
15
|
//# sourceMappingURL=config.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.mts","names":[],"sources":["../../src/virtual/config.ts"],"sourcesContent":[],"mappings":";;;cAEa,SAAA;cACA,QAAA;AADA,cAEA,YAFS,EAE6B,UAF7B;AACtB;AACa,cAGA,UAHsC,EAG1B,MAH0B,CAAA,MAAU,EAGrB,UAHqB,CAAA;AAG7D;AAGa,cAAA,iBAA6B,EAAA,MAAA"}
|
|
1
|
+
{"version":3,"file":"config.d.mts","names":[],"sources":["../../src/virtual/config.ts"],"sourcesContent":[],"mappings":";;;cAEa,SAAA;cACA,QAAA;AADA,cAEA,YAFS,EAE6B,UAF7B;AACtB;AACa,cAGA,UAHsC,EAG1B,MAH0B,CAAA,MAAU,EAGrB,UAHqB,CAAA;AAG7D;AAGa,cAAA,iBAA6B,EAAA,MAAA;AAG1C;cAAa"}
|
package/build/virtual/config.mjs
CHANGED
|
@@ -6,7 +6,9 @@ const sourceLocale = "__SOURCE_LOCALE__";
|
|
|
6
6
|
const apiLocales = {};
|
|
7
7
|
/** Which inline elements the project's engine permits inside a segment. */
|
|
8
8
|
const translationEngine = 3;
|
|
9
|
+
/** The project's regional API host. */
|
|
10
|
+
const apiBaseUrl = "https://api.weglot.com";
|
|
9
11
|
|
|
10
12
|
//#endregion
|
|
11
|
-
export { apiLocales, cacheDir, serverUrl, sourceLocale, translationEngine };
|
|
13
|
+
export { apiBaseUrl, apiLocales, cacheDir, serverUrl, sourceLocale, translationEngine };
|
|
12
14
|
//# sourceMappingURL=config.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.mjs","names":["apiLocales: Record<string, LocaleCode>","translationEngine: number"],"sources":["../../src/virtual/config.ts"],"sourcesContent":["import type { LocaleCode } from \"../locale\";\n\nexport const serverUrl = \"__SERVER_URL__\";\nexport const cacheDir = \"__CACHE_DIR__\";\nexport const sourceLocale = \"__SOURCE_LOCALE__\" as LocaleCode;\n\n/** Public locale to the language code the translate API expects. */\nexport const apiLocales: Record<string, LocaleCode> = {};\n\n/** Which inline elements the project's engine permits inside a segment. */\nexport const translationEngine: number = 3;\n"],"mappings":";AAEA,MAAa,YAAY;AACzB,MAAa,WAAW;AACxB,MAAa,eAAe;;AAG5B,MAAaA,aAAyC,EAAE;;AAGxD,MAAaC,oBAA4B"}
|
|
1
|
+
{"version":3,"file":"config.mjs","names":["apiLocales: Record<string, LocaleCode>","translationEngine: number","apiBaseUrl: string"],"sources":["../../src/virtual/config.ts"],"sourcesContent":["import type { LocaleCode } from \"../locale\";\n\nexport const serverUrl = \"__SERVER_URL__\";\nexport const cacheDir = \"__CACHE_DIR__\";\nexport const sourceLocale = \"__SOURCE_LOCALE__\" as LocaleCode;\n\n/** Public locale to the language code the translate API expects. */\nexport const apiLocales: Record<string, LocaleCode> = {};\n\n/** Which inline elements the project's engine permits inside a segment. */\nexport const translationEngine: number = 3;\n\n/** The project's regional API host. */\nexport const apiBaseUrl: string = \"https://api.weglot.com\";\n"],"mappings":";AAEA,MAAa,YAAY;AACzB,MAAa,WAAW;AACxB,MAAa,eAAe;;AAG5B,MAAaA,aAAyC,EAAE;;AAGxD,MAAaC,oBAA4B;;AAGzC,MAAaC,aAAqB"}
|
package/package.json
CHANGED