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

Dynamic keys are incorrectly marked as unused #21

Closed
victorloux opened this issue Jul 18, 2022 · 6 comments
Closed

Dynamic keys are incorrectly marked as unused #21

victorloux opened this issue Jul 18, 2022 · 6 comments

Comments

@victorloux
Copy link

Hi! I might be missing something, but I believe some keys are incorrectly reported as unused when they are in fact used as dynamic keys.

For example, in code I have some calls like this:

const errorTitle = I18n.t(`event.api.error.${errorClass}.title`);
return I18n.t(`type.gender.${id}`);

Running the CLI command display-unused will list keys like those below as being unused, even though they do get used by the code above, which will fill in the dynamic code with a value such as errorClass or id.

event.api.error.blocked.title
event.api.error.photoUploadLimit.title
event.api.error.photoUploadSize.title

type.gender.woman
type.gender.man
type.gender.nonBinary

Is there a mechanism to automatically mark those as used? This library does seem to support dynamic keys when finding missing keys, but it only seems to be one-way.

@mxmvshnvsk
Copy link
Owner

Hello. It's correct. i18n-unused is static analyze tool, it's mean, that code doesn't compiles, only read sources.

@victorloux
Copy link
Author

victorloux commented Jul 18, 2022

Thanks. I understand that — I think what I was really wondering is whether there is a way to automatically exclude keys that match a dynamic pattern. For example auto-exclude keys containing type.gender. from being reported as missing or unused when seeing the pattern type.gender.${id}.

I don't really want to have it mark all these keys as 'unused' and delete them, when doing so would actually break my app, because they are used. I know I'd then run the risk of having missing or unused keys within that scope, but it'd still be nice to have the option to auto-exclude dynamic keys from these reports, even accepting this risk 😄

@mxmvshnvsk
Copy link
Owner

mxmvshnvsk commented Jul 18, 2022

Oh, ok. You can use option excludeKey in config. It may be strings array. For example: excludeKey: ['${gender}']. Translations, that will contain this substring will be ignored.

@mxmvshnvsk
Copy link
Owner

And if u use js output in your ci, u can ignore dynamicKeys and totalDynamicCount in result, see docs.

{
  translations: [
    {
      filePath: 'src_file_path',
      staticKeys: ['missed_key'], // keys without ${} syntax
      dynamicKeys: ['missed_key'], // keys with ${} syntax
      staticCount: 1,
      dynamicCount: 1,
    },
  ],
  totalStaticCount: 1,
  totalDynamicCount: 1,
}

@mxmvshnvsk
Copy link
Owner

Auto-excluding for dynamic keys it's may be good feature. I'll make a note for future releases.

@victorloux
Copy link
Author

Thanks 🙏 I knew I could manually exclude keys, didn't realise I could put a dynamic substring placeholder in there, that's useful.

And yes, I've seen that the output of the missed keys gives a count of the dynamic keys it found, but it doesn't seem to find them for unused keys which is what I was looking for. I can do without for now — thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants