Xalphor_IoT
Xalphor_IoT

Reputation: 13

Use vim and regex to translate code comments

I got a .py file with Chinese code comments.

My task is to translate those comments in english.

So I got myself the trans command line tool to quickly translate text. (It's a simple CLI for the google translator [https://github.com/soimort/translate-shell])

And figured out a regular expression which matches exactly what I want to translate.

\v([^#]*&[^"']|^)\zs#.+\ze

I can use the trans CLI within vim with the "!" command.

(for example .!trans -b :en translates the line I'm on.

Can anyone come up with a command to translate all matches?

Important:

I want to just pass the match itself to the trans tool.

If I give the tool whole lines like..

some_variable = True #循迹红外传

.. the tool can't tranlate it properly.

Thanks in advance :)

Upvotes: 1

Views: 178

Answers (2)

SergioAraujo
SergioAraujo

Reputation: 11800

Using @Zorzi solution I figured out another way, maybe easier:

:1s/\v([^#]*&[^"']|^)\zs#.+\ze/\=system('trans -b :en \"'. submatch(0) . '\"')

Of course, you can change the range to fit your needs. And if you break the problem down into two pieces, search and substitution it becomes easier to understand what happens:

/\v([^#]*&[^"']|^)\zs#.+\ze/

:1s//\=system('trans -b :en \"'. submatch(0) . '\"')

Note: The submatch(0) represents exactly what our regex matched

I have tested another less complex version with no backslashes and it seems to work:

:1s//\=system('trans -b :en "'. submatch(0) . '"')

Finally, I have found out how to find Chinese characters, making our search easier:

/[^\x00-\xff]\+

Reference: https://stackoverflow.com/a/61233146/2571881

Upvotes: 1

Zorzi
Zorzi

Reputation: 792

Interesting problem!

Here's the solution I came up with:

:%g/\v([^#]*&[^"']|^)\zs#.+\ze/exe"norm!\"acgn\<C-r>=system('trans -b :en <<< \"'.@a.'\"')\<CR>"

You might need to change the system(...) part, as I'm not sure how your trans command works...

Explanation

  • %g| This will execute a given command on each line where a regex is found
  • \v([^#]*&[^"']|^)\zs#.+\ze| That's your regex!
  • exe"..."| executes a given vim command. We use it here so we can enter keystrokes in the normal (e.g. <C-r>)
  • norm!| Simulates keystrokes, as if you typed them yourself

So now we're going to enter keys as if we typed them on our keyboard, and this will be typed on each occurence of the match


  • "a| Will store the result of the next action in the register a
  • cgn| Will cut the next pattern match (or the one under the cursor), and enter in insert mode
  • <C-r>=| Will insert the content of the expression register, in other term, it will let you enter an expression
    • system('trans -b :en <<< \"'.@a.'\"')| system allows you to execute a bash command, then it's up to you what you put in there
    • <CR>| The enter key (Carriage Return)

You can of course find all of that in vim documentation:

  • :help :global
  • :help execute
  • :help normal
  • :help i_CTRL-R
  • :help @=
  • :help system()

Upvotes: 1

Related Questions