Skip to content

Commit

Permalink
fix automated testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Dutchman committed Mar 15, 2019
1 parent 12ecc35 commit 8e5f8b7
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 20 deletions.
Empty file modified LICENSE
100755 → 100644
Empty file.
Empty file modified README.md
100755 → 100644
Empty file.
36 changes: 27 additions & 9 deletions gulpfile.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*!
* ioBroker gulpfile
* Date: 2019-01-28
*/
"use strict";

const gulp = require("gulp");
Expand Down Expand Up @@ -30,7 +34,7 @@ function lang2data(lang, isFlat) {
if (isFlat) {
str += (lang[w] === "" ? (isFlat[w] || w) : lang[w]) + "\n";
} else {
const key = ' "' + w.replace(/"/g, '\\"') + '": ';
const key = ' "' + w.replace(/"/g, '\\"') + '": ';
str += key + '"' + lang[w].replace(/"/g, '\\"') + '",\n';
}
}
Expand All @@ -52,9 +56,9 @@ function readWordJs(src) {
} else {
words = fs.readFileSync(src + fileName).toString();
}

words = words.substring(words.indexOf("{"), words.length);
words = words.substring(0, words.lastIndexOf(";"));

const resultFunc = new Function("return " + words + ";");

return resultFunc();
Expand Down Expand Up @@ -328,7 +332,7 @@ function languages2words(src) {
writeWordJs(bigOne, src);
}

async function translateNotExisting(obj, baseText) {
async function translateNotExisting(obj, baseText, yandex) {
let t = obj["en"];
if (!t) {
t = baseText;
Expand All @@ -337,7 +341,9 @@ async function translateNotExisting(obj, baseText) {
if (t) {
for (let l in languages) {
if (!obj[l]) {
obj[l] = await translate(t, l);
const time = new Date().getTime();
obj[l] = await translate(t, l, yandex);
console.log("en -> " + l + " " + (new Date().getTime() - time) + " ms");
}
}
}
Expand Down Expand Up @@ -415,30 +421,42 @@ gulp.task("updateReadme", function (done) {
});

gulp.task("translate", async function (done) {

let yandex;
const i = process.argv.indexOf("--yandex");
if (i > -1) {
yandex = process.argv[i + 1];
}

if (iopackage && iopackage.common) {
if (iopackage.common.news) {
console.log("Translate News");
for (let k in iopackage.common.news) {
console.log("News: " + k);
let nw = iopackage.common.news[k];
await translateNotExisting(nw);
await translateNotExisting(nw, null, yandex);
}
}
if (iopackage.common.titleLang) {
await translateNotExisting(iopackage.common.titleLang, iopackage.common.title);
console.log("Translate Title");
await translateNotExisting(iopackage.common.titleLang, iopackage.common.title, yandex);
}
if (iopackage.common.desc) {
await translateNotExisting(iopackage.common.desc);
console.log("Translate Description");
await translateNotExisting(iopackage.common.desc, null, yandex);
}

if (fs.existsSync("./admin/i18n/en/translations.json")) {
let enTranslations = require("./admin/i18n/en/translations.json");
for (let l in languages) {
console.log("Translate Text: " + l);
let existing = {};
if (fs.existsSync("./admin/i18n/" + l + "/translations.json")) {
existing = require("./admin/i18n/" + l + "/translations.json");
}
for (let t in enTranslations) {
if (!existing[t]) {
existing[t] = await translate(enTranslations[t], l);
existing[t] = await translate(enTranslations[t], l, yandex);
}
}
if (!fs.existsSync("./admin/i18n/" + l + "/")) {
Expand All @@ -452,6 +470,6 @@ gulp.task("translate", async function (done) {
fs.writeFileSync("io-package.json", JSON.stringify(iopackage, null, 4));
});

gulp.task("translateAndUpdateWordsJS", gulp.series("translate", "adminLanguages2words"));
gulp.task("translateAndUpdateWordsJS", gulp.series("translate", "adminLanguages2words", "adminWords2languages"));

gulp.task("default", gulp.series("updatePackages", "updateReadme"));
1 change: 1 addition & 0 deletions io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"loglevel": "info",
"mode": "daemon",
"type": "network",
"license": "MIT",
"compact": true,
"materialize": true,
"dependencies": [
Expand Down
Empty file modified lib/adapter-config.d.ts
100755 → 100644
Empty file.
53 changes: 46 additions & 7 deletions lib/tools.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,61 @@ function isObject(it) {
* @returns {it is any[]}
*/
function isArray(it) {
if (Array.isArray != null) return Array.isArray(it);
if (typeof Array.isArray === "function") return Array.isArray(it);
return Object.prototype.toString.call(it) === "[object Array]";
}

/**
* Translates text using the Google Translate API
* Translates text to the target language. Automatically chooses the right translation API.
* @param {string} text The text to translate
* @param {string}targetLang The target languate
* @param {string} targetLang The target languate
* @param {string} [yandexApiKey] The yandex API key. You can create one for free at https://translate.yandex.com/developers
* @returns {Promise<string>}
*/
async function translateText(text, targetLang) {
if (targetLang === "en") return text;
async function translateText(text, targetLang, yandexApiKey) {
if (targetLang === "en") {
return text;
}
if (yandexApiKey) {
return await translateYandex(text, targetLang, yandexApiKey);
} else {
return await translateGoogle(text, targetLang);
}
}

/**
* Translates text with Yandex API
* @param {string} text The text to translate
* @param {string} targetLang The target languate
* @param {string} [apiKey] The yandex API key. You can create one for free at https://translate.yandex.com/developers
* @returns {Promise<string>}
*/
async function translateYandex(text, targetLang, apiKey) {
if (targetLang === "zh-cn") {
targetLang = "zh";
}
try {
const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${encodeURIComponent(text)}&lang=en-${targetLang}`;
const response = await axios({url, timeout: 15000});
if (response.data && response.data["text"]) {
return response.data["text"][0];
}
throw new Error("Invalid response for translate request");
} catch (e) {
throw new Error(`Could not translate to "${targetLang}": ${e}`);
}
}

/**
* Translates text with Google API
* @param {string} text The text to translate
* @param {string} targetLang The target languate
* @returns {Promise<string>}
*/
async function translateGoogle(text, targetLang) {
try {
const url = `http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=${targetLang}&dt=t&q=${encodeURIComponent(text)}&ie=UTF-8&oe=UTF-8`;
const response = await axios({url, timeout: 5000});
const response = await axios({url, timeout: 15000});
if (isArray(response.data)) {
// we got a valid response
return response.data[0][0][0];
Expand All @@ -42,7 +82,6 @@ async function translateText(text, targetLang) {
} catch (e) {
throw new Error(`Could not translate to "${targetLang}": ${e}`);
}
return text;
}

module.exports = {
Expand Down
File renamed without changes.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,31 @@
"@iobroker/adapter-core": "^1.0.3"
},
"devDependencies": {
"@iobroker/testing": "^1.1.16",
"@types/chai": "^4.1.7",
"@types/chai-as-promised": "^7.1.0",
"@types/gulp": "^4.0.6",
"@types/mocha": "^5.2.6",
"@types/node": "^10.14.1",
"@types/proxyquire": "^1.3.28",
"@types/sinon": "^7.0.9",
"@types/sinon": "^7.0.10",
"@types/sinon-chai": "^3.2.2",
"axios": "^0.18.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"eslint": "^5.15.1",
"gulp": "^4.0.0",
"mocha": "^5.2.0",
"mocha": "^6.0.2",
"proxyquire": "^2.1.0",
"sinon": "^7.2.7",
"sinon-chai": "^3.3.0"
},
"main": "main.js",
"scripts": {
"test:js": "mocha --opts test/mocha.custom.opts",
"test:package": "mocha test/testPackageFiles.js --exit",
"test:iobroker": "mocha test/testStartup.js --exit",
"test:package": "mocha test/package --exit",
"test:unit": "mocha test/unit --exit",
"test:integration": "mocha test/integration --exit",
"test": "npm run test:js && npm run test:package",
"lint": "npm run lint:js",
"lint:js": "eslint"
Expand Down
Empty file modified tsconfig.json
100755 → 100644
Empty file.

0 comments on commit 8e5f8b7

Please sign in to comment.