How to localise VSCode extension - visual-studio-code

I wrote a VS Code extension to support printing. Since all the recent issues have been concerned with issues relating to foreign character sets, it seems like I should support languages other than English.
But I can't find anything in on localisation in the VS Code API documentation. There's a section on languages but that's about parsing and syntax colouring etc for computer languages.
Is there any support or at least convention regarding localisation of VS Code extensions?
Thanks to Gama11 for pointing me at good resources.

Yes, this is possible, and there is actually a I18n sample extension for this:
https://github.com/microsoft/vscode-extension-samples/tree/master/i18n-sample
It's best if you read the readme, but the basic idea is the following:
use the vscode-nls-dev NPM package
use NLS identifiers such as "%extension.sayHello.title%" as placeholders for command titles and such in package.json
similarly, in JS code NLS identifiers can be translated with a localize() method imported from vscode-nls
have a toplevel i8n directory that contains the translations for those identifiers for the languages that are supported in <file-name>.i18n.json files
Alternatively, you could also take a look at how the C++ extension does it:
https://github.com/microsoft/vscode-cpptools/tree/master/Extension
They seem to take a slightly different approach: no i8n directory, but instead have the translations directly next to the file (package.nls.it.json, package.nls.zh-cn.json and package.nls.json with the default / English). I'm not sure if it translates anything outside of package.json / in JS code though.

The official l10n (localization) sample extension should help a lot, as the old i18n (internationalization) sample has been deprecated.
Clone it here: https://github.com/microsoft/vscode-extension-samples/tree/main/l10n-sample

Related

What are types of extensions in VSCode?

Hi i am creating my first extension for VSCode following the official tutorial
After running the command yo code to create a boilerplate the program asks which type of extension to create.
I couldn't find any docs for these types of extensions that would help me determine how they differ from each other except for
Language Extensions.
It would be helpful if there were some documentation explaining these.
From top to bottom:
An extension that adds any of the possible contribution points (theme, keybindings, language support, icons, snippets etc.). Initial language is Typescript, but you can use other languages at any time, as long as they can be transpiled to Javascript.
Like 1), but with JS as initial language. Still, you can use other languages too.
A color theme for syntax highlighting, which is a collection of colors for predefined token types (these types are determined by a language extension, either provided by another extension or yours).
Language support, which means handling of a programming or markup language. That includes parsing of such code and providing the tokens for syntax highlighting, code completion, code lenses, parameter info, formatting, linting etc. This could include a language server (which is just a separate process for all that mentioned here), but that has an own entry in this list.
Code snippets, to provide small code parts for use during programming.
Keymap, to provide specific keybindings (e.g. vim is a very popular keybinding).
Extension pack, not 100% sure about that, but I believe this packs multiple extensions into one (e.g. if you have separate keybindings and color theme extensions, you can pack them into a combine extension).
The previously mentioned language server. Language processing can be time consuming and you don't want to block the main (UI) thread. So, any such processing can be moved out to a language server, which can even be written in faster languages like C++ for highest performance.
Given this list it should be clear that you either want 1), 2) or 4).

Can I create VS Code extensions in Python/C++?

I am totally new to creating extensions in VS Code, and all the official examples of extensions are written in Typescript/Javascript, which I have no experience with. Is it possible to create VS Code extensions in other languages, such as Python or C++?
If so, could anyone point me to any resources to get me started?
It is possible by creating a C++ module for Node.js, which can then be loaded like any other node module. Of course, some glue code written in JS or TS is necessary to register the extension and translate calls to/from vscode.
I've gone this way in my ANTLR4 extension, but gave up eventually, because of the troubles I had due to incompatible dependencies (you have to make sure the extension uses the exact same V8 version, which was used to build the underlying Node.js used by vscode, on all supported platforms).
This situation might have change, I don't know, but with that in the background I don't recommend it.
If you want to add support for a new language in vscode you can also write a separate language server, as is mentioned in the linked SO answer. For other type of work, I'm afraid, you have no alternative to use.
No, as #rioV8 said, since VSCode is an electron app and runs on Javascript.

VS Code formatting plugins

Is there a way in VS Code to format code according to the style that a person prefers? I guess there might be a plugin for this sort of thing but I've not found that.
For example, I prefer 'K&R' coding style while a colleague prefers 'Whitesmiths' style. When we share code or functions it would be nice to autoformat code into the style that each prefers.
This does not seem like it should be so difficult for a plugin to do except that coding style also has to parse the language of the code, so Whitesmiths PowerShell is going to be different from Whitesmiths on Perl I would imagine.
Are there any plugins like this (or internal functionality in VS Code - it's a very comprehensive tool without installing any plugins of course!) that people use regularly like this?
In contrast to its name, "JIndent" (commercial) is a tool I've been using for the exact purpose you're asking about.

How do TextMate grammars and themes work with VSCode?

VSCode is built on top of MonacoEditor which doesn't support Textmate grammars and themes. But somehow VSCode made it possible. I am curious how VSCode is able to do this.
I am asking because I am making a code editor (based on Monaco) with TextMate grammar and theme support. But I am unable to understand how I can achieve it.
Though there are packages like monaco-textmate to make TextMate grammars work with Monaco, syntax highlighting is still not working properly.
TextMate grammars depend on a particular regex implementation / library called Oniguruma, which is implemented in C. Monaco however is designed to run in the browser, and the JavaScript regex engine available there is not compatible with Oniguruma. All of this is explained in detail in the "Why doesn't the editor support TextMate grammars?" section of Monaco's FAQ. It also mentions the possibiliy of perhaps eventually compiling Oniguruma to WebAssembly to work around this.
VSCode itself uses vscode-textmate for its TMLanguage handling, which has the Oniguruma library as a native dependency. VSCode can have native dependencies because it doesn't run in a browser environment.
According to monaco-textmate's readme, it is actually heavily based on vscode-textmate:
99% of the code in this repository is extracted straight from vscode-textmate
And it does use the WASM approach mentioned earlier:
monaco-textmate relies on onigasm package to provide oniguruma regex engine in browsers. onigasm itself relies on WebAssembly.
As to why syntax highlighting doesn't always work as expected with monaco-textmate... I have no idea, I expect this is simply a bug in the implementation. Perhaps wait for a response from the maintainer, the issue you linked is fairly new.
At least conceptually there shouldn't be a reason why it couldn't achieve the same syntax highlighting VSCode does, since it uses the same regex flavour.

How to access AST of active file in VS Code extension?

From my extension, how can I access the abstract syntax tree that VS Code has for the active file? I have been looking through the API docs but haven't been able to find anything. I also came across this SO question but both the question and answer are pretty opaque to me.
There's no thing like a common AST for files loaded into an editor. In fact, many file aren't even parsed at all, unless an extension is installed which does that.
The linked answer describes a way to implement language support (via a language server), which is not the same as getting a fictional AST from vscode.