Is it possible to assign default keyboard language for specific applications? I know that there is an option to always start an application with a default keyboard language but this is not what I want.
I'm looking for something similar to "fixed workspaces" which you can set using CompizConfig (You set Chrome to always open at X1Y1, terminal at X2Y1 etc). Something where I would set Chrome: Czech, terminal: English, Spotify: English, …
Best Answer
Introduction
The script bellow sets the language for each user-defined program, according to the position of that language in the language menu. For instance, if my order is: English (1), Chinese(2), and Russian(3), I can set Firefox to have language 2, terminal to have language 1, and LibreOffice to have language 3.
The script comes in two parts: part one is the actual script that does the job, the second script serves as a controlling element. The idea is to run the language setting script as a start-up application and whenever you need to manually change language - double click the shortcut to the controller script.
Pre-requisites
wmctrl
program withsudo apt-get install wmctrl
.Script
Controller Script
Launcher (.desktop) file for set-lang.sh script
Launcher (.desktop) file for set-lang-controller.sh
Making the script work
bin
. You can do it in the file manager or use the commandmkdir $HOME/bin
in the terminalbin
folder create two files:set-lang.sh
andset-lang-control.sh
. Save script toset-lang.sh
and controller script toset-lang-control.sh
. Make both scripts executable withsudo chmod +x $HOME/bin/set-lang-control.sh $HOME/bin/set-lang.sh
.desktop
files. One isset-lang.desktop
. Must be placed in the hidden.config/autostart
directory. Second one isset-lang-controller.desktop
, may be placed in yourbin
folder. Next drag and pin to the launcher theset-lang-controller.desktop
file. This will become the shortcut for temporarily stopping and resuming the script execution.NOTE that the line
Exec=
must be altered to have your actual user name in the path to script (because that's your actual home directory). For example, mine would beExec=/home/serg/bin/set-lang.sh
Explanation and customization:
The script itself runs in an infinite while loop and checks the current active window. If the current active window matches one of the options in the case structure, we switch to the appropriate language. To avoid constant setting, each part of the case structure has if statement that checks if the language has already been set to the desired value.
The double clicking on the launcher for
set-lang-controller.sh
will check the status of theset-lang.sh
script; if the script is running - it will be paused, and if the script is paused it will be resumed. A notification will be shown with appropriate message.In order to customize the script, you can open desired application(s), run
wmctrl -lx
and note the third column - the window class. Sample output:Select the appropriate window classes for each program. Next, go to the part of the script that allows customization and add two entries for PROG_CLASS and LANG. Next add the appropriate entry in the case structure.
For instance, if I want to add, LibreOffice's Writer, I open LibreOffice Writer window, go to terminal and run
wmctrl -lx
. It will tell me that the Writer window has classlibreoffice.libreoffice-writer
. Next I will go to the script, addPROG_CLASS_4=libreoffice.libreoffice-writer
andLANG4=3
in the appropriate area. Notice matching number 4. Next, go to case structure, and add the following entry between last;;
andesac
:Again, notice the $ sign and matching number 4.
Also, if the script is running as an autostart item and you want to stop it temporarily to customize it, use
pkill set-lang.sh
and resume withnohup set-lang.sh > /dev/null 2&>1 &
Small note: another way to find out the window class for a program (that stuff that goes before single round bracket in the case structure) is to use this
xprop
andawk
oneliner :xprop | awk '/WM_CLASS/ {gsub(/"/," "); print $3"."$5}