Switch between languages in Linux. Write in multiple languages with same keyboard.

Polyglot Tux
Image: Polyglot Tux (License: CC-BY-ND-SA Marcelo Canina)

How to set up multiple keyboard layouts from console.

Published:
Last modified:

Overview

A guide for polyglots to use Cyrillic letters, Chinese characters, Arabic, Latin letters and any other language in Linux, with the ability to easily switch between them in an X11 graphical environment or console.

There are several input methods that can be used, just a plain usage of xkb which just changes keyboard layouts or more complete methods which include spell checkers, character and word suggestions, and support for Asiatic scripts like ibus.

With ibus (preferred one)

IBus is an intelligent input bus for Linux/Unix. It can help with a fast switching between any language, spell checking and also including emoticons.

sudo apt install ibus-libpinyin ibus-table-emoji ibus-sunpinyin ibus-cangjie ibus-typing-booster ibus-zhuyin

Make Ubuntu use it

Make Ubuntu use ibus as the default input method by using the following command im-config and then choosing it from the list.

It will automatically create the $HOME/.xinputrc file with that information to be loaded at the next start up.

Unicode character and Emojis

Display character suggestions and emojis correctly: sudo apt install xfonts-unifont ttf-unifont fonts-noto-color-emoji

Note: sudo apt get install fonts-noto can also be used but it takes more than 700Mb of disk usage.

Spellchecking

For having the spellchecking and dictionary functionality it needs to have the hunspell dictionaries already installed.

sudo apt install hunspell-ru hunspell-en-us hunspell-es hunspell-pt-br hunspell-it hunspell-de-de  hunspell-es hunspell-fr hunspell-ru hunspell-ar

Then when you select each language from the preferences windows, you will see that some has a tick in the spellchecking feature, those are the dictionaries which have been installed.

Text predictor

To see a list of word suggestions when typing a word, you need to first choose the language you want at the ibus selection window, and then choose ibus-typing-booster[^ibus-typing-booster] from the same window.

^ibus-typing-booster

The predictor works on the previously selected language.

Setup

Set it up with ibus-setup. Configuration data will be saved with dconf.

dconf is a simple tool for manipulating a dconf database1

Migrate config

After adding languages and setting up If you want to have the same ibus configuration between different computers, you need to dump the configuration using dconf and then load it in the new computer.

To check the current configuration use dconf dump /desktop/ibus/:

<pre class="shell">
<samp>
<span class="shell-prompt">$</span> <kbd>dconf dump /desktop/ibus/</kbd>
general]
engines-order=['xkb:us::eng', 'libpinyin', 'xkb:latam::spa', 'xkb:br::por', 'xkb:ru::rus', 'xkb:fr::fra', 'typing-booster', 'xkb:ru:phonetic:rus', 'xkb:ara::ara', 'xkb:jp::jpn', 'xkb:de::ger', 'xkb:it::ita']
preload-engines=['xkb:us::eng', 'libpinyin', 'xkb:ara::ara', 'xkb:fr::fra', 'xkb:latam::spa', 'xkb:ru::rus', 'xkb:jp::jpn', 'xkb:de::ger', 'xkb:it::ita', 'xkb:ru:phonetic:rus', 'xkb:br::por', 'typing-booster']
use-global-engine=false
version='1.5.22'

[general/hotkey]
triggers=['<Control><Alt><Shift>space']

[panel]
custom-font='Sans 20'
show=2
use-custom-font=true

[panel/emoji]
favorites=['✋', '😁']
</samp>
</pre>

Then write it to a file with <dconf dump /desktop/ibus/ > ~/ibus-custom.cfg.

And in the new computer populate the subpath after copying the configuration file: <dconf load /desktop/ibus/ < ~/ibus-custom.cfg.

Now all the languages and custom shortcuts are available at the new computer, but we need to be sure to replicate the same packages in the new computer.

With xkb

The X11/Xorg server uses XKB, the X Keyboard Extension, to control the available keyboard layouts.

If we want to use many languages with the same keyboard, we will have to address four main topics: model, layout, variant and options.

To specify the different languages, we use the command setxkbmap which sets the keyboard using the X Keyboard Extension.

It is used like: setxkbmap [-model xkb_model] [-layout xkb_layout] [-variant xkb_variant] [-option xkb_options] so the in each topic we will see how to fulfill each parameter.

1. Current configuration

You can see your current configuration with setxkbmap -query or for further details setxkbmap -print -verbose 10 .


$ setxkbmap -query
rules:      evdev
model:      pc105
layout:     us

In Debian/Ubuntu systems the default keyboard layout is described in /etc/default/keyboard2.


$ cat /etc/default/keyboard
 KEYBOARD CONFIGURATION FILE

 Consult the keyboard(5) manual page.

XKBMODEL="pc105"
XKBLAYOUT="us"
XKBVARIANT=""
XKBOPTIONS=""

BACKSPACE="guess"

At this stage we pay particular attention to the current “physical” keyboard model.

XkbModel selects the keyboard model. This has an influence only for some extra keys your keyboard might have. The safe fallback are pc104 or pc105. But for instance laptops usually have some extra keys, and sometimes you can make them work by simply setting a proper model.

2. Languages

Select which keyboard layouts are needed, i.e.: the languages you will use.

XkbLayout selects the keyboard layout. Multiple layouts may be specified in a comma-separated list, e.g. if you want to quickly switch between layouts.

For a complete list of keyboard layouts and its descriptions check /usr/share/X11/xkb/rules/base.lst.

The layout name is often the typical two letter country code. e.g.: for Russian: ru.

3. Variants

For each layout, a variant can be chosen.

XkbVariant selects a specific layout variant. For instance, the default sk variant is qwertz, but you can manually specify qwerty, etc.

The full list of variants can also be seen at /usr/share/X11/xkb/rules/base.lst.

For example you can choose:

  • For a Russian layout, a phonetic variant is available, which will make it writing Russian in a Latin/US keyboard very intuitive.

    ! variant
    ...
      phonetic_winkeys ru: Russian (phonetic, with Win keys)
    ...
    

    And to see specifically the mappings it has, consult the proper layout file, e.g. for Russian (ru): /usr/share/X11/xkb/symbols/ru and locate the xkb_symbols "phonetic". It will have all the key mappings like: key <LatT> { [ Cyrillic_te, Cyrillic_TE ] }; where it specifies that the Latin T key will be the Cyrillic version: т.

The full list of variants available can be seen online at: https://manpages.ubuntu.com/manpages/disco/man7/xkeyboard-config.7.html

4. Switch between layouts

XkbOptions contains some extra options (comma-separated). Used for specifying layout switching, notification LED, compose mode etc.

To switch between the keyboard layouts we rely on the options parameter of XKB.

It already comes with a list of possible key combinations that will be switching between all the specified layouts listed in /usr/share/X11/xkb/rules/base.lst, starting with grp: and ending with toggle.


$ grep "grp:.*toggle" /usr/share/X11/xkb/rules/base.lst
grp:toggle           Right Alt
  grp:lalt_toggle      Left Alt
  grp:caps_toggle      Caps Lock
  grp:shift_caps_toggle Shift+Caps Lock
  grp:alt_caps_toggle  Alt+Caps Lock
  grp:shifts_toggle    Both Shift together
  grp:alts_toggle      Both Alt together
  grp:ctrls_toggle     Both Ctrl together
  grp:ctrl_shift_toggle Ctrl+Shift
  grp:lctrl_lshift_toggle Left Ctrl+Left Shift
  grp:rctrl_rshift_toggle Right Ctrl+Right Shift
  grp:ctrl_alt_toggle  Alt+Ctrl
  grp:alt_shift_toggle Alt+Shift
  grp:lalt_lshift_toggle Left Alt+Left Shift
  grp:alt_space_toggle Alt+Space
  grp:menu_toggle      Menu
  grp:lwin_toggle      Left Win
  grp:win_space_toggle Win+Space
  grp:rwin_toggle      Right Win
  grp:lshift_toggle    Left Shift
  grp:rshift_toggle    Right Shift
  grp:lctrl_toggle     Left Ctrl
  grp:rctrl_toggle     Right Ctrl
  grp:sclk_toggle      Scroll Lock
  grp:lctrl_lwin_toggle Left Ctrl+Left Win

The full list of options available can be found online at: https://manpages.ubuntu.com/manpages/disco/man7/xkeyboard-config.7.html .

5. Testing

To test it works as desired, we specify the command and try to switch between layouts.

For example for English and Russian (phonetic) keyboards switching with both CTRL keys pressed at the same time: setxkbmap -model pc105 -layout us,ru -variant ,phonetic -option grp:ctrls_toggle.

Then we press both CTRL and start typing in Русский.

6. Persisting Changes

setxkbmap changes the layout for the current X session only.

To keep changes in the next reboot we can choose between:

  • Manually changing the configuration files:
    • xinitrc or
    • xprofile.
  • Using the command localectl which automatically saves the new configuration.

6.1 Manual

Create the X11 configuration file /etc/X11/xorg.conf.d/00-keyboard.conf with the new data:

Section "InputClass"
        Identifier "system-keyboard"
        MatchIsKeyboard "on"
        Option "XkbLayout" "us,ru"
        Option "XkbModel" "pc105"
        Option "XkbVariant" ",phonetic"
        Option "XkbOptions" "grp:ctrls_toggle"
EndSection

6.2 Automatic

locatectl automatically creates/updates the required configuration file /etc/default/keyboard (in Ubuntu) or /etc/X11/xorg.conf.d/00-keyboard.conf.

It is used like: $ localectl [--no-convert] set-x11-keymap layout [model [variant [options]]]

Following the above example:


$ localectl set-x11-keymap us,ru pc105 ,phonetic grp:ctrls_toggle
$ cat /etc/default/keyboard
XKBMODEL=pc105
XKBLAYOUT=us,ru
XKBVARIANT=,phonetic
XKBOPTIONS=grp:ctrls_toggle
BACKSPACE=guess

7. Graphical indicator

Finally we add an X11 keyboard indicator and switcher.

gxkb is an utility that switchs the language selecting it from a menu, and shows an icon with the current active layout.

If you change the layout with the above combination key, gxkb also updates and show the current layout.

screenshot 1

To install it in Debian/Ubuntu: sudo apt-get install gxkb.

Keyboards layout references

To generate any graphical representation of the keyboard layout, use gkbd-keyboard-display -l "<language_code> or xkbprint.

Examples:

  • for English US version: gkbd-keyboard-display -l us
  • for English US with altgr-intl variant: gkbd-keyboard-display -l “us(altgr-intl)"
Variants for each language can be found at /usr/share/X11/xkb/symbols/

Each letter on the keyboard can have up to 4 symbols associated. They are defined in /usr/share/X11/xkb/symbols/ for each language in the form of key <KEYCODE> { [ symbol1, symbol2, symbol3, symbol4 ] };.

Each symbol can be accesed with:

  • unmodified
    • <letter>
    • press a letter without any other modificator
  • shift
    • press SHIFT + <letter>
    • access the upper left letter symbol definition
  • altgr
    • press ALT gr + <letter>
    • access the bottom right letter symbol definition
  • shift+altgr
    • press SHIFT + ALT gr + <letter>
    • access the upper left letter symbol definition

altgr stands for Alt Graph and is the right alt key. “Is a modifier key used to type characters that are unusual for the locale of the keyboard layout”. 3

Example key layout and how to access each symbol:

SHIFTSHIFT + ALT gr
without modifiersALT gr

Arab

Arab keyboard layout without variants.

Code: ar.

Arab layout

Brazilian

Brazilian portuguese keyboard layout.

Code: br.

Brazilian layout

English (US)

English US keyboard layout.

Code: us(altgr-intl).

English with altgr international layout

French

French keyboard layout.

Code: fr.

French layout

Italian

Italian keyboard layout.

Code: it.

Italian layout

Russian

Russian keyboard main layout is called ЙЦУКЕН4, the keyboard with the most popular Cyrillic letters layout.

Code: ru.

Russian layout

Russian phonetic

Russian phonetic variant layout lets you use a common US English layout (qwerty) but typing each letter phonetic equivalent in Cyrillic.

Code: ru(phonetic).

Russian phonetic layout

Spanish

Spanish keyboard layout.

Code: es.

Spanish layout

References

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


Setup from console to switch between multiple languages in Linux. Use Cyrillic, Pinyin and Latin characters.

Clutter-free software concepts.
Translations English Español

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·