GUI for “Translate Shell”

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

  1. Select the text to translate in your word processor or text editor.
    • If nothing is selected the clipboard content will be used.
  2. 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.

:high_brightness: To bring up the Settings prompt again launch the macro with the Command (⌘) key pressed. :high_brightness:

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:


:bomb: 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 something UTF-8, for example en_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)


7 Likes

Update 2018-06-05

In the section “Issues & Troubleshooting” of the post above I have mentioned a Perl workaround to make the Translate Shell script work correctly with non-ASCII chars from the DeepL engine.

As I have learned today, that workaround is no longer necessary if you use…

  • the current version of the Translate Shell script (0.9.6.7) and
  • install a current version of bash (4.4.23)

The bash that comes with macOS High Sierra is version 3.2.57. You can install bash 4 with Hombrew:

brew install bash

This will currently install bash 4.4.23. For the purpose of the macro it is not necessary to add anything to /etc/shells

If you prefer not to install bash 4 you can of course still use the Perl fix as shown in the post above.

1 Like

Tom, as you know I am a shell scripting / bash neophyte (not even that good).
I don't need your Translate Shell stuff, but should I upgrade to bash 4 anyway?

If so, does it completely replace the current version? Is there anything special I need to do after the upgrade?

And finally, will bash 4 keep me out of trouble? LOL

As always, thanks for your valuable guidance.

I don’t now. The relevant stuff in the case here is a better/different Unicode handling by bash 4, told me the author of Translate Shell.

Besides that I have no clue what the major differences are. I also worked with bash 3 till now.

Here are the complete release notes covering bash 2 through 4.4. — I’m going to read them… one of these days :wink:

If so, does it completely replace the current version?

You can uninstall it at any time with brew uninstall bash. Then the old, preinstalled bash 3 will be used. (Bash 4 via Hombrew will go to /usr/local/bin, whereas the system’s bash 3 is in /bin. So nothing gets overwritten or so.)

Is there anything special I need to do after the upgrade?

Hombrew says, for full usage in the interactive shell the path of bash 4 should be added to /etc/shells. I think this is a security thingy.

As long as you have set your Terminal to use the default login shell (see screenshot below) and /usr/local/bin is on your PATH it will be used by the Terminal automatically.

Terminal prefs, General tab:

31-pty-fs8

Similar of course for KM: /usr/local/bin must be in your ENV_PATH variable. But I guess it is there already, otherwise KM would not find any of your Homebrew-installed tools.

3 Likes

Hello Tom,

Your macro using Deepl seems very interesting and exactly what I was looking for.

I tried your macro (german → english). The output with Deepl is gibberish, most likely because I did not install Bash

  • concerning Bash, where do you recommend I install it from. Is there any way that Bash can mess up my Mac or create a security risk ?

  • I appended the highlighted section below to the execute shell script action of the macro. Did I append it at the right place in the right action?

Thanks VERY much !!

image

Hi Ronald,

the question is not if you have installed Bash (every Mac has it), it’s rather about Bash 4.

Please see my post above.

Have you seen my reply to JM?

Seems correct. But the workaround with the Perl script was before I learned that the new version of Translate Shell will work correctly if Bash 4 is used. In other words: if you’re going to install Bash 4 you don’t need the Perl appendix.

But:

According to your other screenshot (“line 1: trans: command not found”) I rather have the impression that you didn’t install Translate Shell at all (or the path is not known to KM).

Please see…

  • the “Requirements” section of my original post and
  • the “Issues and Troubleshooting” section, especially the first list item with the link for the ENV_PATH variable
1 Like

thank you Tom for your answer.
I am clearly over my head, and will look for a simpler solution
thanks again

I think you are overcomplicating it:

It’s as simple as this:

  1. Install Translate Shell (can be done in one minute via Homebrew)
  2. Install Bash 4 (can also be done via Homebrew) —or— use the Perl workaround
  3. Make sure your “ENV_PATH” KM variable is set appropriately —or— call the tool in the shell script actions directly with /usr/local/bin/trans

Each of the steps is explained in the posts above.

Don’t hesitate to ask, if something is unclear in my explanations.


Two of these things — ① learning to use Homebrew (or another package manager) and ② setting up ENV_PATH — are also pretty much required for any other KM macros that make use of non-standard command line tools.

So, even if at the end you don’t use this macro, you’ll have done the necessary preparations to use other shell script macros.

1 Like

thanks very much.. I will try it out. You are very patient. Thanks !

1 Like

I am working on it

Hi Tom,
I installed Translate Shell and Bash 4.
I added ENV_PATH to my KBM variables.

When I gave ENV_PATH a value of /usr/local/bin/trans as per your message, the macro output did not contain useful translation info.

When I gave ENV-PATH the value /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin which I found in the link 'appropriately' (Make sure your “ENV_PATH” KM variable is set appropriately ) in your reply, the output contains the translation (which reads The russian displeasure at the meat counter) as per below but also extra lines of error messages.

thanks !

image

and here is another translation

image

When I gave ENV-PATH the value /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin which I found in the link 'appropriately'

Yep, this is the right direction. Personally I would make it:

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

Just for the sake of completeness; yours should work also.


The output from the screenshots you posted makes me think that this is the output as intended for the Terminal. (The output for the Terminal contains coloring informations and such.) My macro attempts to avoid the Terminal-specific output by using the -brief switch in the shell script actions.

Please make sure that that switch is still there in your shell script actions. Or just re-download the macro in case of doubt.


Well, basically it works now! Just some minor hurdles!

1 Like

yes, thanks very much for your patience.
I will check -brief
The new path you suggested did not change anything

Funny thing: it is quite slow. A few seconds at least for each translation which is usually immediate

thanks again very much

Something that occurs to me now: maybe it needs gawk (GNU-awk).

However, in that case it shouldn’t have returned you any translation at all. I’m confused, but to be sure, install it with:

brew install gawk

This depends on the engine you choose. Google usually is faster than DeepL.

You can choose the engine by holding down ⌘ while selecting the macro from the palette.

1 Like

In any case: any speed issue is not an issue we can resolve on our side. The deciding factor is the time the engine needs to deliver the results (including connection speeds). The time consumed by the shell script or by the macro is negligible.

1 Like

thank you !

Thanks for the GUI wrapper and the hints, I made it work in a version for me, but DeepL shortens the text to a paragraph, is that happening for everyone, or is there something missing/wrong?