Skip to content

Commit

Permalink
feat: localize incomplete fallback message
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcy Sutton committed May 25, 2017
1 parent 9db63dc commit e38bec1
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 8 deletions.
16 changes: 15 additions & 1 deletion build/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,24 @@ function buildRules(grunt, options, commons, callback) {
function createFailureSummaryObject(summaries) {
var result = {};
summaries.forEach(function (summary) {
result[summary.type] = parseMetaData(summary, 'failureSummaries');
if (summary.type) {
result[summary.type] = parseMetaData(summary, 'failureSummaries');
}
});
return result;
}

function getIncompleteMsg(summaries) {
var result = {};
summaries.forEach(function(summary) {
if (summary.incompleteFallbackMessage) {
result = dot.template(summary.incompleteFallbackMessage).toString();
}
})
return result;
}


function replaceFunctions(string) {
return string.replace(/"(evaluate|after|gather|matches|source|commons)":\s*("[^"]+?")/g, function (m, p1, p2) {
return m.replace(p2, getSource(p2.replace(/^"|"$/g, ''), p1));
Expand Down Expand Up @@ -154,6 +167,7 @@ function buildRules(grunt, options, commons, callback) {

// Translate failureSummaries
metadata.failureSummaries = createFailureSummaryObject(result.misc);
metadata.incompleteFallbackMessage = getIncompleteMsg(result.misc);

callback({
auto: replaceFunctions(JSON.stringify({
Expand Down
4 changes: 4 additions & 0 deletions build/tasks/add-locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ module.exports = function (grunt) {
failureSummaries: result.misc.reduce(function (out, misc) {
out[misc.type] = misc.metadata;
return out;
}, {}),
incompleteFallbackMessage: result.misc.reduce(function (out, misc) {
out[misc.incompleteFallbackMessage] = misc.metadata;
return out;
}, {})
};

Expand Down
1 change: 1 addition & 0 deletions lib/core/base/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Audit.prototype._init = function () {
this.data.checks = (audit.data && audit.data.checks) || {};
this.data.rules = (audit.data && audit.data.rules) || {};
this.data.failureSummaries = (audit.data && audit.data.failureSummaries) || {};
this.data.incompleteFallbackMessage = (audit.data && audit.data.incompleteFallbackMessage || '');

this._constructHelpUrls(); // create default helpUrls
};
Expand Down
11 changes: 11 additions & 0 deletions lib/core/reporters/helpers/incomplete-fallback-msg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*global helpers */

/**
* Provides a fallback message in case incomplete checks don't provide one
* This mechanism allows the string to be localized.
* @return {String}
*/
helpers.incompleteFallbackMessage = function incompleteFallbackMessage() {
'use strict';
return axe._audit.data.incompleteFallbackMessage();
};
5 changes: 2 additions & 3 deletions lib/core/utils/publish-metadata.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

/*global helpers */
/**
* Construct incomplete message from check.data
* @param {Object} checkData Check result with reason specified
Expand All @@ -12,8 +12,7 @@ function getIncompleteReason(checkData, messages) {
// fall back to the default message if no reason specified
return messages.incomplete.default;
} else {
// TODO: localize it
return 'aXe couldn\'t tell the reason. Time to break out the element inspector!';
return helpers.incompleteFallbackMessage();
}
}
if (checkData && checkData.missingData) {
Expand Down
3 changes: 3 additions & 0 deletions lib/misc/incomplete-fallback.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"incompleteFallbackMessage": "aXe couldn't tell the reason. Time to break out the element inspector!"
}
3 changes: 2 additions & 1 deletion locales/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
"any": {
"failureMessage": "Gebruik een van de volgende oplossingen:{{~it:value}}\n {{=value.split('\\n').join('\\n ')}}{{~}}"
}
}
},
"incompleteFallbackMessage": "aXe kon de reden niet vertellen. Tijd om de element inspecteur uit te breken!"
}
21 changes: 21 additions & 0 deletions test/core/reporters/helpers/incomplete-fallback-msg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

describe('helpers.incompleteFallbackMessage', function() {
'use strict';
before(function() {
axe._load({
messages: {},
rules: [],
data: {
incompleteFallbackMessage: function anonymous() {
return 'Dogs are the best';
}
}
});
});

it('should return a string', function() {
var summary = helpers.incompleteFallbackMessage();
assert.equal(summary, 'Dogs are the best');
});

});
9 changes: 6 additions & 3 deletions test/core/utils/publish-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ describe('axe.utils.publishMetaData', function () {
axe._load({
rules: [],
data: {
incompleteFallbackMessage: function() {
return 'Dogs are the best';
},
rules: {
cats: {
help: function () {
Expand Down Expand Up @@ -374,19 +377,19 @@ describe('axe.utils.publishMetaData', function () {
any: [{
result: undefined,
id: 'cats-ANY',
message: 'aXe couldn\'t tell the reason. Time to break out the element inspector!',
message: 'Dogs are the best',
data: {}
}],
none: [{
result: undefined,
id: 'cats-NONE',
message: 'aXe couldn\'t tell the reason. Time to break out the element inspector!',
message: 'Dogs are the best',
data: {}
}],
all: [{
result: undefined,
id: 'cats-ALL',
message: 'aXe couldn\'t tell the reason. Time to break out the element inspector!',
message: 'Dogs are the best',
data: {}
}]
}]
Expand Down

0 comments on commit e38bec1

Please sign in to comment.