Translating comments in a Visual Studio Solution?

Soldato
Joined
18 Jan 2006
Posts
3,104
Location
Norwich
OK, so this is probably a long shot at best, but I'm wondering if anyone has tried anything like this.

I'm consuming a very specialised library, for which I have the full source code. I've got a good idea of what the code is actually doing, and it's pretty easy to see what's going on.
However, the code is heavily commented, with XMLDoc etc. as well in Japanese, of which I have a limited understanding at best.

What I'd like is a way to actually get all this translated to save me having to manually copy + paste comments for translation if I'm stepping in the debugger.
 
Soldato
Joined
29 Aug 2006
Posts
4,139
Location
In a world of my own
I just asked ChatGPT for you.

Me: "write a python script to parse a text file, locate any Japanese language text in the file and translate to english"

Sure, I can help you write a Python script that will parse a text file to find any Japanese text and translate it into English. For this, we'll use a regular expression to detect Japanese characters and the googletrans library, which is a Python wrapper of the Google Translate API for text translation.

Before you can use this script, make sure to install the googletrans library if you haven't already. You can install it using pip:

pip install googletrans==4.0.0-rc1

Here's the python script:

import re
from googletrans import Translator, LANGUAGES

def detect_japanese(text):
# Regular expression for detecting Japanese characters (includes Kanji, Hiragana, and Katakana)
japanese_regex = r'[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]'
if re.search(japanese_regex, text):
return True
return False

def translate_to_english(text):
translator = Translator()
# Translate text to English
translation = translator.translate(text, src='ja', dest='en')
return translation.text

def process_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()

for line in lines:
if detect_japanese(line):
translation = translate_to_english(line)
print("Original: ", line.strip())
print("Translated: ", translation)
print() # Add an empty line for better readability

if __name__ == "__main__":
file_path = "path_to_your_file.txt" # Update this with the actual file path
process_file(file_path)

How the script works:​

  1. detect_japanese function: Uses a regex to detect any Japanese characters in the text.
  2. translate_to_english function: Uses the googletrans library to translate the detected Japanese text to English.
  3. process_file function: Reads a text file, checks each line for Japanese text, and prints both the original and translated text.

Usage:​

Replace "path_to_your_file.txt" with the path to your text file. When you run the script, it will output the original Japanese text and its English translation for each line containing Japanese characters.

Make sure that the text file you're using is encoded in UTF-8, or adjust the encoding in the script accordingly to match your file's encoding.
 
Last edited:
Associate
Joined
31 Dec 2010
Posts
2,460
Location
Sussex
Regex mania and GoogleSheet's Googletranslate() function?
=GOOGLETRANSLATE(A2, "ja", "en")

Or just the spreadsheet and copy the text as is.
Then work out a formula to look for any comment and send that text into the Googletranslate function.
If the formula proves too hard because of formatting, you might be do something like the below to get only the comments (if they start the lines)
Add a column before A,
put in =ROW()
Fill down,
copy all of this first column,
paste as text (so overwriting the ROW() formula with fixed row numbers).
Sort by the second column.
Hopefully any // or other comment lines will then be all grouped.
Translate only this.
Sort by the original row number in column A
 
Soldato
OP
Joined
18 Jan 2006
Posts
3,104
Location
Norwich
Hmm.
The ChatGPT idea is interesting, hadn't even thought of that direction.
Might fall over with the solution size, and I have a suspicion that it'll probably mangle stuff it shouldn't, but I'll have to see what happens there.

I can't see Google sheets working (this is a pretty large C# library), and AFAIK Copilot only translates between programming languages (pointless....)
 
Soldato
Joined
29 Aug 2006
Posts
4,139
Location
In a world of my own
Hmm.
The ChatGPT idea is interesting, hadn't even thought of that direction.
Might fall over with the solution size, and I have a suspicion that it'll probably mangle stuff it shouldn't, but I'll have to see what happens there.

I can't see Google sheets working (this is a pretty large C# library), and AFAIK Copilot only translates between programming languages (pointless....)

Don't use python then. You can use the same prompt to get C or C++ code to do the job and I'm pretty sure a Win32 compiled C .exe isn't going to struggle. :)
 
Back
Top Bottom