Skip to content

Commit

Permalink
Merge pull request #626 from bcentdev/custom-yaml-options
Browse files Browse the repository at this point in the history
Support custom yaml output options
  • Loading branch information
karellm committed Oct 5, 2022
2 parents ab148fe + 2fdd77b commit 17e4b5f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -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,
// }
}
```

Expand Down
2 changes: 2 additions & 0 deletions src/transform.js
Expand Up @@ -41,6 +41,7 @@ export default class i18nTransform extends Transform {
skipDefaultValues: false,
customValueTemplate: null,
failOnWarnings: false,
yamlOptions: null,
}

this.options = { ...this.defaults, ...options }
Expand Down Expand Up @@ -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'
Expand Down
27 changes: 27 additions & 0 deletions test/parser.test.js
Expand Up @@ -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({
Expand Down

0 comments on commit 17e4b5f

Please sign in to comment.