diff --git a/README.md b/README.md index d9073f9e..746ce94a 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,14 @@ export default { // If you wish to customize options in internally used i18next instance, you can define an object with any // configuration property supported by i18next (https://www.i18next.com/overview/configuration-options). // { compatibilityJSON: 'v3' } can be used to generate v3 compatible plurals. + + yamlOptions: null, + // If you wish to customize options for yaml output, you can define an object here. + // Configuration options are here (https://github.com/nodeca/js-yaml#dump-object---options-). + // Example: + // { + // lineWidth: -1, + // } } ``` diff --git a/src/transform.js b/src/transform.js index 1e160add..07ba0c76 100644 --- a/src/transform.js +++ b/src/transform.js @@ -41,6 +41,7 @@ export default class i18nTransform extends Transform { skipDefaultValues: false, customValueTemplate: null, failOnWarnings: false, + yamlOptions: null, } this.options = { ...this.defaults, ...options } @@ -365,6 +366,7 @@ export default class i18nTransform extends Transform { if (path.endsWith('yml')) { text = yaml.dump(contents, { indent: this.options.indentation, + ...this.options.yamlOptions, }) } else { text = JSON.stringify(contents, null, this.options.indentation) + '\n' diff --git a/test/parser.test.js b/test/parser.test.js index 9b793622..130251b6 100644 --- a/test/parser.test.js +++ b/test/parser.test.js @@ -1395,6 +1395,33 @@ describe('parser', () => { i18nextParser.end(fakeFile) }) + it('supports custom output configuration for yml', (done) => { + let result + const i18nextParser = new i18nTransform({ + output: 'locales/$LOCALE/$NAMESPACE.yml', + yamlOptions: { + forceQuotes: true, + quotingType: '"', + }, + }) + const fakeFile = new Vinyl({ + contents: Buffer.from("t('first', 'test')"), + path: 'file.js', + }) + + i18nextParser.on('data', (file) => { + if (file.relative.endsWith(path.normalize('en/translation.yml'))) { + result = file.contents.toString('utf8') + } + }) + i18nextParser.once('end', () => { + assert.equal(result.replace(/\r\n/g, '\n'), 'first: "test"\n') + done() + }) + + i18nextParser.end(fakeFile) + }) + it('writes non-breaking space output to yml', (done) => { let result const i18nextParser = new i18nTransform({