This macro is a GUI wrapper the the excellent Translate Shell script by Mort Yao.
The great thing with Translate Shell is that it works with many different translation engines, for example Google, Bing, Yandex and —since a couple of days— also with the fine DeepL engine.
You get all in one, without the need for different scripts.
The Translate Shell script works perfectly fine in the Terminal, so this macro is really just a “convenience” macro that provides a bit more comfort.
Features
- Text is copied automatically.
- You can query up to three translation engines simultaneously.
- You can query translations to different target languages simultaneously.
- Optionally you can choose the target language from a quick list prompt.
This macro covers only the main use-case, that is, translating junks of text. Translate Shell has many more little great features that are not accessible through the macro, and some of them are designed to be run in the Terminal.
It is really worth to read through the documentation on GitHub!
Requirements
Of course you need to have Translate Shell installed on your Mac. Fortunately this can be done via Homebrew:
brew install --HEAD translate-shell
The --HEAD
switch installs the most recent version, which currently (2017-12-16) is the only one that includes the DeepL engine. To install the “stable” release just omit that switch.
Usage
- Select the text to translate in your word processor or text editor.
- If nothing is selected the clipboard content will be used.
- Launch the macro from the Global palette.
- The translations will be displayed in one or more KM windows.
Settings / First run
On the first run of the macro you will be presented with the Settings prompt:
I think the prompt is pretty much self-explanatory.
Some notes:
-
Target language: Use two-letter ISO codes. If you leave this field empty the system language of your Mac will be the target language. Combine multiple languages with a
+
- Source language: In most cases “Auto” is fine.
- Engine: This is the only setting you have to set.
- Engine 2, Engine 3: Additional engines to query. The results of each engine will be displayed in a separate window. To disable the additional engines set them to “None” or just deselect the checkbox.
After the first run (and you have selected an engine) the Settings prompt will not appear again.
To bring up the Settings prompt again launch the macro with the Command (⌘) key pressed.
List prompt
If you are frequently switching between different target languages, you have the option to activate a quick selection via a List Prompt:
To activate the List Prompt, bring up the Settings window and enter “list” in the “Target language” field:
Issues & Troubleshooting
- Make sure the
ENV_PATH
variable is correctly set in KM. - If you get encoding issues, then set KM’s
ENV_LANG
variable to somethingUTF-8
, for exampleen_US.UTF-8
- When using the DeepL engine there seem to be encoding issues on the Mac, no matter how the
LANG
variable is set.
Workaround for the DeepL encoding issue
To get correct UTF-8 chars with DeepL simply append (without newline) this to the existing script in the Shell Script action (including the |
at the beginning):
| perl -Mopen=':std',':encoding(UTF-8)' -pe '$_ =~ s/\\u(.{4})/chr(hex($1))/eg;'
Note that this is only necessary if you are using DeepL and you are dealing with non-ASCII chars (that is, translating to other languages than English). Google and the other engines work fine without it.
This is meant as a temporary workaround until the issue gets solved by Translate Shell itself.
Resources:
Macro
Translate via Translate Shell.kmmacros (22.1 KB)