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

feat: add config option to disable the non-printable utf8 escaping #685

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ export default {
// {
// lineWidth: -1,
// }


escapeNonPrintableUnicodeCharacters: true
// If you do not want the parser to convert non printable characters to UTF-8 Character Sequences (ex : \\u00a0), put this to true.
}
```

Expand Down
16 changes: 10 additions & 6 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default class i18nTransform extends Transform {
customValueTemplate: null,
failOnWarnings: false,
yamlOptions: null,
escapeNonPrintableUnicodeCharacters: true
}

this.options = { ...this.defaults, ...options }
Expand Down Expand Up @@ -370,12 +371,15 @@ export default class i18nTransform extends Transform {
})
} else {
text = JSON.stringify(contents, null, this.options.indentation) + '\n'
// Convert non-printable Unicode characters to unicode escape sequence
// https://unicode.org/reports/tr18/#General_Category_Property
text = text.replace(/[\p{Z}\p{Cc}\p{Cf}]/gu, (chr) => {
const n = chr.charCodeAt(0)
return n < 128 ? chr : `\\u${`0000${n.toString(16)}`.substr(-4)}`
})

if(this.options.escapeNonPrintableUnicodeCharacters){
// Convert non-printable Unicode characters to unicode escape sequence
// https://unicode.org/reports/tr18/#General_Category_Property
text = text.replace(/[\p{Z}\p{Cc}\p{Cf}]/gu, (chr) => {
const n = chr.charCodeAt(0)
return n < 128 ? chr : `\\u${`0000${n.toString(16)}`.substr(-4)}`
})
}
}

if (this.options.lineEnding === 'auto') {
Expand Down