I want to switch languages on Wikipedia with shortcut keys

I want to switch languages on Wikipedia with shortcut keys.

For example, I want to jump to a French page when I press a shortcut key that I have decided.
I tried using the action "Click Google Chrome Link" and entering "Français" in the Click Link.
But this fails.
Since specifying the version of Wikipedia, the characters "Français" is displayed in the page.
I'm new to Keyboard Maestro and I'm not sure.
Need to do a more complex setup?
Thank you.

You could use Found Image [Keyboard Maestro Wiki] where a screenshot of the language link is the image to be used. This will work, when the relevant section is on the screen.

Thank you.
But isn't there a way to do a text search normally?

Here is something to try and it’s best explained with an example:

The English language page for the Hoover Dam is at: https://en.wikipedia.org/wiki/Hoover_Dam

If you then replace the en in the URL with fr and open it in the browser it loads the French language version of the page at https://fr.wikipedia.org/wiki/Barrage_Hoover

I don’t know how but WP automatically replaces Hoover_Dam in the URL with Barrage_Hoover

I’ve only done this manually, but you could use KM to get the URL from the browser, then perform the replacement in the URL and finally reload the page.

EDIT: just returned to my Mac and tried this out. Here's a KM macro that switches from an English WP page to the French version. It obviously can be extended to prompt for a target language and switch appropriately for example.

Download Macro(s): Test Switch Wikipedia Language from English to French.kmmacros (3.1 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

It's a very useful approach.

Sometimes, inevitably, the page mappings are not direct (the final sections of the urls diverge).

For example:

https://fr.wikipedia.org/wiki/Gironde_(Révolution_française)

probably maps most closely to:

https://en.wikipedia.org/wiki/Girondins

I only tried this approach because "why not?" and I wasn't sure it would always work so I'm glad you've found a counter-example. Hopefully the OP will take this on board :wink:

1 Like

Even if it takes you to a disambiguation page ( as in that example ) it's still very useful.

Many thanks.

1 Like

It looks like another approach is to use a Click Front Browser Link action with arguments like:

  • //*[@lang='fr']
  • //*[@lang='en']

In experiment here this seems to flip happily between, for example, a pair like:

  • https://fr.wikipedia.org/wiki/Gironde_(Révolution_française)
  • https://en.wikipedia.org/wiki/Girondins

because if we look at the 49 languages drop-down on Wiki pages, its language-specific links turn out to be helpfully equipped ( usually ? always ? not sure ...) with the name of specific matching pages, in the form (in this case from the en Girondins page, pointing towards the fr Gironde page):

<a class="autonym" lang="fr" dir="ltr" 
href="https://fr.wikipedia.org/wiki/Gironde_(R%C3%A9volution_fran%C3%A7aise)" 
title="Gironde (Révolution française) – French" hreflang="fr">Français</a>

So perhaps, for example, in the case of fren, something like:

Toggle Wiki language.kmmacros (5.0 KB)

2 Likes

Great detective work!

You could probably devise fractionally more efficient XPaths, for example:

  • //ul/li/a[@lang='fr']
  • //ul/li/a[@lang='en']

but the principle is there.

Here is a variant for cases where you want to cycle between two or more languages.

For example you could cycle between six languages, perhaps:

fr → en → de → nl → it → es → fr ...

by setting local_LangTokens to the following space-separated list of language tokens

or to some other shorter or longer space-separated list of the standard two-letter codes, in the macro below:

(Something that I would usually do in a script, but here's an exercise in sticking to the rich set of blocks and tokens that Keyboard Maestro offers out of the box)


Cycle Wiki language.kmmacros (11 KB)

2 Likes

Thank you for your research. I actually want to use languages such as Japanese and Korean, but this method didn't work. Even if I change en in the URL to jp or kr, it will not work.

I actually want to use languages such as Japanese and Korean, but this method didn't work. Even if I change en in the URL to jp or kr, it will not work.

I think you're just using the wrong codes.

(If you use the web inspector in your browser on the other language links, you will see that the wikipedia links use ja and ko for those languages).

2 Likes

Sorry, my explanation was insufficient.
For example, for English and French, just change the URL from "en" to "fr".

For example, in the Girondin English Wikipedia, as you experimented, changing the URL from en to fr works.

However, try changing the URL manually to jp or ko. It will be failed.
For English and French, the last word of the URL is changed automatically, but for Japanese and Korean it doesn't, so it failed.

Thank you.

In other words, I think that the method of changing the URL is not sufficient, and I still need to click on the "English" or "日本語(This means Japanese)" or "한국어(This means Korean)" text from the language options.

In the first place, isn't KM's action "Click Google Chrome link" an action that clicks the word "Japanese" on the page if I set the word "Japanese"?

Thank you so much.

As I said, the code used by wikipedia is not jp.

It's ja

The "Cycle Wiki Language" macro above is working fine with the following setting for example:

and cycles between English Japanese Korean, and back to English.

If I start at:

on first use of the macro with the setting en ja ko

it cycles to:

and on next use, to:

This works because on the Wikipedia pages

  • the 日本語 link has the attribute lang=ja
  • the 한국어 link has the attribute lang=ko

and you can achieve these jumps directly with Keyboard Maestro actions like:

Click Front Browser link: //ul/li/a[@lang='ja']

Click Front Browser link: //ul/li/a[@lang='ko']


On the Korean page, for example, the link to the Japanese page is as below. Note the attribute value lang="ja", which suffices to locate and click this link with an XPath expression.

<a class="autonym" lang="ja" dir="ltr" 
href="https://ja.wikipedia.org/wiki/%E5%9C%8F%E8%AB%96" 
title="圏論 – 일본어" hreflang="ja">日本語</a>
2 Likes

I'm so sorry. I made a stupid mistake. Certainly, as you said twice for me, it is "ja" and "ko". Please excuse my stupidity.

However, a new problem appeared.
I set both the macro, but when I ran it, an error appeared.

The macro that toggles between two languages and the cycles through three languages were both errors.

I'll describe the steps I took.
(1) I downloaded your macro.
(2) Menu of KM → File → Import → Import Macros Safely...
(3) I created a group that fires in Chrome and set it as follows.

However, when I click shortcut key on Wikipedia, this error has appeared.

I also ran it in secret mode to make sure no extensions were interfering, but it's the same.

Chrome version is 114.0.5735.133(Latest at the moment)
Thank you so much...

Not sure what this Gyazo site is, but I won't click on those links to see your images.

If you want us to view images, you can drag image files directly into one of these editing boxes – there's no need to require us to navigate to unknown 3rd party sites.

This is becoming a bit protracted – if you are struggling to make use of the macros supplied, you may be able to save time for yourself and others by constructing your own, less complicated, macro using KM actions like:

Front Browser XPath Click.kmactions (486 Bytes)

and

Front Browser Control.kmactions (486 B)

Keyboard Maestro Export

2 Likes

Here you go:

image

At a wild guess it's because ja isn't an available choice when already on a Japanese language page.

2 Likes