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.
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.
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:
- To the clipboard, AND
- 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.
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