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(parser): Add exception for removing unused keys #262

Merged
merged 4 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ Below are the configuration options with their default values:
compatibilityJSON: 'v3', // One of: 'v1', 'v2', 'v3', 'v4
debug: false,
removeUnusedKeys: false,
filterUnusedKeys: false,
sort: false,
attr: {
list: ['data-i18n'],
Expand Down Expand Up @@ -574,6 +575,28 @@ Type: `Boolean` Default: `false`

Set to `true` to remove unused translation keys from i18n resource files.

#### filterUnusedKeys

Type: `Object` Default: `false`

If an `Object` is supplied and `removeUnusedKeys` is set to `true`, to leave specific keys you can specify a function like so:
cheton marked this conversation as resolved.
Show resolved Hide resolved
```js
filterUnusedKeys: ({ lng, ns, unusedKey }) => {
if (ns === 'resource') {
const exceptionKeys = ['word', 'key.word', ...];
if (exceptionKeys.includes(unusedKey)) {
// leave key
return true;
}
}
if (ns === 'other_resource') {
// leave key
return true;
}
// remove key
return false;
}

#### sort

Type: `Boolean` Default: `false`
Expand Down
34 changes: 32 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,10 +796,40 @@ class Parser {
const resStoreKeys = flattenObjectKeys(_.get(this.resStore, [lng, ns], {}));
const resScanKeys = flattenObjectKeys(_.get(this.resScan, [lng, ns], {}));
const unusedKeys = _.differenceWith(resStoreKeys, resScanKeys, _.isEqual);
let filteredKey;

for (let i = 0; i < unusedKeys.length; ++i) {
_.unset(resMerged[lng][ns], unusedKeys[i]);
this.log(`Removed an unused translation key { ${chalk.red(JSON.stringify(unusedKeys[i]))} from ${chalk.red(JSON.stringify(this.formatResourceLoadPath(lng, ns)))}`);
filteredKey = false;

if (
this.options.filterUnusedKeys &&
typeof this.options.filterUnusedKeys === 'function'
) {
filteredKey = this.options.filterUnusedKeys({
lng,
ns,
unusedKey: unusedKeys[i].toString().replaceAll(',', '.'),
});
}

if (!filteredKey) {
_.unset(resMerged[lng][ns], unusedKeys[i]);
this.log(
`Removed an unused translation key { ${chalk.red(
JSON.stringify(unusedKeys[i])
)} } from ${chalk.red(
JSON.stringify(this.formatResourceLoadPath(lng, ns))
)}`
);
continue;
}
this.log(
`Skipped removing an unused translation key { ${chalk.yellow(
JSON.stringify(unusedKeys[i])
)} } from ${chalk.yellow(
JSON.stringify(this.formatResourceLoadPath(lng, ns))
)}`
);
}

// Omit empty object
Expand Down