translate.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/env node
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * npm install --save-dev colors
  7. * npm install --save-dev iconv-lite
  8. */
  9. 'use strict';
  10. require('colors');
  11. const fs = require('fs-extra');
  12. const readline = require('readline');
  13. const rl = readline.createInterface({
  14. input: process.stdin,
  15. output: process.stdout,
  16. });
  17. const client_id = 'J2f4lmfl2XghGb92zQ4N';
  18. const client_secret = '1qHQ9YQNMc';
  19. function translate(query, target) {
  20. return new Promise((resolve) => {
  21. if (target !== 'ko') {
  22. var api_url = 'https://openapi.naver.com/v1/papago/n2mt';
  23. var request = require('request');
  24. var options = {
  25. url: api_url,
  26. form: { source: 'ko', target: target, text: query },
  27. headers: { 'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret },
  28. };
  29. request.post(options, function (error, response, body) {
  30. if (!error && response.statusCode == 200) {
  31. resolve(JSON.parse(body).message.result.translatedText.trim());
  32. } else {
  33. resolve(query.trim());
  34. }
  35. });
  36. } else {
  37. resolve(query.trim());
  38. }
  39. });
  40. }
  41. function showLogo(callBack) {
  42. fs.readFile('./banner.txt', 'utf8', (error, jsonFile) => {
  43. const logo = jsonFile;
  44. fs.readFile('./package.json', 'utf8', (error, jsonFile) => {
  45. const packageInfo = JSON.parse(jsonFile);
  46. console.log(logo.green + '\n' + (packageInfo.description + ' ' + packageInfo.version).red);
  47. callBack();
  48. });
  49. });
  50. }
  51. var myArgs = process.argv.slice(2);
  52. function translateQ(translateQueue, target, callBack ) {
  53. const q = translateQueue.shift();
  54. if (q !== null && q !== undefined) {
  55. translate(q.query, target).then((result) => {
  56. process.stdout.write(".");
  57. q.setter(result);
  58. translateQ(translateQueue, target, callBack );
  59. });
  60. } else {
  61. callBack();
  62. }
  63. }
  64. showLogo(() => {
  65. if (myArgs.length > 0) {
  66. const folderName = myArgs[0];
  67. const question = [];
  68. question.push('Select Language to Translate.)');
  69. question.push('ko - Korean(Resort)');
  70. question.push('ja - Japanese');
  71. question.push('en - English');
  72. question.push('cn - Chinese');
  73. question.push('es, fr, vi, th, id');
  74. question.push('Language (ja | en | cn ...) - ? ');
  75. rl.question(question.join('\n'), function (locale = '') {
  76. let target = 'en';
  77. switch (locale.toLowerCase()) {
  78. case 'ko':
  79. case 'korean':
  80. target = 'ko';
  81. break;
  82. case 'ja':
  83. case 'japanese':
  84. target = 'ja';
  85. break;
  86. case 'en':
  87. case 'english':
  88. target = 'en';
  89. break;
  90. case 'cn':
  91. case 'zh':
  92. case 'zh-cn':
  93. case 'chinese':
  94. target = 'zh-CN';
  95. break;
  96. case 'es':
  97. case 'fr':
  98. case 'vi':
  99. case 'th':
  100. case 'id':
  101. target = locale.toLowerCase();
  102. break;
  103. }
  104. console.log("Translate Korean to " + target.red + " start !!");
  105. fs.readFile(folderName + '.json', 'utf-8', function (err, jsonFile) {
  106. const jsonInfo = JSON.parse(jsonFile);
  107. const translateQueue = [];
  108. const keys = Object.keys(jsonInfo);
  109. keys.forEach(key => {
  110. if (typeof(jsonInfo[key]) === 'string') {
  111. translateQueue.push({ query : jsonInfo[key], setter : (txt) => {
  112. jsonInfo[key] = txt;
  113. }})
  114. } else if (typeof(jsonInfo[key]) === 'object') {
  115. const childObj = jsonInfo[key];
  116. const childKeys = Object.keys(childObj);
  117. childKeys.forEach(childKey => {
  118. if (typeof(childObj[childKey]) === 'string') {
  119. translateQueue.push({ query : childObj[childKey], setter : (txt) => {
  120. childObj[childKey] = txt;
  121. }})
  122. }
  123. });
  124. }
  125. });
  126. translateQ(translateQueue, target, function() {
  127. fs.writeFile(
  128. folderName + '-' + target + '.json',
  129. JSON.stringify(jsonInfo, null, "\t"),
  130. 'utf-8',
  131. () => {
  132. console.log("\n\nTranslate Korean to " + target.red + " end !! - " + folderName + '-' + target + '.json');
  133. process.exit(1);
  134. }
  135. );
  136. });
  137. });
  138. });
  139. } else {
  140. console.log('missing argument!!'.red + ' ex) node ./scripts/translate.js docs/translate');
  141. process.exit(1);
  142. }
  143. });