langsys-js-typescript 0.6.3 → 0.6.4

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.mjs CHANGED
@@ -287,14 +287,76 @@ function warnUnmatchedParams(source, texts, params, context) {
287
287
  }
288
288
  function interpolate(template, params, locale) {
289
289
  if (isICU(template)) {
290
- try {
291
- return new IntlMessageFormat(template, locale || "en").format(params);
292
- } catch {
293
- return simpleInterpolate(template, params, locale);
290
+ const resolved = locale || "en";
291
+ const hasNullParam = !!params && Object.values(params).some((value) => value === null);
292
+ if (!hasNullParam) {
293
+ try {
294
+ return new IntlMessageFormat(template, resolved).format(params);
295
+ } catch {
296
+ }
297
+ }
298
+ {
299
+ try {
300
+ const ast = new IntlMessageFormat(template, resolved).ast;
301
+ if (!Array.isArray(ast)) return simpleInterpolate(template, params, locale);
302
+ const recovered = _recoverMissingArgs(JSON.parse(JSON.stringify(ast)), params);
303
+ return new IntlMessageFormat(recovered, resolved).format(params);
304
+ } catch {
305
+ return simpleInterpolate(template, params, locale);
306
+ }
294
307
  }
295
308
  }
296
309
  return simpleInterpolate(template, params, locale);
297
310
  }
311
+ var ICU_LITERAL = 0;
312
+ var ICU_ARGUMENT = 1;
313
+ var ICU_NUMBER = 2;
314
+ var ICU_DATE = 3;
315
+ var ICU_TIME = 4;
316
+ var ICU_SELECT = 5;
317
+ var ICU_PLURAL = 6;
318
+ var ICU_POUND = 7;
319
+ var ICU_TAG = 8;
320
+ function _recoverMissingArgs(nodes, params) {
321
+ const out = [];
322
+ for (const node of nodes) {
323
+ const name = node.value;
324
+ const value = name !== void 0 && name in params ? params[name] : void 0;
325
+ const supplied = value !== void 0 && value !== null;
326
+ if (node.type === ICU_TAG) {
327
+ out.push({ ...node, children: _recoverMissingArgs(node.children ?? [], params) });
328
+ continue;
329
+ }
330
+ if (node.type === ICU_SELECT || node.type === ICU_PLURAL) {
331
+ if (supplied) {
332
+ const options = {};
333
+ for (const [key, branch2] of Object.entries(node.options ?? {})) {
334
+ options[key] = { value: _recoverMissingArgs(branch2.value, params) };
335
+ }
336
+ out.push({ ...node, options });
337
+ continue;
338
+ }
339
+ const other = node.options?.other;
340
+ if (!other) {
341
+ out.push(node);
342
+ continue;
343
+ }
344
+ const branch = _recoverMissingArgs(other.value, params);
345
+ out.push(
346
+ ...branch.map(
347
+ (child) => child.type === ICU_POUND ? { type: ICU_LITERAL, value: `{${name}}` } : child
348
+ )
349
+ );
350
+ continue;
351
+ }
352
+ if (!supplied && (node.type === ICU_ARGUMENT || node.type === ICU_NUMBER || node.type === ICU_DATE || node.type === ICU_TIME)) {
353
+ out.push({ type: ICU_LITERAL, value: `{${name}}` });
354
+ continue;
355
+ }
356
+ out.push(node);
357
+ }
358
+ return out;
359
+ }
298
360
  function simpleInterpolate(template, params, locale) {
299
361
  return template.replace(/\{([^{},]+)\}/g, (match, rawKey) => {
300
362
  const key = rawKey.trim();