Skip to content

Commit

Permalink
Merge pull request #970 from wanderlog/extract-filenames
Browse files Browse the repository at this point in the history
Output the files in which a string is found in customValueTemplate
  • Loading branch information
karellm committed Feb 26, 2024
2 parents 38035f6 + 200add5 commit d5ed3a0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
28 changes: 24 additions & 4 deletions README.md
Expand Up @@ -204,13 +204,33 @@ export default {

customValueTemplate: null,
// If you wish to customize the value output the value as an object, you can set your own format.
// ${defaultValue} is the default value you set in your translation function.
// Any other custom property will be automatically extracted.
//
// - ${defaultValue} is the default value you set in your translation function.
// - ${filePaths} will be expanded to an array that contains the absolute
// file paths where the translations originated in, in case e.g., you need
// to provide translators with context
//
// Any other custom property will be automatically extracted from the 2nd
// argument of your `t()` function or tOptions in <Trans tOptions={...} />
//
// Example:
// {
// For `t('my-key', {maxLength: 150, defaultValue: 'Hello'})` in
// /path/to/your/file.js,
//
// Using the following customValueTemplate:
//
// customValueTemplate: {
// message: "${defaultValue}",
// description: "${maxLength}", // t('my-key', {maxLength: 150})
// description: "${maxLength}",
// paths: "${filePaths}",
// }
//
// Will result in the following item being extracted:
//
// "my-key": {
// "message": "Hello",
// "description": 150,
// "paths": ["/path/to/your/file.js"]
// }

resetDefaultValueLocale: null,
Expand Down
2 changes: 2 additions & 0 deletions src/helpers.js
Expand Up @@ -89,6 +89,8 @@ function dotPathToHash(entry, target = {}, options = {}) {
entries.forEach((valueEntry) => {
if (valueEntry[1] === '${defaultValue}') {
inner[lastSegment][valueEntry[0]] = newValue
} else if (valueEntry[1] === '${filePaths}') {
inner[lastSegment][valueEntry[0]] = entry.filePaths
} else {
inner[lastSegment][valueEntry[0]] =
entry[valueEntry[1].replace(/\${(\w+)}/, '$1')] || ''
Expand Down
10 changes: 10 additions & 0 deletions src/transform.js
Expand Up @@ -68,6 +68,10 @@ export default class i18nTransform extends Transform {

this.i18next = i18next.createInstance()
this.i18next.init(this.options.i18nextOptions)

// Track where individual keys are found, for use in customValueTemplate
// substitution into "${filePath}"
this.keyToFilePaths = {}
}

error(error) {
Expand Down Expand Up @@ -129,6 +133,12 @@ export default class i18nTransform extends Transform {
key = key.replace(/\\\\/g, '\\')
entry.key = key
entry.keyWithNamespace = entry.namespace + this.options.keySeparator + key
// Add the filename so that we can use it in customValueTemplate
this.keyToFilePaths[key] = this.keyToFilePaths[key] || []
if (!this.keyToFilePaths[key].includes(file.path)) {
this.keyToFilePaths[key].push(file.path)
}
entry.filePaths = this.keyToFilePaths[key]

this.addEntry(entry)
}
Expand Down
9 changes: 9 additions & 0 deletions test/parser.test.js
Expand Up @@ -1958,6 +1958,7 @@ describe('parser', () => {
description: '${max}',
namespace: '${namespace}',
key: '${key}',
paths: '${filePaths}',
},
})

Expand All @@ -1968,6 +1969,11 @@ describe('parser', () => {
path: 'file.js',
})

const anotherFakeFile = new Vinyl({
contents: Buffer.from("t('test')"),
path: 'anotherFile.js',
})

i18nextParser.on('data', (file) => {
result = JSON.parse(file.contents)
})
Expand All @@ -1979,18 +1985,21 @@ describe('parser', () => {
description: '',
namespace: 'translation',
key: 'test',
paths: ['anotherFile.js', 'file.js'],
},
salt: {
message: 'salty',
description: '150',
namespace: 'translation',
key: 'salt',
paths: ['file.js'],
},
})

done()
})

i18nextParser.write(anotherFakeFile)
i18nextParser.end(fakeFile)
})

Expand Down

0 comments on commit d5ed3a0

Please sign in to comment.