I need to transform all the labels in my application into label constants.
Some parts of the HTML contain dynamic content, such as
This label has '1' dynamic values
, where the '1'
can vary based on the component or a different application state.
To address this, I plan to define two separate values in my labelConstants:
label_1: 'This label has \'',
label_2: '\' dynamic values'
I will then combine these values like so:
let string = LabelConst.label_1 + '1' + LabelConst.label_2;
An alternative solution could involve using placeholders in the label constants:
label_1: 'This label has '%s' dynamic values'
In order to generate the label values, I would use a function to replace the %s
placeholder in my TypeScript code:
let requiredLabel = insertDynamicValues(LabelConst.label_1, '3');
The function insertDynamicValues()
would then substitute the placeholder and return
This label has '3' dynamic values
.
Is there a more efficient or better way to achieve this without relying on external libraries?