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

Use type information during translation key extraction #1095

Open
mspiess opened this issue Dec 24, 2024 · 0 comments
Open

Use type information during translation key extraction #1095

mspiess opened this issue Dec 24, 2024 · 0 comments

Comments

@mspiess
Copy link

mspiess commented Dec 24, 2024

🚀 Feature Proposal

Resolve typescript types to support variables with literal types and union types of literal types being passed to the translation function as keys.

Motivation

i18next-parser has some known caveats.
The need for the workaround is disappointing, as the purpose of the tool is to remove such manual maintenance, especially when the required information is available through typescript.

Example

The following keys a and b should be extracted from the following code:

let a: 'a' | 'b';
a = Math.random() > .5 ? 'a' : 'b';
i18n.t(a);

Implementation Notes

I implemented a proof of concept at mspiess/i18next-parser-ts.
Using type information requires access to the typeChecker which precludes the use of ts.createProgram instead of ts.createSourceFile.
createProgram admits a list of root file paths and walks the project from there.
Using it would require replacing the current vfs node stream architecture, which seems like a lot of work which would possibly incur some breaking changes, so I'd appreciate advice on the feasibility of such a major undertaking.
Maybe it's also possible to assemble a typeChecker without using createProgram. I don't know the typescript internals well enough to tell.

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

1 participant