Last active
March 21, 2018 13:21
-
-
Save HoverBaum/03c7c904d1bd5dc66c3e540084db0dbf to your computer and use it in GitHub Desktop.
Translate a space to a different locale for Contentful.
This file contains 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
#!/usr/bin/env node | |
/** | |
* 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