Pluralization in message nodes

I would like to ask your advise on the best practice for pluralization in message nodes? Currently I’m using Intl.PluralRules and doing something like this in the message node:

const pr = new Intl.PluralRules(context.currentLanguage);
const rule = pr.select(numberOfTransactions);
const subjectMap = new Map([
  ['one', 'Here is your'],
  ['other', 'Here are your'],
]);
const transactionMap = new Map([
  ['one', 'transaction'],
  ['other', 'transactions'],
]);
const fromDate = Intl.DateTimeFormat(context.currentLanguage, options).format(moment(datePeriod.fromDate));
const toDate = Intl.DateTimeFormat(context.currentLanguage options).format(moment(datePeriod.toDate));

text = `${subjectMap.get(rule)} ${numberOfTransactionsinWords} latest ${transactionMap.get(rule)} from ${fromDate} to ${toDate}`;
  1. Is there a better way to do pluralization, and
  2. Can Kore.ai support translation files instead of hardcoding the subjectMap and transactionMap?

Thank you,

Hi @pastapareo,

We infer from your explanation and code that you are trying to pluralize few values/words.
But, we would like to understand your use-case behind writing this code in a message node.

  • What would you like to print at the end?
  • Also, can this code be processed in a script node and then store the final value in a context variable to print it through a message node?
  • What is the current error that you are facing after executing the message node?

We will check with our internal teams if these methods are supported in Javascript tabs of message node and also in the script node.

Regards,
Yoga Ramya

Hi,

  • What would you like to print at the end?

I want to print something in this format based on the number of transactions and the entities:

Here [ is are ] your
[ five ] latest [ description ] [ transaction transactions ]
From your [ account ]
From [ from date ] to [ to date ]

Sample message #1: Here are your five latest transactions from your payroll account xxxx-1234 from June 6, 2020 to June 15, 2020.

Sample message #2: Here is your latest transaction from your payroll account xxxx-1234.


  • Also, can this code be processed in a script node and then store the final value in a context variable to print it through a message node?

Good point on moving to a script node, but the pluralizations are part of the messages and we want it such that if we need to customize the messages, as much as possible, we will only touch the message nodes.


  • What is the current error that you are facing after executing the message node?

Actually, I just tried it and it works. I just want to confirm if there is a better way to do pluralizations, maybe Kore has its own facility for it? Or can we externalize the pluralization map as json file.

Also i just noticed that the options parameter of Intl.DateTimeFormat is not working.

  const options = { year: 'numeric', month: 'long', day: 'numeric' };
  fromDate = new Intl.DateTimeFormat('en-GB', options).format(DateTime.now());

I’m expecting fromDate to be 5 September 2020 but it prints September 5, 2020

Hi @pastapareo,

We understand your requirement.
The platform gave the provision to write script as per the developer’s requirement as only majorly used functionalities are included at the platform level.
We appreciate your work and you can use the current script that you are already using as it is working as per your requirement.
Also, please refer to the following link of BotFunctions in Bot builder, Kindly check if it helps you in any way to make this code reusable in different message nodes.

Let us know if you need any further clarification.

Regards,
Yoga Ramya
https://developer.kore.ai/docs/bots/how-tos/how-to-use-bot-functions/