From e38bec1a9c83f685169d65ab3e0372661b511bf1 Mon Sep 17 00:00:00 2001 From: Marcy Sutton Date: Thu, 25 May 2017 16:42:33 -0700 Subject: [PATCH] feat: localize incomplete fallback message --- build/configure.js | 16 +++++++++++++- build/tasks/add-locale.js | 4 ++++ lib/core/base/audit.js | 1 + .../helpers/incomplete-fallback-msg.js | 11 ++++++++++ lib/core/utils/publish-metadata.js | 5 ++--- lib/misc/incomplete-fallback.json | 3 +++ locales/nl.json | 3 ++- .../helpers/incomplete-fallback-msg.js | 21 +++++++++++++++++++ test/core/utils/publish-metadata.js | 9 +++++--- 9 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 lib/core/reporters/helpers/incomplete-fallback-msg.js create mode 100644 lib/misc/incomplete-fallback.json create mode 100644 test/core/reporters/helpers/incomplete-fallback-msg.js diff --git a/build/configure.js b/build/configure.js index 9c1153551a..e16851722a 100644 --- a/build/configure.js +++ b/build/configure.js @@ -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)); @@ -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({ diff --git a/build/tasks/add-locale.js b/build/tasks/add-locale.js index 14ff83ca42..a7ec59f3f1 100644 --- a/build/tasks/add-locale.js +++ b/build/tasks/add-locale.js @@ -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; }, {}) }; diff --git a/lib/core/base/audit.js b/lib/core/base/audit.js index a89817a213..1c6432eaf1 100644 --- a/lib/core/base/audit.js +++ b/lib/core/base/audit.js @@ -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 }; diff --git a/lib/core/reporters/helpers/incomplete-fallback-msg.js b/lib/core/reporters/helpers/incomplete-fallback-msg.js new file mode 100644 index 0000000000..4fe4b61509 --- /dev/null +++ b/lib/core/reporters/helpers/incomplete-fallback-msg.js @@ -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(); +}; \ No newline at end of file diff --git a/lib/core/utils/publish-metadata.js b/lib/core/utils/publish-metadata.js index 6bd47a7c76..a0eff0ccba 100644 --- a/lib/core/utils/publish-metadata.js +++ b/lib/core/utils/publish-metadata.js @@ -1,4 +1,4 @@ - +/*global helpers */ /** * Construct incomplete message from check.data * @param {Object} checkData Check result with reason specified @@ -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) { diff --git a/lib/misc/incomplete-fallback.json b/lib/misc/incomplete-fallback.json new file mode 100644 index 0000000000..4db8db68e7 --- /dev/null +++ b/lib/misc/incomplete-fallback.json @@ -0,0 +1,3 @@ +{ + "incompleteFallbackMessage": "aXe couldn't tell the reason. Time to break out the element inspector!" +} diff --git a/locales/nl.json b/locales/nl.json index a0fa057cf4..9cc0e29b0c 100644 --- a/locales/nl.json +++ b/locales/nl.json @@ -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!" } \ No newline at end of file diff --git a/test/core/reporters/helpers/incomplete-fallback-msg.js b/test/core/reporters/helpers/incomplete-fallback-msg.js new file mode 100644 index 0000000000..7852886cd0 --- /dev/null +++ b/test/core/reporters/helpers/incomplete-fallback-msg.js @@ -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'); + }); + +}); diff --git a/test/core/utils/publish-metadata.js b/test/core/utils/publish-metadata.js index fa311a89de..4f857aa845 100644 --- a/test/core/utils/publish-metadata.js +++ b/test/core/utils/publish-metadata.js @@ -300,6 +300,9 @@ describe('axe.utils.publishMetaData', function () { axe._load({ rules: [], data: { + incompleteFallbackMessage: function() { + return 'Dogs are the best'; + }, rules: { cats: { help: function () { @@ -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: {} }] }]