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.mjs
CHANGED
|
@@ -1251,6 +1251,14 @@ var Translate = class {
|
|
|
1251
1251
|
if (locale && this.parseComplete) this.translateUpdate(locale);
|
|
1252
1252
|
});
|
|
1253
1253
|
}
|
|
1254
|
+
/** Update interpolation params (e.g. a changed count) and re-render. Mirrors `Phrase.setParams`. */
|
|
1255
|
+
setParams(params) {
|
|
1256
|
+
this.options.params = params;
|
|
1257
|
+
if (!this.parseComplete) return;
|
|
1258
|
+
this.lastTranslatedLocale = "";
|
|
1259
|
+
const locale = currentlyLoadedLocale.get();
|
|
1260
|
+
if (locale) this.translateUpdate(locale);
|
|
1261
|
+
}
|
|
1254
1262
|
/** Stop reacting to locale changes. Safe to call multiple times. */
|
|
1255
1263
|
destroy() {
|
|
1256
1264
|
if (this.unsubscribeLocale) {
|
|
@@ -1263,7 +1271,7 @@ var Translate = class {
|
|
|
1263
1271
|
if (this.lastTranslatedLocale === currentLocale) return;
|
|
1264
1272
|
const { category = "" } = this.options;
|
|
1265
1273
|
if (this.tokens.length === 1) {
|
|
1266
|
-
this.element.innerText = LangsysApp.Translations.t(this.tokens[0], category);
|
|
1274
|
+
this.element.innerText = this.applyParams(LangsysApp.Translations.t(this.tokens[0], category));
|
|
1267
1275
|
} else {
|
|
1268
1276
|
this.translate(Array.from(this.element.childNodes));
|
|
1269
1277
|
this.lastTranslatedLocale = currentLocale;
|
|
@@ -1281,7 +1289,7 @@ var Translate = class {
|
|
|
1281
1289
|
this.tokens = tokens;
|
|
1282
1290
|
const { category = "" } = this.options;
|
|
1283
1291
|
if (this.tokens.length === 1) {
|
|
1284
|
-
this.element.innerText = LangsysApp.Translations.t(this.tokens[0], category);
|
|
1292
|
+
this.element.innerText = this.applyParams(LangsysApp.Translations.t(this.tokens[0], category));
|
|
1285
1293
|
} else {
|
|
1286
1294
|
const contentBlock = {
|
|
1287
1295
|
custom_id: "",
|
|
@@ -1317,7 +1325,7 @@ var Translate = class {
|
|
|
1317
1325
|
}
|
|
1318
1326
|
translate(nodes) {
|
|
1319
1327
|
const currentLocale = currentlyLoadedLocale.get();
|
|
1320
|
-
if (currentLocale === config.baseLocale && (this.lastTranslatedLocale === "" || this.lastTranslatedLocale === config.baseLocale)) {
|
|
1328
|
+
if (currentLocale === config.baseLocale && (this.lastTranslatedLocale === "" || this.lastTranslatedLocale === config.baseLocale) && isEmpty(this.options.params)) {
|
|
1321
1329
|
return;
|
|
1322
1330
|
}
|
|
1323
1331
|
nodes.forEach((node) => {
|
|
@@ -1340,9 +1348,10 @@ var Translate = class {
|
|
|
1340
1348
|
if (!contentToken) return;
|
|
1341
1349
|
const translation = this.getTranslation(contentToken);
|
|
1342
1350
|
if (translation && contentToken && node.originalNodeValue) {
|
|
1343
|
-
|
|
1351
|
+
const resolved = this.applyParams(translation);
|
|
1352
|
+
node.nodeValue = node.originalNodeValue.replace(contentToken, () => resolved);
|
|
1344
1353
|
} else if (node.originalNodeValue) {
|
|
1345
|
-
node.nodeValue = node.originalNodeValue;
|
|
1354
|
+
node.nodeValue = this.applyParams(node.originalNodeValue);
|
|
1346
1355
|
}
|
|
1347
1356
|
});
|
|
1348
1357
|
}
|
|
@@ -1351,6 +1360,16 @@ var Translate = class {
|
|
|
1351
1360
|
const { category = "" } = this.options;
|
|
1352
1361
|
return LangsysApp.Translations.lookupContent(category, this.custom_id, token);
|
|
1353
1362
|
}
|
|
1363
|
+
/**
|
|
1364
|
+
* Interpolate `{name}`-style params into a resolved text, matching `t()`:
|
|
1365
|
+
* unknown keys fall through untouched, and the untranslated fallback is
|
|
1366
|
+
* interpolated too. No-op when no params were supplied.
|
|
1367
|
+
*/
|
|
1368
|
+
applyParams(text) {
|
|
1369
|
+
const { params } = this.options;
|
|
1370
|
+
if (isEmpty(params)) return text;
|
|
1371
|
+
return interpolate(text, params, currentlyLoadedLocale.get());
|
|
1372
|
+
}
|
|
1354
1373
|
translateAttributes(element) {
|
|
1355
1374
|
const tagName = element.tagName.toLowerCase();
|
|
1356
1375
|
if (!element.originalAttributes) element.originalAttributes = {};
|
|
@@ -1377,7 +1396,7 @@ var Translate = class {
|
|
|
1377
1396
|
const originalText = optionEl.originalAttributes["textContent"].trim();
|
|
1378
1397
|
if (originalText) {
|
|
1379
1398
|
const translation = this.getTranslation(originalText);
|
|
1380
|
-
option.textContent = translation || optionEl.originalAttributes["textContent"];
|
|
1399
|
+
option.textContent = this.applyParams(translation || optionEl.originalAttributes["textContent"]);
|
|
1381
1400
|
}
|
|
1382
1401
|
});
|
|
1383
1402
|
}
|
|
@@ -1391,8 +1410,8 @@ var Translate = class {
|
|
|
1391
1410
|
const originalValue = element.originalAttributes[attr].trim();
|
|
1392
1411
|
if (!originalValue) return;
|
|
1393
1412
|
const translation = this.getTranslation(originalValue);
|
|
1394
|
-
if (translation) element.setAttribute(attr, translation);
|
|
1395
|
-
else element.setAttribute(attr, element.originalAttributes[attr]);
|
|
1413
|
+
if (translation) element.setAttribute(attr, this.applyParams(translation));
|
|
1414
|
+
else element.setAttribute(attr, this.applyParams(element.originalAttributes[attr]));
|
|
1396
1415
|
}
|
|
1397
1416
|
};
|
|
1398
1417
|
|