translate.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const path = require('path');
  2. const fs = require('fs');
  3. const argv = require('minimist')(process.argv.slice(2));
  4. var readlineSync = require('readline-sync');
  5. const root = path.resolve(__dirname+"/../config/locales");
  6. const generated = require(root+"/generated/en.json")['en'];
  7. const ALLOWED_LANG = ['fr'];
  8. let langs = argv.lang.split(',');
  9. langs.forEach(function(lang) {
  10. if (ALLOWED_LANG.indexOf(lang) === -1) {
  11. return;
  12. }
  13. let localePath = root+"/"+lang+".json";
  14. let locale = {};
  15. locale[lang] = {};
  16. try {
  17. locale = require(localePath);
  18. } catch(exception ) {
  19. console.log("New translation into : "+lang);
  20. }
  21. Object.keys(generated).forEach(function(word) {
  22. //If word hasn't already been translated
  23. if (!(word in locale[lang])) {
  24. console.log("New word to translate into "+lang);
  25. let translation = readlineSync.question('+ '+generated[word]+" : ", {
  26. encoding : 'utf-8'
  27. });
  28. locale[lang][word] = translation;
  29. }
  30. });
  31. let localeWords = Object.keys(locale[lang]);
  32. let generatedWords = Object.keys(generated);
  33. //Tidy locale file to remove useless translated words
  34. let data = {}
  35. localeWords.forEach(function(word) {
  36. if (generatedWords.indexOf(word) !== -1) {
  37. data[word] = locale[lang][word]
  38. }
  39. });
  40. locale[lang] = data;
  41. fs.writeFile(localePath, JSON.stringify(locale), function(err) {
  42. console.log("en -> "+lang);
  43. });
  44. })
  45. // dir.readFiles(root, {
  46. // exclude : ['generated']
  47. // }, function(err, content, next) {
  48. //
  49. // let locale = JSON.parse(content);
  50. // let lang = Object.keys(locale)[0];
  51. //
  52. // if (lang === "fr") {
  53. //
  54. // let newLocale = Object.assign({}, generated, locale[lang]);
  55. // let data = {};
  56. // data[lang] = newLocale;
  57. //
  58. // fs.writeFile(root+"/"+lang+".json", JSON.stringify(data), function(err) {
  59. // console.log("en -> "+lang);
  60. // });
  61. //
  62. // } else {
  63. //
  64. // }
  65. //
  66. // next();
  67. //
  68. // }, function(err, files) {
  69. // //console.log(files);
  70. // });