@botonic/plugin-contentful
Version:
Botonic Plugin Contentful is one of the **[available](https://github.com/hubtype/botonic/tree/master/packages)** plugins for Botonic. **[Contentful](http://www.contentful.com)** is a CMS (Content Management System) which manages contents of a great variet
89 lines • 3.39 kB
JavaScript
import { __awaiter } from "tslib";
import { ErrorReportingCMS } from '../../cms';
import { Contentful } from '../../contentful/cms-contentful';
import { ContentFieldType } from '../../manage-cms';
import { I18nField } from '../../manage-cms/fields';
import { checkLocale } from '../../nlp';
import { CsvExport, skipEmptyStrings } from './csv-export';
export class PostProcessor {
constructor(targetLocaleOrCountry) {
this.targetLocaleOrCountry = targetLocaleOrCountry;
}
urlAutoTrans(field) {
if (!this.targetLocaleOrCountry) {
return field;
}
if (field.name != ContentFieldType.URL) {
return field;
}
const convertCountry = (urlChunk) => {
if (urlChunk.length != 2) {
return urlChunk;
}
const path = () => {
const lang = this.targetLocaleOrCountry.substr(0, 2).toLowerCase();
if (this.targetLocaleOrCountry.length == 2) {
return lang;
}
const country = this.targetLocaleOrCountry.substr(3).toLowerCase();
return `${country}/${lang}`;
};
const newChunk = path();
console.log(`Replacing part of URL '${field.value}': from '${urlChunk}' to '${newChunk}'`);
return newChunk;
};
return new I18nField(field.name, field.value.split('/').map(convertCountry).join('/'));
}
}
/**
* @param targetLocale if set, it converts URLs which contain the
* locale (eg. a.com/es/) into targetLocale
*/
function writeCsvForTranslators(options, locale, fileName, targetLocaleOrCountry) {
return __awaiter(this, void 0, void 0, function* () {
checkLocale(locale);
const cms = new ErrorReportingCMS(new Contentful(options));
const postProcess = targetLocaleOrCountry
? new PostProcessor(targetLocaleOrCountry)
: undefined;
const exporter = new CsvExport({
stringFilter: skipEmptyStrings,
}, postProcess
? (field) => postProcess.urlAutoTrans(field)
: undefined);
return yield exporter.write(fileName, cms, locale);
});
}
if (process.argv.length < 7 || process.argv[2] == '--help') {
console.log(`Usage: space_id environment access_token locale filename [target_locale]`);
console.log('If target_locale specified, it converts URLs which contains the ' +
'locale (eg. a.com/es/) into target_locale');
// eslint-disable-next-line no-process-exit
process.exit(1);
}
const spaceId = process.argv[2];
const environment = process.argv[3];
const accessToken = process.argv[4];
const locale = process.argv[5];
const fileName = process.argv[6];
const targetLocaleOrCountry = process.argv[7];
function main() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield writeCsvForTranslators({
spaceId,
accessToken,
environment,
resumeErrors: true,
}, locale, fileName, targetLocaleOrCountry);
console.log('done');
}
catch (e) {
console.error(e);
}
});
}
// void tells linters that we don't want to wait for promise
// await in main requires esnext
void main();
//# sourceMappingURL=export-csv-for-translators.js.map