habitica/tasks/gulp-transifex-test.js

177 lines
5.4 KiB
JavaScript
Raw Normal View History

import fs from 'fs';
import _ from 'lodash';
import nconf from 'nconf';
import gulp from 'gulp';
import { postToSlack, conf } from './taskHelper';
const SLACK_CONFIG = {
channel: conf.get('TRANSIFEX_SLACK_CHANNEL'),
username: 'Transifex',
emoji: 'transifex'
}
2015-08-07 03:42:24 +00:00
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 malformedStringExceptions = {
messageDropFood: true,
armoireFood: true,
feedPet: true
}
2015-08-10 22:31:43 +00:00
gulp.task('transifex', ['transifex:missingFiles', 'transifex:missingStrings', 'transifex:malformedStrings']);
2015-08-09 03:47:26 +00:00
gulp.task('transifex:missingFiles', () => {
2015-08-10 22:17:27 +00:00
2015-08-09 03:47:26 +00:00
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';
let formattedMessage = formatMessageForPosting(message, missingStrings);
postToSlack(formattedMessage, SLACK_CONFIG);
2015-08-09 03:47:26 +00:00
}
});
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';
let formattedMessage = formatMessageForPosting(message, missingStrings);
postToSlack(formattedMessage, SLACK_CONFIG);
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 && !malformedStringExceptions[key]) {
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';
let formattedMessage = formatMessageForPosting(message, stringsWithMalformedInterpolations);
postToSlack(formattedMessage, SLACK_CONFIG);
2015-08-09 21:43:04 +00:00
}
if (!_.isEmpty(stringsWithIncorrectNumberOfInterpolations)) {
2015-08-10 12:53:26 +00:00
let message = 'The following strings have a different number of string interpolations';
let formattedMessage = formatMessageForPosting(message, stringsWithIncorrectNumberOfInterpolations);
postToSlack(formattedMessage, SLACK_CONFIG);
2015-08-09 21:43:04 +00:00
}
});
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 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;
}