habitica/tasks/gulp-transifex-test.js

180 lines
5.3 KiB
JavaScript
Raw Normal View History

2015-08-07 03:42:24 +00:00
import fs from 'fs';
import _ from 'lodash';
import nconf from 'nconf';
import gulp from 'gulp';
import request from 'superagent';
nconf.argv().env().file({ file: 'config.json' });
const LOCALES = './common/locales/';
const ENGLISH_LOCALE = `${LOCALES}en/`;
2015-08-09 21:13:17 +00:00
const ALL_LANGUAGES = getArrayOfLanguages();
2015-08-07 03:42:24 +00:00
const SLACK_URL = nconf.get('TRANSIFEX_SLACK:url');
2015-08-09 21:44:34 +00:00
const SLACK_CHANNEL = '#' + (nconf.get('TRANSIFEX_SLACK:channel') || 'general');
2015-08-07 03:42:24 +00:00
const SLACK_USERNAME = 'Transifex';
const SLACK_EMOJI = ':transifex:';
2015-08-09 03:47:26 +00:00
gulp.task('transifex:missingFiles', () => {
let missingStrings = [];
2015-08-09 21:13:17 +00:00
eachTranslationFile(ALL_LANGUAGES, (error) => {
2015-08-09 03:47:26 +00:00
if(error) {
missingStrings.push(error.path);
}
});
if (!_.isEmpty(missingStrings)) {
let message = 'the following files were missing from the translations folder';
post(message, missingStrings);
}
});
2015-08-09 03:25:02 +00:00
gulp.task('transifex:missingStrings', () => {
2015-08-07 03:42:24 +00:00
2015-08-07 14:00:17 +00:00
let missingStrings = [];
2015-08-07 03:42:24 +00:00
2015-08-09 21:13:17 +00:00
eachTranslationString(ALL_LANGUAGES, (language, filename, key, englishString, translationString) => {
if (!translationString) {
2015-08-10 12:33:27 +00:00
let errorString = `${language} - ${filename} - ${key} - ${englishString}`;
2015-08-07 03:42:24 +00:00
missingStrings.push(errorString);
}
});
if (!_.isEmpty(missingStrings)) {
2015-08-07 14:00:17 +00:00
let message = 'The following strings are not translated';
post(message, missingStrings);
2015-08-07 03:42:24 +00:00
}
});
2015-08-09 21:43:04 +00:00
gulp.task('transifex:malformedStrings', () => {
let jsonFiles = stripOutNonJsonFiles(fs.readdirSync(ENGLISH_LOCALE));
let interpolationRegex = /<%= [a-zA-Z]* %>/g;
let stringsToLookFor = getStringsWith(jsonFiles, interpolationRegex);
let stringsWithMalformedInterpolations = [];
let stringsWithIncorrectNumberOfInterpolations = [];
let count = 0;
_(ALL_LANGUAGES).each(function(lang) {
_.each(stringsToLookFor, function(strings, file) {
let translationFile = fs.readFileSync(LOCALES + lang + '/' + file);
let parsedTranslationFile = JSON.parse(translationFile);
_.each(strings, function(value, key) {
let translationString = parsedTranslationFile[key];
if (!translationString) return;
let englishOccurences = stringsToLookFor[file][key];
let translationOccurences = translationString.match(interpolationRegex);
if (!translationOccurences) {
2015-08-10 12:33:27 +00:00
let malformedString = `${lang} - ${file} - ${key} - ${translationString}`;
2015-08-09 21:43:04 +00:00
stringsWithMalformedInterpolations.push(malformedString);
} else if (englishOccurences.length !== translationOccurences.length) {
2015-08-10 12:33:27 +00:00
let missingInterploationString = `${lang} - ${file} - ${key} - ${translationString}`;
2015-08-09 21:43:04 +00:00
stringsWithIncorrectNumberOfInterpolations.push(missingInterploationString);
}
});
});
});
if (!_.isEmpty(stringsWithMalformedInterpolations)) {
2015-08-10 12:53:26 +00:00
let message = 'The following strings have malformed or missing interpolations';
2015-08-09 21:43:04 +00:00
post(message, stringsWithMalformedInterpolations);
}
if (!_.isEmpty(stringsWithIncorrectNumberOfInterpolations)) {
2015-08-10 12:53:26 +00:00
let message = 'The following strings have a different number of string interpolations';
2015-08-09 21:43:04 +00:00
post(message, stringsWithIncorrectNumberOfInterpolations);
}
});
2015-08-07 14:00:17 +00:00
function getArrayOfLanguages() {
2015-08-07 03:42:24 +00:00
let languages = fs.readdirSync(LOCALES);
languages.shift(); // Remove README.md from array of languages
2015-08-07 14:00:17 +00:00
return languages;
}
function eachTranslationFile(languages, cb) {
2015-08-07 03:42:24 +00:00
let jsonFiles = stripOutNonJsonFiles(fs.readdirSync(ENGLISH_LOCALE));
_(languages).each((lang) => {
2015-08-07 14:00:17 +00:00
_.each(jsonFiles, (filename) => {
2015-08-09 03:46:51 +00:00
try {
var translationFile = fs.readFileSync(LOCALES + lang + '/' + filename);
var parsedTranslationFile = JSON.parse(translationFile);
} catch (err) {
return cb(err);
}
2015-08-07 03:42:24 +00:00
2015-08-07 14:00:17 +00:00
let englishFile = fs.readFileSync(ENGLISH_LOCALE + filename);
2015-08-07 03:42:24 +00:00
let parsedEnglishFile = JSON.parse(englishFile);
2015-08-09 03:46:51 +00:00
cb(null, lang, filename, parsedEnglishFile, parsedTranslationFile)
2015-08-07 03:42:24 +00:00
});
});
}
2015-08-07 14:00:17 +00:00
function eachTranslationString(languages, cb) {
2015-08-09 03:46:51 +00:00
eachTranslationFile(languages, (error, language, filename, englishJSON, translationJSON) => {
if (error) return;
2015-08-07 03:42:24 +00:00
_.each(englishJSON, (string, key) => {
var translationString = translationJSON[key];
2015-08-07 14:00:17 +00:00
cb(language, filename, key, string, translationString);
2015-08-07 03:42:24 +00:00
});
});
}
function post(message, items) {
let formattedMessage = formatMessageForPosting(message, items);
request.post(SLACK_URL)
.send({
channel: SLACK_CHANNEL,
username: SLACK_USERNAME,
text: formattedMessage,
icon_emoji: SLACK_EMOJI
})
.end((err, res) => {
if (err) console.error('Unable to post to slack', err);
});
}
function formatMessageForPosting(msg, items) {
let body = `*Warning:* ${msg}`;
2015-08-10 12:33:27 +00:00
body += '\n\n```\n';
2015-08-07 03:42:24 +00:00
body += items.join('\n');
2015-08-10 12:33:27 +00:00
body += '\n```';
2015-08-07 03:42:24 +00:00
return body;
}
2015-08-09 21:43:04 +00:00
function getStringsWith(json, interpolationRegex) {
var strings = {};
_(json).each(function(file_name) {
var raw_file = fs.readFileSync(ENGLISH_LOCALE + file_name);
var parsed_json = JSON.parse(raw_file);
strings[file_name] = {};
_.each(parsed_json, function(value, key) {
var match = value.match(interpolationRegex);
if(match) strings[file_name][key] = match;
});
});
return strings;
}
2015-08-07 03:42:24 +00:00
function stripOutNonJsonFiles(collection) {
let onlyJson = _.filter(collection, (file) => {
return file.match(/[a-zA-Z]*\.json/);
});
return onlyJson;
}