Skip to content

Commit

Permalink
fix: Don't print i18next internal options
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Skelton committed Mar 26, 2021
1 parent bac3ede commit 525cf7b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { PostProcessorModule } from 'i18next'

const ignore = ['defaultValue', 'interpolation', 'lng', 'lngs', 'ns']

export default {
name: 'printKeys',
type: 'postProcessor',
process(_, key, options) {
return Object.keys(options).length
? key + ' ' + JSON.stringify(options)
// Filter out internal i18next options when printing keys. The resulting
// array is passed to JSON.stringify which instructs it which keys should
// be printed.
const optionKeys = Object.keys(options).filter(
(key) => !ignore.includes(key)
)

return optionKeys.length
? key + ' ' + JSON.stringify(options, optionKeys)
: key.toString()
},
} as PostProcessorModule
7 changes: 7 additions & 0 deletions test/printKeys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ it('should print interpolated values after the key', () => {
)
expect(i18next.t('count', { count: 12 })).toBe('count {"count":12}')
})

it('should not print the internal options', () => {
expect(i18next.t('key', { defaultValue: 'hi' })).toBe('key')
expect(i18next.t('key', { lng: 'en-US' })).toBe('key')
expect(i18next.t('key', { lngs: ['en', 'es'] })).toBe('key')
expect(i18next.t('key', { interpolation: {} })).toBe('key')
})
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"module": "CommonJS",
"target": "ES6",
"lib": ["ESNext"],
"moduleResolution": "node",
"esModuleInterop": true,
"removeComments": true,
Expand Down

0 comments on commit 525cf7b

Please sign in to comment.