translate('h
^------------------------------------------------------
| - hello Hi! đź‘‹ |
| - header.title Do more with your translations đź“” |
| - header.sub in Visual Studio Code 👩‍💻 |
------------------------------------------------------
An extension for VSCode which provides autocompletion for the translations within your project. Uses https://github.com/rbozan/lsp-translations under the hood. Download the extension on https://marketplace.visualstudio.com/items?itemName=rbozan.vscode-lsp-translations.
completion.mp4
hovering.mp4
This extension requires https://github.com/rbozan/lsp-translations which will automatically be downloaded or updated on startup.
-
Translation files must be in
.json
or.yaml
/.yml
format for now. Supporting other formats is being worked on in the Treesitter issue. -
While
lsp-translations
theoretically works with any programming language, this extension is currently only enabled on a limited amount of programming languages and extensions, namely:-
JavaScript (including JSX)
-
TypeScript (including TSX)
-
Ruby
-
PHP
-
- Install the extension on https://marketplace.visualstudio.com/items?itemName=rbozan.vscode-lsp-translations
- Add the configuration under Common configurations to your workspace-settings
translate(
away! (ort(
,translate
,I18n.t(
),
This extension contributes the following settings which are recommended to set as workspace settings:
Glob patterns for the translation files which contain all the translations of your project.
[
"./translations.json",
"./translations/*.json",
"./translations.yml",
"./translations/*.yml",
"./translations.yaml",
"./translations/*.yaml"
]
Glob patterns for the files which match the patterns in lsp-translations.translationFiles.include
but should not be included as translation file.
[]
An optional regex pattern for the file name which can provide extra details to the extension. Can be useful when your translation file name contains language like 'en'.
None.
"(?P<language>.+?)\\.json"
An optional regex pattern for the key of a translation which can provide extra details to the extension. Can be useful when your translation keys contains a language like 'en'.
None.
"^.+?\\.(?P<language>.+?)\\."
An optional regex pattern to filter out unneeded parts of a translation key.
None.
If you have a translation key like 123.abc.key
and you only provide key
to the translation
function in your code, you can use this setting to filter out the unneeded parts. The first regex group would then be the correct key to be used. In the case of 123.abc.key
you can use the following regex:
^.+?\..+?\.(.+$)
To read more information about this particular regex, see https://regex101.com/r/YLnGRw/1.
Most projects have a similar way of defining their translations. If you use any of the following ways to organise your translations you can use the configuration for your workspace to easily use the extension.
Key | Value | Example |
---|---|---|
File name | (language).json |
en.json |
Key format | (key) |
header |
"lsp-translations.translationFiles.include": ["./translations/*.json"],
"lsp-translations.fileName.details": "(?P<language>.+?)\\."
Key | Value | Example |
---|---|---|
File name | (anything).json |
language.json |
Key format | (language).(key) |
en.header |
"lsp-translations.translationFiles.include": ["./translations/*.json"],
"lsp-translations.key.details": "^(?P<language>.+?)\\.",
"lsp-translations.key.filter": "^.+?\\.(.+$)"
Key | Value | Example |
---|---|---|
File name | (anything).json |
language.json |
Key format | (project_id).(language).(key) |
12345.en.header |
"lsp-translations.translationFiles.include": ["./translations/*.json"],
"lsp-translations.key.details": "^.+?\\.(?P<language>.+?)\\.",
"lsp-translations.key.filter": "^.+?\\..+?\\.(.+$)"
Check if you have setted up the configuration correctly.
Use the regex match group language
to distinguish by language.