Skip to content

Commit

Permalink
fix(mess): add translation error messages
Browse files Browse the repository at this point in the history
closes #379
  • Loading branch information
Pewillia committed Nov 21, 2019
1 parent f1c9fde commit 87121ab
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 46 deletions.
6 changes: 5 additions & 1 deletion src/app/core/common.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ angular
* define common Javascript methods
* @function commonService
* @param {Object} $translate translation service Angular object
* @param {object} $rootScope Angular Object
* @param {Object} events Angular object
* @param {Object} $timeout promise with timeout Angular object
* @param {Object} constants modules that contain all the constants
* @return {Object} service common service
*/
function commonService($translate, events, $timeout, constants) {
function commonService($translate, $rootScope, events, $timeout, constants) {

const service = {
parseJSON,
Expand Down Expand Up @@ -116,6 +117,9 @@ function commonService($translate, events, $timeout, constants) {
function setLang(value) {
// show splash with language switch event as parameter
$translate.use(value).then(() => events.$broadcast(events.avShowSplash, events.avSwitchLanguage));

// trigger validation
$timeout(() => $rootScope.$broadcast(events.avValidateForm), constants.delaySetVersion);
}

/**
Expand Down
38 changes: 30 additions & 8 deletions src/app/ui/forms/form.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,20 +410,42 @@ function formService($timeout, $rootScope, events, $mdDialog, $translate, keyNam
* @function setErrorMessage
* @param {Object} form form object to get value from
* @param {String} message message to get from translation.csv
* @param {Array} variables variables to replace
* @param {Array} variables variables to replace
* @return {String} mess the updated message
*/
function setErrorMessage(form, message, variables) {
function setErrorMessage(form, message, variables=undefined) {
const errCode = [
{ error: 'number', mess: 'form.map.wkidnuminvalid' },
{ error: 'max', mess: 'form.map.layeropacitymaxerr' },
{ error: 'min', mess: 'form.map.extentdefxminerr' },
{ error: '302', mess: 'form.map.requirederr' },
{ error: '101', mess: 'form.map.extentdefxminerr' },
{ error: '103', mess: 'form.map.layeropacitymaxerr' }]

let mess = $translate.instant(message);

for (let variable of variables) {
let errorcode = form.error;
if (message !== '') {
mess = $translate.instant(message);
}
else {
mess = $translate.instant(errCode.filter( ({error}) => error === errorcode ).map( item => item.mess ).toString() ) ;
}

if (typeof variables !== 'undefined') {
for (let variable of variables) {
// get the replacing value from form object
let replace = form;
variable.split('.').map(item => { replace = replace[item] });
let replace = form;
variable.split('.').map(item => { replace = replace[item] });

// replace value in the message
mess = mess.replace(`{${variable}}`, replace);
// replace value in the message
mess = mess.replace(`{${variable}}`, replace);
}
}
else if (typeof variables === 'undefined') {
// replace values in message string if no parameters passed to replace them with
mess = mess.replace(/{.+?}/, ' ').replace(/{.+?}/, ' ');
mess = mess.replace("( )", ' ');
}

return mess;
}
Expand Down
Loading

0 comments on commit 87121ab

Please sign in to comment.