From 2bc4c6ff7af2947974f4ee8b9286fd6806fff63d Mon Sep 17 00:00:00 2001 From: gabrielburnworth Date: Tue, 15 Aug 2017 05:06:25 -0700 Subject: [PATCH] restore language helper and edit README --- README.md | 9 + config/webpack.dev.js | 5 +- public/app-resources/languages/_helper.js | 294 +++++++++++----------- 3 files changed, 160 insertions(+), 148 deletions(-) diff --git a/README.md b/README.md index 4a2d7a0bc..977d78934 100644 --- a/README.md +++ b/README.md @@ -130,3 +130,12 @@ $.ajax({ # Want to Help? [Low Hanging Fruit](https://github.com/FarmBot/Farmbot-Web-App/search?utf8=%E2%9C%93&q=todo). [Raise an issue](https://github.com/FarmBot/Farmbot-Web-App/issues/new?title=Question%20about%20a%20TODO) if you have any questions. + +## Translating the web app into your language + +Thanks for your interest in internationalizing the FarmBot web app! To add translations: + +1. Fork this repo +0. Navigate to `/public/app-resources/languages` and run the command `node _helper.js yy` where `yy` is your language's [language code](http://www.science.co.il/Language/Locale-codes.php). Eg: `ru` for Russian. +0. Edit the translations in the file created in the previous step: `"phrase": "translated phrase"`. +0. When you have updated or added new translations, commit/push your changes and submit a pull request. diff --git a/config/webpack.dev.js b/config/webpack.dev.js index a59910e2f..85656795c 100644 --- a/config/webpack.dev.js +++ b/config/webpack.dev.js @@ -68,5 +68,8 @@ module.exports = { new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("production") }) - ] + ], + node: { + fs: "empty" + } } diff --git a/public/app-resources/languages/_helper.js b/public/app-resources/languages/_helper.js index 6023eba1b..25c5f4664 100644 --- a/public/app-resources/languages/_helper.js +++ b/public/app-resources/languages/_helper.js @@ -1,170 +1,170 @@ -// var HelperNamespace = (function () { -// /** -// * @desc Build a list of all the files that are children of the root isDirectory -// * @param {string} dir The rot isDirectory -// * @param {list} filelist The list of the directories/files already detected -// * @param {string} ext The extension to filter for the files -// */ -// function walkSync(dir, filelist, ext) { -// var path = path || require('path'); -// var fs = fs || require('fs'), -// files = fs.readdirSync(dir); -// filelist = filelist || []; -// files.forEach(function (file) { -// if (fs.statSync(path.join(dir, file)).isDirectory()) { -// filelist = walkSync(path.join(dir, file), filelist, ext); -// } -// else { -// if (file.indexOf(ext) > 0) -// filelist.push(path.join(dir, file)); -// } -// }); -// return filelist; -// }; +var HelperNamespace = (function () { + /** + * @desc Build a list of all the files that are children of the root isDirectory + * @param {string} dir The rot isDirectory + * @param {list} filelist The list of the directories/files already detected + * @param {string} ext The extension to filter for the files + */ + function walkSync(dir, filelist, ext) { + var path = path || require('path'); + var fs = fs || require('fs'), + files = fs.readdirSync(dir); + filelist = filelist || []; + files.forEach(function (file) { + if (fs.statSync(path.join(dir, file)).isDirectory()) { + filelist = walkSync(path.join(dir, file), filelist, ext); + } + else { + if (file.indexOf(ext) > 0) + filelist.push(path.join(dir, file)); + } + }); + return filelist; + }; -// /** -// * @desc search in the file in parameter to detect the tags -// */ -// function searchInFile(path) { -// /* some tags could be missed in this regex -// We detect all the tags in the code -// regex is matching '.t("")' or '{t("")}' or ' t("")' or '(t("")' */ -// var REGEX = /[\.|\{|(|\s]t\(\"([\w|\s|\{|\}|\(|\)]*)[\"|,].*\)/g; + /** + * @desc search in the file in parameter to detect the tags + */ + function searchInFile(path) { + /* some tags could be missed in this regex + We detect all the tags in the code + regex is matching '.t("")' or '{t("")}' or ' t("")' or '(t("")' */ + var REGEX = /[\.|\{|(|\s]t\(\"([\w|\s|\{|\}|\(|\)]*)[\"|,].*\)/g; -// var fs = fs || require('fs') -// // load the file -// var fileContent = fs.readFileSync(path, 'utf8'); -// let strArray = []; -// //match all the groups -// var match = REGEX.exec(fileContent); -// while (match != null) { -// strArray.push(match[1]) -// match = REGEX.exec(fileContent); -// } -// return strArray; -// } + var fs = fs || require('fs') + // load the file + var fileContent = fs.readFileSync(path, 'utf8'); + var strArray = []; + //match all the groups + var match = REGEX.exec(fileContent); + while (match != null) { + strArray.push(match[1]) + match = REGEX.exec(fileContent); + } + return strArray; + } -// /** -// * Get all the tags in the files with extension .ts of the current project -// */ -// function getAllTags() { -// const srcPath = __dirname + '/../../..'; + /** + * Get all the tags in the files with extension .ts of the current project + */ + function getAllTags() { + const srcPath = __dirname + '/../../../'; -// var listFilteredFiles = walkSync(srcPath, [], '.ts'); -// var allTags = listFilteredFiles.map(x => searchInFile(x)); -// //flatten list of list in a simple list -// var flattenedTags = [].concat.apply([], allTags); + var listFilteredFiles = walkSync(srcPath, [], '.ts'); + var allTags = listFilteredFiles.map(x => searchInFile(x)); -// //distinct -// var uniq = [...new Set(flattenedTags)]; + //flatten list of list in a simple list + var flattenedTags = [].concat.apply([], allTags); -// var sorted = uniq.sort(function (a, b) { return a.localeCompare(b); }); + //distinct + var uniq = Array.from(new Set(flattenedTags)); -// return sorted; -// } + var sorted = uniq.sort(function (a, b) { return a.localeCompare(b); }); -// /** -// * For debugging -// */ -// function logAllTags() { -// console.dir(getAllTags()); -// } + return sorted; + } -// /** -// * Create the translation file or update it with new tags -// * The tags are in the following order: -// * 1. New tags in English that need to be translated (ASC) -// * 2. Tags already translated, and kept because it match an existing tag in src (ASC) -// * 3. Tags already in the file before but not found at the moment in the src (ASC) -// * @param {string} lang The short name of the language. for the language in parameter -// */ -// function createOrUpdateTranslationFile(lang) { -// lang = lang || 'en'; + /** + * For debugging + */ + function logAllTags() { + console.dir(getAllTags()); + } -// //check current file entry -// const langFilePath = __dirname + '/' + lang + '.js'; -// var fs = fs || require('fs') + /** + * Create the translation file or update it with new tags + * The tags are in the following order: + * 1. New tags in English that need to be translated (ASC) + * 2. Tags already translated, and kept because it match an existing tag in src (ASC) + * 3. Tags already in the file before but not found at the moment in the src (ASC) + * @param {string} lang The short name of the language. for the language in parameter + */ + function createOrUpdateTranslationFile(lang) { + lang = lang || 'en'; -// try { -// var columnsResult = HelperNamespace.getAllTags(); + //check current file entry + const langFilePath = __dirname + '/' + lang + '.js'; + var fs = fs || require('fs') -// var jsonCurrentTagData = {}; -// columnsResult.forEach(function (column) { -// jsonCurrentTagData[column] = column; -// }); + try { + var columnsResult = HelperNamespace.getAllTags(); -// var ordered = {}; -// var fileContent; -// try { -// //check the file can be opened -// var stats = fs.statSync(langFilePath); + var jsonCurrentTagData = {}; + columnsResult.forEach(function (column) { + jsonCurrentTagData[column] = column; + }); -// // load the file -// var fileContent = fs.readFileSync(langFilePath, 'utf8'); -// console.log("Current file content: "); -// console.log(fileContent); -// } -// catch (e) { // do this -// console.log("we will create the file: " + langFilePath); -// //If there is no current file, we will create it -// }; + var ordered = {}; + var fileContent; + try { + //check the file can be opened + var stats = fs.statSync(langFilePath); -// try { -// if (fileContent != undefined) { -// var jsonContent = fileContent -// .replace("module.exports = ", "") -// //regex to delete all comments // and :* in the JSON file -// .replace(/(\/\*(\n|\r|.)*\*\/)|(\/\/.*(\n|\r))/g, ""); + // load the file + var fileContent = fs.readFileSync(langFilePath, 'utf8'); + console.log("Current file content: "); + console.log(fileContent); + } + catch (e) { // do this + console.log("we will create the file: " + langFilePath); + //If there is no current file, we will create it + }; -// var jsonParsed = JSON.parse(jsonContent); + try { + if (fileContent != undefined) { + var jsonContent = fileContent + .replace("module.exports = ", "") + //regex to delete all comments // and :* in the JSON file + .replace(/(\/\*(\n|\r|.)*\*\/)|(\/\/.*(\n|\r))/g, ""); -// Object.keys(jsonParsed).sort().forEach(function (key) { -// ordered[key] = jsonParsed[key]; -// }); -// } -// } catch (e) { -// console.log("file: " + langFilePath + " contains an error: " + e); -// //If there is an error with the current file content, abort -// return; -// } + var jsonParsed = JSON.parse(jsonContent); -// // merge new tags with existing translation -// var result = {}; -// var unexistingTag = {}; -// // all current tags in English -// for (var key in jsonCurrentTagData) result[key] = jsonCurrentTagData[key]; -// for (var key in ordered) { -// // replace current tag with an existing translation -// if (result.hasOwnProperty(key)) { -// delete result[key]; -// result[key] = ordered[key]; -// } -// // if the tag doesn't exist but a translation exists, -// // put the key/value at the end of the json -// else { -// unexistingTag[key] = ordered[key]; -// } -// } -// for (var key in unexistingTag) result[key] = unexistingTag[key]; + Object.keys(jsonParsed).sort().forEach(function (key) { + ordered[key] = jsonParsed[key]; + }); + } + } catch (e) { + console.log("file: " + langFilePath + " contains an error: " + e); + //If there is an error with the current file content, abort + return; + } -// var stringJson = JSON.stringify(result, null, " "); -// var newFileContent = "module.exports = " + stringJson; + // merge new tags with existing translation + var result = {}; + var unexistingTag = {}; + // all current tags in English + for (var key in jsonCurrentTagData) result[key] = jsonCurrentTagData[key]; + for (var key in ordered) { + // replace current tag with an existing translation + if (result.hasOwnProperty(key)) { + delete result[key]; + result[key] = ordered[key]; + } + // if the tag doesn't exist but a translation exists, + // put the key/value at the end of the json + else { + unexistingTag[key] = ordered[key]; + } + } + for (var key in unexistingTag) result[key] = unexistingTag[key]; -// fs.writeFileSync(langFilePath, newFileContent); -// } catch (e) { -// console.log("file: " + langFilePath + ". error append: " + e); -// } -// } + var stringJson = JSON.stringify(result, null, " "); + var newFileContent = "module.exports = " + stringJson; -// //public functions -// return { -// logAllTags: logAllTags, -// getAllTags: getAllTags, -// createOrUpdateTranslationFile: createOrUpdateTranslationFile -// }; -// })(); + fs.writeFileSync(langFilePath, newFileContent); + } catch (e) { + console.log("file: " + langFilePath + ". error append: " + e); + } + } -// // Need to run this cmd in this folder: node _helper.js -// var language = process.argv[2]; -// HelperNamespace.createOrUpdateTranslationFile(language) + //public functions + return { + logAllTags: logAllTags, + getAllTags: getAllTags, + createOrUpdateTranslationFile: createOrUpdateTranslationFile + }; +})(); +// Need to run this cmd in this folder: node _helper.js +var language = process.argv[2]; +HelperNamespace.createOrUpdateTranslationFile(language)