translate.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 ALLOWED_LANG = ['fr'];
  7. let langs = argv.dest.split(',');
  8. let src = argv.src
  9. const generated = require(root+"/"+src+".json")[src];
  10. langs.forEach(function(lang) {
  11. if (ALLOWED_LANG.indexOf(lang) === -1) {
  12. return;
  13. }
  14. let localePath = root+"/"+lang+".json";
  15. let locale = {};
  16. locale[lang] = {};
  17. try {
  18. locale = require(localePath);
  19. } catch(exception ) {
  20. console.log("New translation into : "+lang);
  21. }
  22. Object.keys(generated).forEach(function(word) {
  23. //If word hasn't already been translated
  24. if (!(word in locale[lang])) {
  25. console.log("New word to translate into "+lang);
  26. let translation = readlineSync.question('+ '+generated[word]+" : ", {
  27. encoding : 'utf-8'
  28. });
  29. locale[lang][word] = translation;
  30. }
  31. });
  32. let localeWords = Object.keys(locale[lang]);
  33. let generatedWords = Object.keys(generated);
  34. //Tidy locale file to remove useless translated words
  35. let data = {}
  36. localeWords.forEach(function(word) {
  37. if (generatedWords.indexOf(word) !== -1) {
  38. data[word] = locale[lang][word]
  39. }
  40. });
  41. locale[lang] = data;
  42. fs.writeFile(localePath, JSON.stringify(locale), function(err) {
  43. console.log("en -> "+lang);
  44. });
  45. });