I'll start by using
vim as a file editor. Everything is convenient and fast, but there are some problems that are highlighted during long-term use:
- No asynchrony. I know that there is nvim - but it does not work so quickly and not so smoothly.
- The difficulty of writing plugins. Of course, a controversial question for different gurus - but to be honest, there are so many “porridges”, there is not enough modularity and some kind of structurality of the already implemented “bicycles”
- When too many vim plugins start - you need to be honest for quite a long time and it is annoying
I also use my main hobby -
Android Studio . This IDE is very voracious to system resources, the processor sometimes goes crazy processing the next cycle of gradle assembleDebug.
Visual Studio Code was skeptical. The first thing that surprised startup speed, along with a large project structure. Instant file search and many plug-ins, a great plug-in manager similar to Sublime. The minimum CPU load.
')
A long time ago, in Android Studio, one useful thing was missing — the translation of the
strings.xml resources into other languages. Doing this before was very inconvenient:
- Highlight text
- To copy
- Go to any online translator
- Insert text
- Select current translation language
- Choose which language to translate
- Copy translation
- Open the resulting strings.xml
- Insert translation
What I wanted - with one key combination to get an instant result.
Honestly, I was looking for a similar plugin for vim right away, it was found fairly quickly, but its implementation seems outdated and was relevant at the time when google-translate online was free for use in the “request-answer” mode.
Visual Studio Code has its own
market . The search for a plug-in for translations gave one result, but it was not the translator that I imagined - a translation from traditional Chinese into simplified Chinese, as it was not particularly suited for my queries.
Therefore, I started to implement my own plugin. I started by looking for a service that will help solve the main task - to translate text. This service, fortunately, was
translate.yandex.ru .
A plus was also the fact that the package manager for node.js quickly found an already
implemented module for the Yandex Translate service.
Well, it remains to write the plugin for Visual Studio Code itself.
The basic structure and description are well
documented and do not represent the difficulty of preparing for implementation.
TypeScript was something new for me, before that I used pure node.js for a fairly large project for accounting statistical data.
I will dwell on the moments that were difficult. In node.js, in order to process some process asynchronously, it is convenient to use the
async library. Especially convenient is the async.map method, which allows you to transfer an array of data that needs to be processed and to obtain an array of results.
This case looks like this:
var async = require('async'); async.map(['test1','test2','test3'], test, function(err, results){ console.log(results); }); function test(item) { return item; }
What I needed was a similar structure - to transfer an array of selected strings to the translation function, then translate each one and replace the result with the target strings located between the tags.
<item>string to translate</item>
which are located in the string resource strings.xml android project.
But as it turned out you can use the
Promise :
async function processData(data: any[]) { const promises = data.map(async (item) => { await doSomeAsyncStuff(item);
What it gave - in the end, the size of the plug-in decreased by 2MB, since it was not necessary to pull up the async package for node.js
In the process of implementing the plug-in, I came across a problem - everything worked perfectly in debug mode. When I assembled the plugin, I hurried to put it into the market of the studio. How to do it slowly described in the
instructions .
What led to - an error when starting any extension command:
"An extension might be missing an activation event."Why did this happen - the system did not find the name of the action command that is written in the files of the main code and the custom extension package.json file. In fact, all the names coincided - and the salt was in that they initially pulled the wrong async library using the outdated node-async that failed during initialization. As a result, both the async library were removed from dependencies due to the use of Promise.
The result of the implementation
Having started Visual Studio Code, press Ctrl + P and paste “ext install yandex-translate”. After installation, the studio will offer to “reload” in order to apply the extension to its shell. After rebooting, we take any file in which you need to translate something, press F1, select Yandex translate selected, or use the Ctrl + t key combination.
You can specify from which language to which one to translate using the key combination Ctrl + shift + t or F1 -> Yandex choose languages
In the strings.xml files, selecting the necessary strings and pressing the Ctrl + alt + t combination will result in the translation of all the strings enclosed between the elements:
<item>string to translate</item>
Just in case, I implemented the Yandex Api Key change - all of a sudden, you can make a limited number of transfers using the same key. Then you can change the key to your own.
Everything! Successful translation.
References:
Visual studio codeDescription of the extension system for Visual Studio CodeExpansion of yandex-translate in the marketYandex-translate expansion project on github