Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert a exported Spaces locale.
#!/usr/bin/env node
/* eslint-disable */
/**
* Replace the locale in an exported space.
*
* Usage:
* node changeSpaceLocale.js -l es-ES < space.json > translated.json
*
* The above will translate the space to Spanish (currently the only supported
* language to translate to).
*/
console.time('runtime')
const cli = require('commander')
const getStdin = require('get-stdin')
cli
.version(require('./package.json').version)
.option('-l, --locale [target locale]', 'The target locale')
cli.parse(process.argv)
if(!cli.locale) {
console.log('Plese provide the target locale using -l <locale>.')
process.exit(1)
}
const locales = JSON.parse(`[
{
"name": "Spanish (Spain)",
"code": "es-ES",
"fallbackCode": null,
"default": true,
"contentManagementApi": true,
"contentDeliveryApi": true,
"optional": false,
"sys": {
"type": "Locale",
"id": "3koMgAFIN0p0KxWpdMzmYC",
"version": 1,
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "otr3kvjzo6rg"
}
},
"createdBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "3wfO9ZgIpLXVfGau9ononY"
}
},
"createdAt": "2018-02-28T15:10:25Z",
"updatedBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "3wfO9ZgIpLXVfGau9ononY"
}
},
"updatedAt": "2018-02-28T15:10:25Z"
}
}
]`)
const run = async () => {
if (!locales.find(locale => locale.code === cli.locale)) {
console.log(`Sorry, but we don't support ${cli.locale}`)
}
const space = await getStdin()
const replacesLocaleCodes = space.replace(/en-US/g, cli.locale)
const parsedSpace = JSON.parse(replacesLocaleCodes)
parsedSpace.locales = [locales.find(locale => locale.code === cli.locale)]
console.log(JSON.stringify(parsedSpace, null, 2))
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment