Skip to content

Instantly share code, notes, and snippets.

@joeydebreuk
Last active February 13, 2020 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeydebreuk/7be6519d4fde3b91c1125513595bf47d to your computer and use it in GitHub Desktop.
Save joeydebreuk/7be6519d4fde3b91c1125513595bf47d to your computer and use it in GitHub Desktop.
Send missing translations in your Vue project to TranslationHut
/*
This file can add all missing vue-i18n translations in
your Vue App to your TranslationHut.com project
Setup vue-i18n: https://kazupon.github.io/vue-i18n/started.html
Setup TranslationHut.com
1. Signup and create a project at TranslationHut.com
2. Go to "develop"
3. Create an API-key -> Paste the secret in this file
Run the script:
1. $ install npm: npm install --global vue-i18n-extract
2. Add this file in the root map of your App
3. Optionally configure PATH_TO_MESSAGES and PATH_TO_SRC
4. execute this file with node: $ node ./sendMissingTranslationKeys.js
*/
/*
Configuration
*/
const SECRET = 'YOURSECRET';
// These might already be configured correctly:
const PATH_TO_MESSAGES = './src/messages/*.?(js|json)';
const PATH_TO_SRC = './src/**/*.?(js|vue|ts)';
/*
Script
*/
const VueI18NExtract = require('vue-i18n-extract').default;
const fetch = require('node-fetch');
const URL = 'https://translationhut.com/graphql/';
const CREATE_ASSET_QUERY = `
mutation addAsset ($value:String!) {
addAsset(assetValue: $value) {
ok
errors {
field
messages
}
}
}`;
const FETCH_OPTIONS = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Api-Key ${SECRET}`,
},
credentials: 'omit',
};
const logResponse = (key, json) => console.log(`Response for asset ${key}:`, json);
const createFetchOptions = key => ({
...FETCH_OPTIONS,
body: JSON.stringify({
query: CREATE_ASSET_QUERY,
variables: { value: key },
}),
});
const sendKey = key =>
fetch(URL, createFetchOptions(key))
.then(res => res.json())
.then(json => logResponse(key, json))
.catch(console.error);
const uniqueArray = arr => [...new Set(arr)];
const report = VueI18NExtract.createI18NReport(PATH_TO_SRC, PATH_TO_MESSAGES);
const keys = report.missingKeys.map(({path}) => path);
uniqueArray(keys).forEach(sendKey);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment