File tree Expand file tree Collapse file tree 1 file changed +40
-1
lines changed
Expand file tree Collapse file tree 1 file changed +40
-1
lines changed Original file line number Diff line number Diff line change 3737 var service = {
3838 getCurrentLocale : getCurrentLocale ,
3939 populate : populate ,
40- get : get
40+ get : get ,
41+ formatMessage : formatMessage
4142 } ;
4243
4344 return service ;
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} ) ( ) ;
You can’t perform that action at this time.
0 commit comments