From 00ec1bf63308c2f44a3a8e1b510ca1787451c877 Mon Sep 17 00:00:00 2001
From: Tom Robinson <tlrobinson@gmail.com>
Date: Mon, 18 Mar 2019 14:27:00 -0700
Subject: [PATCH] Add POEditor scripts, remove unused update-translation(s)
 scripts

---
 bin/i18n/export-pot-to-poeditor  | 13 +++++
 bin/i18n/import-po-from-poeditor | 88 ++++++++++++++++++++++++++++++++
 bin/i18n/update-translation      | 22 --------
 bin/i18n/update-translations     |  7 ---
 4 files changed, 101 insertions(+), 29 deletions(-)
 create mode 100755 bin/i18n/export-pot-to-poeditor
 create mode 100755 bin/i18n/import-po-from-poeditor
 delete mode 100755 bin/i18n/update-translation
 delete mode 100755 bin/i18n/update-translations

diff --git a/bin/i18n/export-pot-to-poeditor b/bin/i18n/export-pot-to-poeditor
new file mode 100755
index 00000000000..67c75c178fd
--- /dev/null
+++ b/bin/i18n/export-pot-to-poeditor
@@ -0,0 +1,13 @@
+#!/usr/bin/env bash
+
+set -eu
+
+POEDITOR_PROJECT_ID="200535"
+
+echo "Uploading metabase.pot to POEditor..."
+
+curl -X POST https://api.poeditor.com/v2/projects/upload \
+     -F api_token="${POEDITOR_API_TOKEN}" \
+     -F id="${POEDITOR_PROJECT_ID}" \
+     -F updating="terms" \
+     -F file=@"locales/metabase.pot"
diff --git a/bin/i18n/import-po-from-poeditor b/bin/i18n/import-po-from-poeditor
new file mode 100755
index 00000000000..9a7b584d399
--- /dev/null
+++ b/bin/i18n/import-po-from-poeditor
@@ -0,0 +1,88 @@
+#!/usr/bin/env node
+
+// USAGE:
+//
+// POEDITOR_API_TOKEN=TOKEN ./bin/i18n/import-po-from-poeditor [LANG...]
+//
+// If no arguments are provided it updates existing locales in ./locales
+
+const fs = require("fs");
+const fsp = fs.promises;
+const https = require("https");
+const fetch = require("isomorphic-fetch");
+const url = require("url");
+
+const POEDITOR_API_TOKEN = process.env["POEDITOR_API_TOKEN"];
+const POEDITOR_PROJECT_ID = "200535"; // Metabae POEditor project
+
+// currently we don't support variants of language
+const aliases = {
+  "pt-br": "pt",
+  "zh-CN": "zh",
+};
+
+async function main(args) {
+  const wanted = new Set(args.length > 0 ? args : await getExistingLanguages());
+  const other = [];
+
+  // list available languages
+  const { result: { languages } } = await poeditor("languages/list");
+  for (const language of languages) {
+    const code = aliases[language.code] || language.code;
+    if (wanted.has(code)) {
+      wanted.delete(code);
+      console.log(`Downloading: ${code}  (${language.percentage}%)`);
+      // start an export
+      const { result: { url } } = await poeditor("projects/export", {
+        language: language.code,
+        type: "po",
+      });
+      // download the exported file
+      https.get(url, res =>
+        res.pipe(fs.createWriteStream(`./locales/${code}.po`)),
+      );
+    } else {
+      other.push(language);
+    }
+  }
+  // log others with large percentage complete
+  for (const language of other
+    .filter(l => l.percentage > 50)
+    .sort((a, b) => b.percentage - a.percentage)) {
+    console.log(`Other:       ${language.code} (${language.percentage}%)`);
+  }
+  // log languages that are wanted but missing in POEditor
+  for (const code of wanted) {
+    console.log(`Missing:     ${code}`);
+  }
+  if (wanted.size > 0) {
+    throw new Error("Some wanted language not found");
+  }
+}
+
+// simple API client for poeditor
+function poeditor(command, params = {}) {
+  const uri = url.format({
+    protocol: "https",
+    hostname: "api.poeditor.com",
+    pathname: `/v2/${command}`,
+  });
+  const query = {
+    api_token: POEDITOR_API_TOKEN,
+    id: POEDITOR_PROJECT_ID,
+    ...params,
+  };
+  return fetch(uri, {
+    method: "POST",
+    headers: { "Content-Type": "application/x-www-form-urlencoded" },
+    body: url.format({ query }).replace(/^\?/, ""),
+  }).then(res => res.json());
+}
+
+async function getExistingLanguages() {
+  return (await fsp.readdir("./locales"))
+    .filter(f => /\.po$/.test(f))
+    .map(f => f.replace(/\.po$/, ""));
+}
+
+main(process.argv.slice(2)).then(null, console.warn);
diff --git a/bin/i18n/update-translation b/bin/i18n/update-translation
deleted file mode 100755
index afa3da9a36a..00000000000
--- a/bin/i18n/update-translation
+++ /dev/null
@@ -1,22 +0,0 @@
-#!/bin/sh
-
-set -eu
-
-# gettext installed via homebrew is "keg-only", add it to the PATH
-if [ -d "/usr/local/opt/gettext/bin" ]; then
-  export PATH="/usr/local/opt/gettext/bin:$PATH"
-fi
-
-POT_NAME="locales/metabase.pot"
-PO_NAME="locales/$1.po"
-
-if [ $# -lt 1 ]; then
-  echo "USAGE: update-translation en_US"
-  exit 1
-fi
-
-if [ -f "$PO_NAME" ]; then
-  exec msgmerge -U "$PO_NAME" "$POT_NAME"
-else
-  exec msginit -i "$POT_NAME" -o "$PO_NAME" -l "$1"
-fi
diff --git a/bin/i18n/update-translations b/bin/i18n/update-translations
deleted file mode 100755
index 85dfefe8e29..00000000000
--- a/bin/i18n/update-translations
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/bin/sh
-
-set -eu
-
-./bin/i18n/update-translation-template
-
-find locales -name "*.po" -exec sh -c './bin/i18n/update-translation $(basename {} .po)' \;
-- 
GitLab