2

I've just inherited some C code from a German programmer, and all of the comments are, naturally, in German. As I've forgotten most of my high school German, this is a slight problem.

Does anyone know of any translation tools that are code-aware; meaning it will only translate language within comments? The project has many files, being able to operate on all of them at once would also be fantastic.

I'm currently copying-and-pasting into Google Translate, and while this is less than ideal, it can at least get me some answers.

Brown
  • 1,132
  • 1
  • 13
  • 33
  • arg, got a similar problem.. You find a solution? – MikeSchem Mar 06 '17 at 22:30
  • 1
    Unfortunately, no. I had briefly considered writing a tool to do it, as suggested by Pekka and Sean below, but I ended up just slogging through it with Google Translate. In fact, the biggest issue ended up not being the comments, it was variable names! We're all taught that single-letter variable names are horrible choices, but when the variable names are in another language, they may as well be "foo", "bar", or "x"! – Brown Mar 08 '17 at 15:15
  • Hah, brutal.... – MikeSchem Mar 08 '17 at 16:21
  • Always wondered how people who don't speak english code – MikeSchem Mar 08 '17 at 16:21
  • The same way people who DO speak English write code, I suppose. In our case, the software was written by a German, in Germany, for other Germans. Plus, the author's English was far better than MY German, so I cut him some slack. I can't fault the author for the comments and variable names, however the bugs are another story! – Brown Mar 09 '17 at 16:22
  • No, I mean the key words of a programing language are all in english. If you are doing a for loop in english "for" makes sense while in german that would be a "zum" loop (according to google translate). – MikeSchem Mar 09 '17 at 17:51

2 Answers2

0

I would only know exactly how to do this in java, but I am sure there is a way to do this in C as well, as the tools exist:

  1. Grab a parser that understands C source files (this one sounds ok, but I don't know much about C)
  2. build a syntax tree. iterate over all nodes of the tree, replacing the text of all comment nodes with translated text.
  3. write the tree back to a new source file (perhaps in a different directory).
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

Very broadly, this should be possible to do using Google translation's Ajax API and a regex function that can deal with callbacks - I don't think JS's built-in regex functions are up to the task but I'm sure there are libraries out there. You would have to build a regular expression that can isolate the comments, send each chunk to the API, and return the translated result in the callback function.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088