30 lines
767 B
JavaScript
30 lines
767 B
JavaScript
/**
|
|
* @typedef {import('./').Replacements} Replacements
|
|
*/
|
|
|
|
/**
|
|
* A simple translation stub to be used for multi-language support
|
|
* in diagrams. Can be easily replaced with a more sophisticated
|
|
* solution.
|
|
*
|
|
* @example
|
|
*
|
|
* // use it inside any diagram component by injecting `translate`.
|
|
*
|
|
* function MyService(translate) {
|
|
* alert(translate('HELLO {you}', { you: 'You!' }));
|
|
* }
|
|
*
|
|
* @param {string} template to interpolate
|
|
* @param {Replacements} [replacements] a map with substitutes
|
|
*
|
|
* @return {string} the translated string
|
|
*/
|
|
export default function translate(template, replacements) {
|
|
|
|
replacements = replacements || {};
|
|
|
|
return template.replace(/{([^}]+)}/g, function(_, key) {
|
|
return replacements[key] || '{' + key + '}';
|
|
});
|
|
} |