Localization of a format message with a value

In my localization, I have a message like the following:

 'Welcome, {0}' 

where {0} refers to the name of the username.

Is there a string format function in the Syncfusion JavaScript library which allows me to format the localized message and pass a value? Something like:


const instance = new L10n('common', { welcomeMessage: 'Welcome {0}' });

const welcomeMessageFormatString = instance.getConstant('welcomeMessage');

const messageToDisplay = stringFormat(welcomeMessageFormatString, userName);



1 Reply

DA Deepika Arumugasamy Syncfusion Team February 19, 2025 03:46 PM UTC

Hi AnilaCar,

Thank you for reaching out!

 

Currently, Syncfusion does not provide a built-in stringFormat function to dynamically replace placeholders.

 

However, you can achieve this functionality by implementing a custom string formatting function explicitly, as shown below:

 

code


function stringFormat(format: string, ...args: string[]): string {

    return format.replace(/{(\d+)}/g, (match, index) => args[index] || match);
}

 

const instance = new L10n('common', { welcomeMessage: 'Welcome, {0}' }, 'en-US');
const welcomeMessageFormatString = instance.getConstant('welcomeMessage');
const messageToDisplay = stringFormat(welcomeMessageFormatString, userName);

 

console.log(messageToDisplay); // Output: "Welcome, John"




This approach ensures that your localized strings remain dynamic and can be formatted with user-specific values.

Let us know if you need any further assistance!



Regards,
Deepika


Loader.
Up arrow icon