# Command line japanese-english machine translation

I'm writing a small app for translating Japanese text to English. The idea is to split the text into paragraphs, machine translate each paragraph and then let user correct the translated text. The app will be constructed in a certain way to make it convenient to use (in case you wonder why I really bother writing the thing instead of trying my luck with an existing application). Now the problem is machine translation of Japanese to English.

I'd need a 3rd party tool which I can call from my app.

At first I thought that I'll just use Google Translator for this, but it turns out (unlike the web page and Android app) Google Translator API is a paid service and I don't feel like paying anything for a project that I'm working on in my spare time (much less telling Google my credit card number).

I'm looking for an alternative (preferably an offline one). Something like a command line app that runs under windows.

I searched the depths of internet but found mostly nothing (in the offline department). Well I did come across a few open source translation engines, but to use them, I would first have to teach them some Japanese*.

I also came across libtranslate which apparently just uses online services (odds are none of those are still accessible). I managed to compile the thing on Windows, but no matter what I do, I can't get it to load the services list.

*If there is nothing out there I'll try tackling that. The Kyoto Free Translation Task seems to provide in depth information on the subject. But understanding all this and the applying will require a lot of work/time (which I'd like to avoid if possible).

• It would probably look better if you post your solution as an answer instead of an edit to the question – Tymric Oct 4 '14 at 19:35

Translate Shell might be what you are looking for; it is free, supports Japanese.

There is a gotcha, though...in order to make it run on a Windows environment, you'd need to install Cygwin. Not sure whether this is a deal-breaker or not.

Running it using cygwin on Windows is a little inconvenient for me, but after a little tinkering I managed to "extract" necessary files from cygwin and now I can run it directly from windows (or from cmd). To be able to run Translate Shell outside of cygwin, you will need to copy the following files (from cygwin of course):

• sh.exe
• cyggcc_s-1.dll
• cygwin1.dll
• cygintl-8.dll
• cygncursesw-10.dll
• gawk.exe
• cyggmp-10.dll
• cygmpfr-4.dll
• cygiconv-2.dll

There is one more thing that is necessary for this to work. You have to add the path to the folder containing gawk.exe to the environment variable PATH. I kept all the copied files in one directory and tested it in cmd, so running

SET PATH=%PATH%;.


once made it possible to run Translate Shell.

Example use:

sh trans.sh "Saluton, Mundo"


When using Translate Shell in a batch file setting encoding to UTF-8 might be required (if the language your translating from\to uses UTF-8 characters). This can be achieved using:

chcp 65001

To sum it up if you want to make a batch file that translates some text etc. it will look like this:

chcp 65001
SET PATH=%PATH%;.
sh trans.sh "Saluton, Mondo"


If you want to save the output as a file, you can add > output.txt to the last line.

sh trans.sh "Saluton, Mondo" > output.txt


There are some special character\sequences [1m and [21m inside such file (note those sequences start with an invisible ASCII character 27; I think they indicate start and end of some lines). I haven't figured out exactly what they are, but those are likely to make it easier to parse the output anyway.

UPDATE

To make it easier to use Translate Shell under windows you can put this batch in the folder containing the script and the executables:

@echo off  >nul 2>nul
chcp 65001 >nul 2>nul
SET PATH=%PATH%;. >nul 2>nul
sh trans.sh %*


Assuming your copy of Translate Shell is named trans.sh and that you named this batch trans.bat, you can use it in command line (and elsewhere) like you would use Translate Shell on some linux. In other words you can use it like this:

trans "Saluton, Mondo"

• This is actually looking good. I think I can copy some necessary files from cygwin and be able to run it "directly" under windows. Though, under the hood Translate Shell is just a hack of Google Translator webpage (I'm not sure what google thinks about that). Still that's better than nothing. All in all this is similar to libtranslate, but it looks like I should be able to use it since it doesn't rely on external files placed in impossible locations (impossible under windows). – jahu Oct 4 '14 at 9:13
• If you can "escape" the Cygwin, please do come back and share how you did it. Hope it works well for you. – elder elder Oct 4 '14 at 9:27
• Check out my question for info on how to run Translate Shell "without" Cygwin under Windows. – jahu Oct 4 '14 at 12:15
• Thank you for the detailed tutorial, I appreciate your follow-up. In case you are interested, you could now create an installer package for your application using, for example, an INNO Setup script. This way you could automate the steps above, as well as perform a cleanup when the application is uninstalled. – elder elder Oct 4 '14 at 14:14
• That's not really necessary. If I alter the PATH environment variable within a batch file, the changes are lost afterwards (so nothing really to clean up). I can also include listed dll and exe files with my app (so no installation would be necessary). – jahu Oct 4 '14 at 18:09