Search code examples
visual-studio-codescriptinglocalizationkeyboard-shortcutsvscode-tasks

How to create a custom command in VSCode to modify my text for translation?


Having to do localization throughout the entire project. I have to change every text in the project from

<p> Home </p>

to

 <p> {t("Home")} </p>

Is there a way to bind a shortcut, such that on the selection of Home text and pressing the key binding with converting it to the required format.


Solution

  • Define the following keybinding:

    {
      "key": "ctrl+i l",  // or other combo
      "command": "editor.action.insertSnippet",
      "when": "editorTextFocus",
      "args": {
        "snippet": "{t(\"$TM_SELECTED_TEXT\")}"
      }
    }