Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom yaml output options #626

Merged
merged 1 commit into from Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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