From 403afa8daf0acf4ed69ada20c9e696936b9091b0 Mon Sep 17 00:00:00 2001 From: DominikNoga Date: Fri, 18 Oct 2024 13:35:10 +0200 Subject: [PATCH] OLMIS-7987: Added missing formatMessage function which caused troubles in order create --- src/openlmis-i18n/message.service.js | 41 +++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/openlmis-i18n/message.service.js b/src/openlmis-i18n/message.service.js index 3fe55568..364d01e0 100644 --- a/src/openlmis-i18n/message.service.js +++ b/src/openlmis-i18n/message.service.js @@ -37,7 +37,8 @@ var service = { getCurrentLocale: getCurrentLocale, populate: populate, - get: get + get: get, + formatMessage: formatMessage }; return service; @@ -107,6 +108,44 @@ } return displayMessage; } + + /** + * @ngdoc method + * @methodOf openlmis-i18n.messageService + * @name formatMessage + * + * @description + * Used in React components to get a translated message from the messageService. + * + * @param {String} key - key of the message + * @param {Object} params - parameters to be used in the message + */ + function formatMessage(key, params) { + var currentLocale = getCurrentLocale(); + var displayMessage = key; + + if (OPENLMIS_MESSAGES[currentLocale] && OPENLMIS_MESSAGES[currentLocale][key]) { + displayMessage = OPENLMIS_MESSAGES[currentLocale][key]; + + if (params) { + //eslint-disable-next-line no-useless-escape + var REPLACE_PLACEHOLDERS_REGEX = /\$\{([\s]*[^;\s\{]+[\s]*)\}/g; + + displayMessage = displayMessage.replace(REPLACE_PLACEHOLDERS_REGEX, function(_, match) { + var MISSING_PARAM = 'MISSING_PARAM'; + var paramValue = params[match.trim()]; + + return paramValue ? paramValue : MISSING_PARAM; + }); + } + + } else { + // eslint-disable-next-line no-console + console.error('[ERROR]: Translation message not found for: ' + key); + } + + return displayMessage; + } } })();