How can I quickly switch between two out of multiple languages?
I have 3 input sources (languages) configured in Text Input Settings. Let us call them E (as in English) and A and B for two other languages. Mainly I switching between English and A or English and B and almost never between A and B. However switching keyboard shortcuts always cycle them in E->A->B or E<-A<-B direction. This is very inconvenient.
I am looking for a way to do this as it is implemented under MacOS, where switcher cycles between the 2 last languages by default, but you can force it to advance to 3rd one using a separate shortcut or via toolbar menu, as shown in the screenshot below:
Is it possible to configure something like this on Ubuntu?
EDIT: Jacob's solution below allows to create a custom shortcut to switch between two languages. I've modified his script to replicate MacOS switching scheme, where last 2 languages are cycles automatically. You can see it here. Blog post explaining details here.
Solution 1:
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
- Copy the script into an empty file, save it as
set_lang.py
-
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). -
Now test-run the script to toggle between two indexes. To toggle between
('xkb', 'us+intl')
and('xkb', 'nl')
(index0
and2
):python3 /path/to/set_lang.py 1 3
where bot languages are represented by the arguments
1 3
-
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
- Copy the script into an empty file, save it as
switchlang.py
-
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.
Solution 2:
Go: >System Settings > Text Entry
and add languages to "Input sources to use:"
make sure to check "Show current input source in menu bar" just select input sources from the drop down menu.