My own tool, a bit silly, but it does the job. Some code bases come with comments in Japanese, and if you don’t read Japanese, you first need to find out where those comments are and what they say. comments-tool walks through a directory, picks out Japanese comments from C and C++ files and collects all of them in one JSON file, so you can get them translated in one go.
The code is on GitHub.
What it does#
The script reads .c, .cpp, .h and .hpp files, either a whole directory of them or a single file, looks for // and /* ... */ comments and keeps those with hiragana, katakana or kanji in them. Every such comment lands in translations.json as a key with an empty value, and you fill in translations later, by hand or with whatever translator you like. Comments that are already in the file aren’t added a second time.
There are a few side jobs as well: listing every Japanese character with its line and position, listing non-ASCII characters, and finding and removing BOM characters.
It doesn’t translate anything and doesn’t touch your code, except with --remove-bom. Its regular expressions are simple, so a // inside a string literal counts as a comment too.
How to run it#
You need Python 3.8 or newer. With uv:
git clone https://github.com/marcinklimek/comments-tool.git
cd comments-tool
uv venv && source .venv/bin/activate
uv pip install -e .
comment_tool --directory path/to/source # collect Japanese comments
comment_tool --scan --file path/to/file.cpp # show where the Japanese characters are
comment_tool --scan-bom # find BOM characters
comment_tool --remove-bom # remove themRun without a flag, the tool collects comments and writes translations.json and translation.log to the current directory.
Example#
For a file with this line:
int speed = 10; // 速度の初期値
the run adds this to translations.json:
{
"// 速度の初期値": ""
}In the log you’ll find which file and which line each comment came from. The empty value is where the translation goes (“initial value of the speed”).