Skip to content

Commit bc41df3

Browse files
authored
Merge pull request #50 from OpenLMIS/OLMIS-7987-add-format-message-function
OLMIS-7987: Added missing formatMessage function which caused trouble with order create
2 parents 3d3170e + 403afa8 commit bc41df3

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/openlmis-i18n/message.service.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
var service = {
3838
getCurrentLocale: getCurrentLocale,
3939
populate: populate,
40-
get: get
40+
get: get,
41+
formatMessage: formatMessage
4142
};
4243

4344
return service;
@@ -107,6 +108,44 @@
107108
}
108109
return displayMessage;
109110
}
111+
112+
/**
113+
* @ngdoc method
114+
* @methodOf openlmis-i18n.messageService
115+
* @name formatMessage
116+
*
117+
* @description
118+
* Used in React components to get a translated message from the messageService.
119+
*
120+
* @param {String} key - key of the message
121+
* @param {Object} params - parameters to be used in the message
122+
*/
123+
function formatMessage(key, params) {
124+
var currentLocale = getCurrentLocale();
125+
var displayMessage = key;
126+
127+
if (OPENLMIS_MESSAGES[currentLocale] && OPENLMIS_MESSAGES[currentLocale][key]) {
128+
displayMessage = OPENLMIS_MESSAGES[currentLocale][key];
129+
130+
if (params) {
131+
//eslint-disable-next-line no-useless-escape
132+
var REPLACE_PLACEHOLDERS_REGEX = /\$\{([\s]*[^;\s\{]+[\s]*)\}/g;
133+
134+
displayMessage = displayMessage.replace(REPLACE_PLACEHOLDERS_REGEX, function(_, match) {
135+
var MISSING_PARAM = 'MISSING_PARAM';
136+
var paramValue = params[match.trim()];
137+
138+
return paramValue ? paramValue : MISSING_PARAM;
139+
});
140+
}
141+
142+
} else {
143+
// eslint-disable-next-line no-console
144+
console.error('[ERROR]: Translation message not found for: ' + key);
145+
}
146+
147+
return displayMessage;
148+
}
110149
}
111150

112151
})();

0 commit comments

Comments
 (0)