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

Extract string to keys in translation files #7

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

nim442
Copy link

@nim442 nim442 commented Mar 24, 2022

ts-refactor-tools.1.mov

This is a little wonky as of right now. A lot of weird undocumented quirks in the API made it kinda hard to implement this feature and needs just a tiny bit more work.

This PR adds a refactor option for StringLiteral ast node that works in the following way

  • If the node on cursor is a StringLiteral, a refactor option to extract the string to a the translation file is added
  • The refactor code looks for a ./lang/en-US-translations.json relative to the current file.
  • The string is Camel Cased and is added as a key to ./lang/en-US-translations.json
  • Intlc is ran to generate the .ts file with the new camelCased key
  • The string is renamed to t.camelCasedName() and the user is prompted to rename the key

We also tell the plugin to look for a rename location in the ./lang/en-US-translations.json file also. This allows us to rename the key name in the json file too

Based on that, there are the following caveats(that can be improved in the near future)

  • This only works if ./lang/en-US-translations.json exists relative to the current file.
  • The rename at source is always t.camelCasedName but we don't always use t context
  • For rename, we don't check is the rename is being called on a intlc key name. Any rename in any context will try to rename the key in json file too which is a bug (Easy fix)

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

Successfully merging this pull request may close these issues.

2 participants