From de4354eb765c243fed6345540885f6c1d1de08f7 Mon Sep 17 00:00:00 2001 From: meriadec Date: Wed, 21 Feb 2018 11:41:51 +0100 Subject: [PATCH] Synchronize translations with Lokalise --- package.json | 5 ++- scripts/trans.js | 71 ++++++++++++++++++++++++++++++++++ static/i18n/en/translation.yml | 21 +--------- static/i18n/fr/translation.yml | 23 ++--------- 4 files changed, 80 insertions(+), 40 deletions(-) create mode 100755 scripts/trans.js diff --git a/package.json b/package.json index 32528127..70670759 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "publish-storybook": "bash ./scripts/publish-storybook.sh", "release": "build", "start": "bash ./scripts/start.sh", - "storybook": "STORYBOOK_ENV=1 start-storybook -s ./static -p 4444" + "storybook": "STORYBOOK_ENV=1 start-storybook -s ./static -p 4444", + "trans": "node scripts/trans" }, "lint-staged": { "*.js": [ @@ -112,6 +113,7 @@ "babel-preset-flow": "^6.23.0", "babel-preset-react": "^6.24.1", "babel-preset-stage-0": "^6.24.1", + "chalk": "^2.3.1", "chance": "^1.0.13", "concurrently": "^3.5.1", "dotenv": "^5.0.0", @@ -131,6 +133,7 @@ "flow-typed": "^2.3.0", "hard-source-webpack-plugin": "^0.5.18", "husky": "^0.14.3", + "js-yaml": "^3.10.0", "lint-staged": "^6.1.1", "node-loader": "^0.6.0", "prettier": "^1.10.2", diff --git a/scripts/trans.js b/scripts/trans.js new file mode 100755 index 00000000..d519a6ba --- /dev/null +++ b/scripts/trans.js @@ -0,0 +1,71 @@ +#!/usr/bin/env node + +/* eslint-disable no-console */ +/* eslint-disable no-use-before-define */ + +require('dotenv').config() + +const path = require('path') +const fs = require('fs') +const axios = require('axios') +const querystring = require('querystring') +const forEach = require('lodash/forEach') +const objectPath = require('object-path') +const yaml = require('js-yaml') +const chalk = require('chalk') + +const { LOKALISE_TOKEN, LOKALISE_PROJECT } = process.env +const BASE = 'https://api.lokalise.co/api' + +const stats = { + nb: 0, +} + +main() + +async function main() { + try { + console.log(`${chalk.blue('[>]')} ${chalk.dim('Fetching translations...')}`) + const url = `${BASE}/string/list` + const { data } = await axios.post( + url, + querystring.stringify({ + api_token: LOKALISE_TOKEN, + id: LOKALISE_PROJECT, + }), + ) + if (data.response.status === 'error') { + throw new Error(JSON.stringify(data.response)) + } + const { strings } = data + forEach(strings, syncLanguage) + console.log( + `${chalk.blue('[>]')} ${chalk.dim('Successfully imported')} ${stats.nb} ${chalk.dim( + 'translations', + )}`, + ) + } catch (err) { + console.log(err) + console.log(`${chalk.red('[x] Error in the process')}`) + process.exit(1) + } +} + +function syncLanguage(translations, language) { + const folderPath = getLanguageFolderPath(language) + const filePath = path.resolve(folderPath, 'translation.yml') + if (!fs.existsSync(folderPath)) { + fs.mkdirSync(folderPath) + } + const obj = translations.reduce((acc, cur) => { + objectPath.set(acc, cur.key, cur.translation) + console.log(`${chalk.green('[✓]')} ${language} ${chalk.dim(cur.key)}`) + ++stats.nb + return acc + }, {}) + fs.writeFileSync(filePath, yaml.dump(obj)) +} + +function getLanguageFolderPath(language) { + return path.resolve(__dirname, `../static/i18n/${language}`) +} diff --git a/static/i18n/en/translation.yml b/static/i18n/en/translation.yml index cd06e6f5..d0faf6f1 100644 --- a/static/i18n/en/translation.yml +++ b/static/i18n/en/translation.yml @@ -3,42 +3,31 @@ common: cancel: Cancel chooseWalletPlaceholder: Choose a wallet... currency: Currency - language: en: English fr: French - orderAccounts: name: Alphabetic balance: Balance type: Cryptocurrency - sidebar: menu: Menu accounts: Accounts - device: connected: Connected notConnected: Not connected - dashboard: title: Dashboard - send: title: Send - receive: title: Receive - addAccount: title: Add account - import: - placeholder: Account {{index}} - + placeholder: 'Account {{index}}' settings: title: Settings - tabs: display: Display money: Money @@ -47,26 +36,20 @@ settings: tools: Tools blockchain: Blockchain profile: Profile - display: language: Language orderAccounts: Order accounts - SelectAccount: - placeholder: Select a account - + placeholder: Select an account AccountPage: balance: Balance receive: Receive lastOperations: Last operations - update: newVersionReady: A new version is ready to be installed. relaunch: Re-launch app now - IsUnlocked: password: Password - sendModal: Amount: Amount Summary: Summary diff --git a/static/i18n/fr/translation.yml b/static/i18n/fr/translation.yml index cd6298b0..2151cb1e 100644 --- a/static/i18n/fr/translation.yml +++ b/static/i18n/fr/translation.yml @@ -1,44 +1,33 @@ common: - ok: Okay + ok: Ok cancel: Annuler chooseWalletPlaceholder: Choisir un portefeuille... currency: Monnaie - language: en: Anglais fr: Français - orderAccounts: - name: Alphabetic + name: Alphabétique balance: Balance type: Cryptocurrency - sidebar: menu: Menu accounts: Comptes - device: connected: Connecté notConnected: Non connecté - dashboard: title: Tableau de bord - send: title: Envoyer - receive: title: Recevoir - addAccount: title: Ajouter un compte - import: - placeholder: Compte {{index}} - + placeholder: 'Compte {{index}}' settings: title: Réglages - tabs: display: Affichage money: Monnaie @@ -47,26 +36,20 @@ settings: tools: Outils blockchain: Blockchain profile: Profil - display: language: Langage orderAccounts: Ordre des comptes - SelectAccount: placeholder: Sélectionner un compte - AccountPage: balance: Balance receive: Recevoir lastOperations: Dernières opérations - update: newVersionReady: Une nouvelle version est prête à être installée relaunch: Redémarrer l'application maintenant - IsUnlocked: password: Mot de passe - sendModal: Amount: Montant Summary: Résumé