Skip to content

OLMIS-7987: Added missing formatMessage function which caused trouble with order create #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/openlmis-i18n/message.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
var service = {
getCurrentLocale: getCurrentLocale,
populate: populate,
get: get
get: get,
formatMessage: formatMessage
};

return service;
Expand Down Expand Up @@ -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;
}
}

})();
Loading