Translation Macro

has someone already created a macro for fast and simple translations (eg using google translator)?
You might ask the user to enter in an input box the text to translate or automatically translate the text in the clipboard.

Enrico,

Nothing against KM, but I use Alfred and KM, sometimes together. For executing URL calls with an argument, it is very easily implemented in in Alfred - that is how I do my translation calls.

As I study Chinese, if someone has other translation-related scripts or approaches, I certainly would like to hear them.

I gave it a shot. The script comes from https://github.com/soimort/translate-shell , which I installed with Homebrew, but you can download directly. I had to play around with the PATH to get it to function because KM doesn't use your bash settings. You also have to set LANG for the default language. The macro is set to copy a selection, use -trans- in brief mode, then paste the result. But -trans- has a ton of functions that you could customize into multiple macros.

2 Likes

You may be able to use

pbpaste | /usr/local/bin/trans  -brief

Rather than using a Keyboard Maestro variable.

Also, by using the full path to the command you likely obviate the need to set the PATH (unless trans has some other dependencies).

Thanks. I wasn’t familiar with pbpaste.

There were other dependencies, and they were all installed by Homebrew.

Here's an improved version that can be used when you're not in a text field.

Before I run down a rabbit hole, I assume that I need the translate-shell to be installed for this, with Homebrew, etc. (of which I currently have no knowledge other than what I am reading here…

Is there a KM self-contained way to pull this off (e.g., the bash script can be in KM, etc.). I guess with everything else on my plate, not looking to go installing libraries, troubleshooting path issues, etc., if I can ignore it.

On github, there are a number of examples of command-line interfaces for google translate. If they’re easier to use, they tend to have dependencies that make homebrew very useful for installation. This python script may fit into a single KM macro: https://github.com/terryyin/google-translate-python . But homebrew is worth the time if you use the terminal at all.

As I suspected, I tried yesterday and things started going wacko. For example, I would get to the last step and it would fail. I do a lot of work with translations so my gut feeling is that this in fact IS worth my time to figure out.

I’ll report back with my success, or my lack thereof…

Hey Ed,

I spent quite a while last night wrestling with that bash script. I got it working within a few minutes, but I couldn't get consistent results from KM when switching from words to phrases.

On the other hand I just downloaded the Python script Keith posted, and it's working quite well.

https://raw.githubusercontent.com/terryyin/google-translate-python/master/translate.py

I put it in /usr/local/bin/ and made it executable:

chmod +x <path-to-file>

Google Translate via Python Script.kmmacros.zip (1006 Bytes)

At the moment I have it set to translate to French, but that's easy to change.

#! /usr/bin/env bash
export LANG="en_US.UTF-8";
PATH=/usr/local/bin:$PATH;
# echo $KMVAR_TranslateText
translate.py -t fr "$KMVAR_TranslateText"

Syntax: translate -f zh -t ja 我是谁

The bash script is almost 2000 lines and designed to run from the Terminal. This Python script is 70 and is a little more agnostic.

Including the time it took me to build the macro, the Python script took me about 10 minutes to get working.

-Chris

Hello - newbie here!

I do much translation from English to Welsh.

I’ve got the above KM macro working, with 2 problems.

1 I couldn’t find the ‘Syntax: translate -f zh -t ja 我是谁’ line

2 Everytime the text to translate has a ’ character in it, it comes back as ’

Any help much appreciated as this is a massive potential timesaver for me.

Very many thanks.

God bless,

Robert

This is the syntax for the command if you are using the script from the terminal. -f is ‘from’, -t is ‘to’.

The whole command: translate , then -f , followed by the language code of the source language, then -t, followed by the language code of the target language, then the word to translate. (With a space between each component.)

With this knowledge you can now customize the macro to your needs. In your case it’s simple: just replace the “fr” (after the ”-t”) in the last line of the Execute Shell Script action with “cy”:

translate.py -t cy "$KMVAR_TranslateText"

If it doesn’t work, try this:

translate.py -f en -t cy "$KMVAR_TranslateText"

Not sure what you mean.

I didn’t try the macro but when I use the script in the Terminal and want to translate a phrase I have to wrap it in straight double quotes.

Been a while on this one.

I simply want to highlight Chinese Text and have it translated simultaneously to English:

  1. To the clipboard, AND
  2. To a popup window.

The original translate.py program specified in the link in this posting is no longer available so I found an alternative version. I am not clear on whether that is creating problems.

The current macro "Google Translate via Python Script.kmmacros.zip" does not seem to bring up any window. As well, if I simply try to execute the "Execute Shell Script" within the macro by itself (Cog>Try Action). No window is opened either.

Of course, if there is a simpler way to do this, I am open to suggestions.

47 AM

There is now a more capable script available, called “Translate Shell”.

Some time ago I’ve written a macro for that:

It already writes the translation to a KM window, and you can easily add the actions to make it write to the clipboard also.

Hey Tom - thanks for the reply. I will give your suggestions a try and report back.

Hi Tom,

A friend just pointed out your Translate Shell GUI for KM and I’ve got everything set up, but get the following when I try to translate anything:

“/var/folders/9v/rqgh_tyx76gdrl14ds7x1tpw0000gn/T/Keyboard-Maestro-Script-3862940C-5270-4B50-978D-702C4BDB9F61: line 1: trans: command not found”

I can confirm that Translate Shell is in my path:

which trans
/usr/local/bin/trans

and that it actually works when I run it from the command line.

I can’t, however, find “Keyboard-Maestro-Script-3862940C-5270-4B50-978D-702C4BDB9F61” when I navigate to “/var/folders/9v/rqgh_tyx76gdrl14ds7x1tpw0000gn/T/”.

I’m a bit stumped as I’ve not used any sort of scripting for a couple of years now, so am having a difficult time parsing what I need to change to fix this.

Any help would be greatly appreciated.

Will

I The important thing is that also KM needs to know about the path of your shell executables. My macro contains a link to the Wiki article that explains it.

I‘m on my iPhone ATM, I can explain it in details later, if still necessary.

PS: This

Thanks, Tom.

I think that must be it. I’ve gotten around this to some extent by adding the following to the beginning of each of the “Execute Shell Script” options:

+++++++++++++++++++++

#! /usr/bin/env bash
export LANG=“en_US.UTF-8”;
PATH=/usr/local/bin:$PATH;

+++++++++++++++++++++

That being said (and if I’ve understood you correctly), do I need to expressly go into my KM prefs and set “ENV_PATH” to be my path as quoted above or have I gotten things confused (probably likely).

And by the way: I can’t seem to get Bing to work as an option.

Just to prove it wasn’t a problem with syntax/Translate Shell I did the following:

trans -e google -b :de "Roses are red and violets are blue."
Rosen sind rot und Veilchen sind blau.

trans -e yandex -b :de "Roses are red and violets are blue."
Rosen sind rot und veilchen sind blau.

trans -e bing -b :de “Roses are red and violets are blue.”
[nothing]

trans -e deepl -b :de "Roses are red and violets are blue."
Rosen sind rot und Veilchen sind blau.

Has anyone else found this problem as well?

Thanks for your help.

Will

No, you don’t have to.

You have two possibilities:

Possibility 1:

You can call the path explicitly with every script acion. As you have done; and it works perfectly fine.

Possibility 2:

Set the the KM variable correctly in KM, i.e.:

ENV_PATH

~/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin

ENV_LANG

en_US.UTF-8

ENV_LC_ALL

en_US.UTF-8

Notes:

If you don’t have a ~bin directory, don’t set it.
For the language-specific stuff; you can also use, for instance,

de_DE

or

fr_FR

depending on your language. The important stuff is the UTF-8 at the end. (For instance fr_FR.UTF-8)

But this (the language settings) sould be OK on your system, unless you have meddled around before.

The difference is, if you set the KM Environment variables appropriatetly, you don’t have to set the path each time explicitly with each script action. That’s all, but that’s why I recommned to do it.

Thanks, Tom. It’s been a few years since I’ve needed to monkey about with this sort of stuff, so I appreciate your patient and very detailed help.

Have a good weekend.

Will