Let me explain. Suppose I want to teach Python to someone who only speaks Spanish. As you know, in most programming languages all keywords are in English. How complex would it be to create a program that will find all keywords in a given source code and translate them? Would I need to use a parser and stuff, or will a couple of regexes and string functions be enough?
If it depends on the source programming language, then Python and Javascript would be the most important.
What I mean by “how complex would it be” is that would it be enough to have a list of keywords, and parse the source code to find keywords not in quotes? Or are there enough syntactical weirdnesses that something more complicated is required?
If all you want is to translate keywords, then (while you definitely DO need a proper parser, as otherwise avoiding any change in strings, comments &c becomes a nightmare) the task is quite simple. For example, since you mentioned Python:
I hope it’s obvious how to generalize this to “translate” any Python source and in any language (I’m supplying only a very partial Italian keyword translation dict). This emits:
(strange though correct whitespace, but that could be easily enough remedied). As an Italian speaker I can tell you this is terrible to read, but that’s par for the course for any “programming language translation” as you desire. Worse, NON-keywords such as
rangeremain un-translated (as per your specs) — of course, you don’t have to constrain your translation to keywords-only (it’s easy enough to remove theifthat does that above;-).