Last active
May 5, 2019 21:05
-
-
Save darkoromanov/02cc6755969d55535a1035ab254d38dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Read https://cloud.google.com/translate/docs/quickstart-client-libraries | |
* for Google APIs authentication | |
* | |
* Dependencies: gettext-parser, sleep, @google-cloud/translate | |
*/ | |
const fs = require('fs') | |
// npm i gettext-parser | |
var gettextParser = require("gettext-parser"); | |
// npm i sleep | |
var sleep = require('sleep'); | |
// Your Google Project Id | |
const projectId = 'final-tiles-gallery' | |
// PO source file | |
const po_source_path = '/Users/darko/Local Sites/ftgdev/app/public/wp-content/plugins/final-tiles-grid-gallery-lite/languages/final-tiles-grid-gallery-lite-de.po' | |
// PO backup file | |
const po_bkp_path = '/Users/darko/Local Sites/ftgdev/app/public/wp-content/plugins/final-tiles-grid-gallery-lite/languages/final-tiles-grid-gallery-lite-bkp-de.po' | |
// PO destination file | |
const po_path = '/Users/darko/Local Sites/ftgdev/app/public/wp-content/plugins/final-tiles-grid-gallery-lite/languages/final-tiles-grid-gallery-lite-de.po' | |
// MO destination file | |
const mo_path = '/Users/darko/Local Sites/ftgdev/app/public/wp-content/plugins/final-tiles-grid-gallery-lite/languages/final-tiles-grid-gallery-lite-de.mo' | |
// Target language | |
const target = 'de'; | |
async function tr(text) { | |
// Imports the Google Cloud client library | |
const {Translate} = require('@google-cloud/translate'); | |
// Instantiates a client | |
const translate = new Translate({projectId}); | |
const [translation] = await translate.translate(text, target); | |
return translation | |
} | |
async function start() { | |
var input = require('fs').readFileSync(po_source_path); | |
var po = gettextParser.po.parse(input); | |
for(let k in po.translations['']) { | |
if(po.translations[''][k] && | |
po.translations[''][k].msgstr && | |
po.translations[''][k].msgstr[0] == '') { | |
let translation = await tr(k) | |
console.log(k + " => " + translation) | |
po.translations[''][k].msgstr[0] = translation | |
if(! po.translations[''][k].comments) | |
po.translations[''][k].comments = {} | |
po.translations[''][k].comments.translator = "automatic translation" | |
let output_po = gettextParser.po.compile(po); | |
fs.writeFileSync(po_bkp_path, output_po); | |
// sleep to avoid flooding the Google APIs | |
sleep.msleep(500) | |
} | |
} | |
var output = gettextParser.mo.compile(po); | |
var output_po = gettextParser.po.compile(po); | |
fs.writeFileSync(mo_path, output); | |
fs.writeFileSync(po_path, output_po); | |
} | |
start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment