How can I quickly switch between two out of multiple languages?

1. Toggle between two (fixed) languages

What you describe is basically a keyboard shortcut to toggle between two input languages. The script below will offer the option.

#!/usr/bin/env python3
import subprocess
import sys

args = sys.argv[1:]
k = ["org.gnome.desktop.input-sources", "current"]

def get(command):  return subprocess.check_output(command).decode("utf-8")

currlang = get(["gsettings", "get", k[0], k[1]]).strip().split()[-1]
newlang = args[1] if currlang == args[0] else args[0]
subprocess.Popen(["gsettings", "set", k[0], k[1], newlang])

How to use

  1. Copy the script into an empty file, save it as set_lang.py
  2. In a terminal window, run the command:

    gsettings get org.gnome.desktop.input-sources sources
    

    This will output like:

    [('xkb', 'us+intl'), ('xkb', 'us'), ('xkb', 'nl')]
    

    This list represents your input languages. The index of the languages is equal to the position in the list, starting with 0, e.g. ('xkb', 'us') has index 1 (in my case).

  3. Now test-run the script to toggle between two indexes. To toggle between ('xkb', 'us+intl') and ('xkb', 'nl') (index 0 and 2):

    python3 /path/to/set_lang.py 1 3
    

    where bot languages are represented by the arguments

    1 3
    
  4. If all works fine, add it to a shortut key: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

    python3 /path/to/set_lang.py 1 3
    

    to a shortcut of your choice.

You can then use the existing shortcut to browse all languages, or (of course) the menu.

Short explanation

  • The available languages can be retrieved by the command:

    gsettings get org.gnome.desktop.input-sources sources
    
  • The currently set language can be retrieved by the command:

    gsettings get org.gnome.desktop.input-sources current
    

    which will output (a.o.) the index of the currently set language.

  • We can set the language by (e.g.) the command:

    gsettings set org.gnome.desktop.input-sources current 0
    

    to set the language to the first in the list (index 0)

In short: if we run the script we two languages (indices) as arguments, the script will look what is the current index, will switch to the other one.




2. Toggle between the two last used languages

The version of the script below will switch between the two last used languages, which turned out to be similar to the behaviour of MacOs.

#!/usr/bin/env python3
import subprocess
import os

k = ["org.gnome.desktop.input-sources", "current"]
stored = os.path.join(os.environ["HOME"], ".lastlang")

def get(command):  return subprocess.check_output(command).decode("utf-8")

currlang = get(["gsettings", "get", k[0], k[1]]).strip().split()[-1]
try:
    newlang = open(stored).read().strip()
except FileNotFoundError:
    newlang = currlang

open(stored, "wt").write(currlang)
subprocess.Popen(["gsettings", "set", k[0], k[1], newlang])

I added this version as an additional option. The two last used languages will persist (be remembered) after reboot.

How to use

  1. Copy the script into an empty file, save it as switchlang.py
  2. Test- run the script by the command:

    python3/ path/to/switchlang.py
    

    After first run, switch language from the menu, now run

    python3/ path/to/switchlang.py
    

    again. From then on, the script will always toggle between the last two used languages.


Go: >System Settings > Text Entry

  and add languages to "Input sources to use:"

enter image description here

make sure to check "Show current input source in menu bar" just select input sources from the drop down menu.