langsys-js-typescript 0.3.0 → 0.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/dist/index.d.mts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +27 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +27 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -375,6 +375,8 @@ interface TranslateOptions {
|
|
|
375
375
|
custom_id?: string;
|
|
376
376
|
/** Optional human-readable label shown in the Translation Manager. */
|
|
377
377
|
label?: string;
|
|
378
|
+
/** Interpolation params — `{name}`, `{count}`, etc. Same single-brace syntax as `t()`. */
|
|
379
|
+
params?: Record<string, ParamPrimitive>;
|
|
378
380
|
}
|
|
379
381
|
/**
|
|
380
382
|
* Wrap a DOM element and manage translation for its contents.
|
|
@@ -399,6 +401,8 @@ declare class Translate {
|
|
|
399
401
|
private custom_id;
|
|
400
402
|
private unsubscribeLocale;
|
|
401
403
|
constructor(element: HTMLElement, options?: TranslateOptions);
|
|
404
|
+
/** Update interpolation params (e.g. a changed count) and re-render. Mirrors `Phrase.setParams`. */
|
|
405
|
+
setParams(params: Record<string, ParamPrimitive> | undefined): void;
|
|
402
406
|
/** Stop reacting to locale changes. Safe to call multiple times. */
|
|
403
407
|
destroy(): void;
|
|
404
408
|
private translateUpdate;
|
|
@@ -406,6 +410,12 @@ declare class Translate {
|
|
|
406
410
|
private handleContentBlock;
|
|
407
411
|
private translate;
|
|
408
412
|
private getTranslation;
|
|
413
|
+
/**
|
|
414
|
+
* Interpolate `{name}`-style params into a resolved text, matching `t()`:
|
|
415
|
+
* unknown keys fall through untouched, and the untranslated fallback is
|
|
416
|
+
* interpolated too. No-op when no params were supplied.
|
|
417
|
+
*/
|
|
418
|
+
private applyParams;
|
|
409
419
|
private translateAttributes;
|
|
410
420
|
private translateAttribute;
|
|
411
421
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -375,6 +375,8 @@ interface TranslateOptions {
|
|
|
375
375
|
custom_id?: string;
|
|
376
376
|
/** Optional human-readable label shown in the Translation Manager. */
|
|
377
377
|
label?: string;
|
|
378
|
+
/** Interpolation params — `{name}`, `{count}`, etc. Same single-brace syntax as `t()`. */
|
|
379
|
+
params?: Record<string, ParamPrimitive>;
|
|
378
380
|
}
|
|
379
381
|
/**
|
|
380
382
|
* Wrap a DOM element and manage translation for its contents.
|
|
@@ -399,6 +401,8 @@ declare class Translate {
|
|
|
399
401
|
private custom_id;
|
|
400
402
|
private unsubscribeLocale;
|
|
401
403
|
constructor(element: HTMLElement, options?: TranslateOptions);
|
|
404
|
+
/** Update interpolation params (e.g. a changed count) and re-render. Mirrors `Phrase.setParams`. */
|
|
405
|
+
setParams(params: Record<string, ParamPrimitive> | undefined): void;
|
|
402
406
|
/** Stop reacting to locale changes. Safe to call multiple times. */
|
|
403
407
|
destroy(): void;
|
|
404
408
|
private translateUpdate;
|
|
@@ -406,6 +410,12 @@ declare class Translate {
|
|
|
406
410
|
private handleContentBlock;
|
|
407
411
|
private translate;
|
|
408
412
|
private getTranslation;
|
|
413
|
+
/**
|
|
414
|
+
* Interpolate `{name}`-style params into a resolved text, matching `t()`:
|
|
415
|
+
* unknown keys fall through untouched, and the untranslated fallback is
|
|
416
|
+
* interpolated too. No-op when no params were supplied.
|
|
417
|
+
*/
|
|
418
|
+
private applyParams;
|
|
409
419
|
private translateAttributes;
|
|
410
420
|
private translateAttribute;
|
|
411
421
|
}
|
package/dist/index.js
CHANGED
|
@@ -1253,6 +1253,14 @@ var Translate = class {
|
|
|
1253
1253
|
if (locale && this.parseComplete) this.translateUpdate(locale);
|
|
1254
1254
|
});
|
|
1255
1255
|
}
|
|
1256
|
+
/** Update interpolation params (e.g. a changed count) and re-render. Mirrors `Phrase.setParams`. */
|
|
1257
|
+
setParams(params) {
|
|
1258
|
+
this.options.params = params;
|
|
1259
|
+
if (!this.parseComplete) return;
|
|
1260
|
+
this.lastTranslatedLocale = "";
|
|
1261
|
+
const locale = currentlyLoadedLocale.get();
|
|
1262
|
+
if (locale) this.translateUpdate(locale);
|
|
1263
|
+
}
|
|
1256
1264
|
/** Stop reacting to locale changes. Safe to call multiple times. */
|
|
1257
1265
|
destroy() {
|
|
1258
1266
|
if (this.unsubscribeLocale) {
|
|
@@ -1265,7 +1273,7 @@ var Translate = class {
|
|
|
1265
1273
|
if (this.lastTranslatedLocale === currentLocale) return;
|
|
1266
1274
|
const { category = "" } = this.options;
|
|
1267
1275
|
if (this.tokens.length === 1) {
|
|
1268
|
-
this.element.innerText = LangsysApp.Translations.t(this.tokens[0], category);
|
|
1276
|
+
this.element.innerText = this.applyParams(LangsysApp.Translations.t(this.tokens[0], category));
|
|
1269
1277
|
} else {
|
|
1270
1278
|
this.translate(Array.from(this.element.childNodes));
|
|
1271
1279
|
this.lastTranslatedLocale = currentLocale;
|
|
@@ -1283,7 +1291,7 @@ var Translate = class {
|
|
|
1283
1291
|
this.tokens = tokens;
|
|
1284
1292
|
const { category = "" } = this.options;
|
|
1285
1293
|
if (this.tokens.length === 1) {
|
|
1286
|
-
this.element.innerText = LangsysApp.Translations.t(this.tokens[0], category);
|
|
1294
|
+
this.element.innerText = this.applyParams(LangsysApp.Translations.t(this.tokens[0], category));
|
|
1287
1295
|
} else {
|
|
1288
1296
|
const contentBlock = {
|
|
1289
1297
|
custom_id: "",
|
|
@@ -1319,7 +1327,7 @@ var Translate = class {
|
|
|
1319
1327
|
}
|
|
1320
1328
|
translate(nodes) {
|
|
1321
1329
|
const currentLocale = currentlyLoadedLocale.get();
|
|
1322
|
-
if (currentLocale === config.baseLocale && (this.lastTranslatedLocale === "" || this.lastTranslatedLocale === config.baseLocale)) {
|
|
1330
|
+
if (currentLocale === config.baseLocale && (this.lastTranslatedLocale === "" || this.lastTranslatedLocale === config.baseLocale) && isEmpty(this.options.params)) {
|
|
1323
1331
|
return;
|
|
1324
1332
|
}
|
|
1325
1333
|
nodes.forEach((node) => {
|
|
@@ -1342,9 +1350,10 @@ var Translate = class {
|
|
|
1342
1350
|
if (!contentToken) return;
|
|
1343
1351
|
const translation = this.getTranslation(contentToken);
|
|
1344
1352
|
if (translation && contentToken && node.originalNodeValue) {
|
|
1345
|
-
|
|
1353
|
+
const resolved = this.applyParams(translation);
|
|
1354
|
+
node.nodeValue = node.originalNodeValue.replace(contentToken, () => resolved);
|
|
1346
1355
|
} else if (node.originalNodeValue) {
|
|
1347
|
-
node.nodeValue = node.originalNodeValue;
|
|
1356
|
+
node.nodeValue = this.applyParams(node.originalNodeValue);
|
|
1348
1357
|
}
|
|
1349
1358
|
});
|
|
1350
1359
|
}
|
|
@@ -1353,6 +1362,16 @@ var Translate = class {
|
|
|
1353
1362
|
const { category = "" } = this.options;
|
|
1354
1363
|
return LangsysApp.Translations.lookupContent(category, this.custom_id, token);
|
|
1355
1364
|
}
|
|
1365
|
+
/**
|
|
1366
|
+
* Interpolate `{name}`-style params into a resolved text, matching `t()`:
|
|
1367
|
+
* unknown keys fall through untouched, and the untranslated fallback is
|
|
1368
|
+
* interpolated too. No-op when no params were supplied.
|
|
1369
|
+
*/
|
|
1370
|
+
applyParams(text) {
|
|
1371
|
+
const { params } = this.options;
|
|
1372
|
+
if (isEmpty(params)) return text;
|
|
1373
|
+
return interpolate(text, params, currentlyLoadedLocale.get());
|
|
1374
|
+
}
|
|
1356
1375
|
translateAttributes(element) {
|
|
1357
1376
|
const tagName = element.tagName.toLowerCase();
|
|
1358
1377
|
if (!element.originalAttributes) element.originalAttributes = {};
|
|
@@ -1379,7 +1398,7 @@ var Translate = class {
|
|
|
1379
1398
|
const originalText = optionEl.originalAttributes["textContent"].trim();
|
|
1380
1399
|
if (originalText) {
|
|
1381
1400
|
const translation = this.getTranslation(originalText);
|
|
1382
|
-
option.textContent = translation || optionEl.originalAttributes["textContent"];
|
|
1401
|
+
option.textContent = this.applyParams(translation || optionEl.originalAttributes["textContent"]);
|
|
1383
1402
|
}
|
|
1384
1403
|
});
|
|
1385
1404
|
}
|
|
@@ -1393,8 +1412,8 @@ var Translate = class {
|
|
|
1393
1412
|
const originalValue = element.originalAttributes[attr].trim();
|
|
1394
1413
|
if (!originalValue) return;
|
|
1395
1414
|
const translation = this.getTranslation(originalValue);
|
|
1396
|
-
if (translation) element.setAttribute(attr, translation);
|
|
1397
|
-
else element.setAttribute(attr, element.originalAttributes[attr]);
|
|
1415
|
+
if (translation) element.setAttribute(attr, this.applyParams(translation));
|
|
1416
|
+
else element.setAttribute(attr, this.applyParams(element.originalAttributes[attr]));
|
|
1398
1417
|
}
|
|
1399
1418
|
};
|
|
1400
1419
|
|