Writing Hiragana in Linux

If you study japanese, or speak it to some degree, you probably write a lot of japanese in your computer. If japanese is not your main language, then you probably have to switch keyboard layouts back and forth.

There are a few Japanese input options available for Linux, although I haven’t tried all of them. The one I use is mozc. It is in active development, autocompletion suggestions are very good and it integrates well with my GNOME desktop. It is pretty flawless.

I just had one small inconvenience…

Switching between keyboard layouts is as easy as hitting Super + Space. However, when you first switch to mozc, it defaults to “direct input” AKA roman letters. If I’m switching from my english layout to mozc, it’s because I definitely don’t want to write roman letters. Now, I have to open the keyboard layout menu, choose mozc, then hiragana. Sure, I only have to do this once per session, but it’s too many clicks.

Too many clicks to get here.

Fortunately, mozc is open source, so I asked on github if there is any way to configure mozc to default to hiragana. The next day I found a reply saying that there is no such configuration, however this can be achieved by changing the value of one constant in the source code and recompiling mozc.

Apparently some people disagree with my ideas and expect mozc to be turned off for IBus 1.5.0 or later. This is enforced in the propery_handler.cc file

#if IBUS_CHECK_VERSION(1, 5, 0)
const bool kActivatedOnLaunch = false;
#else
const bool kActivatedOnLaunch = true;
#endif // IBus>=1.5.0

I can’t argue with this decision, but I’m free to fork the project and modify it as I like, so I can replace those 5 lines of code with this:

const bool kActivatedOnLaunch = true;

With this small change, the IME will be activated on launch, regardless of the IBus version. Now all that’s left to do is uninstall the version of mozc that is currently running on my machine and build the one that I just modified. Easy right?

The build instructions for mozc on github are well detailed, however there’s probably an easier way to do this. Since I’m running Arch Linux, I originally installed mozc from the AUR with pacman. The AUR already provides a solid script for pacman to download the sources, compile and install mozc. Instead of going though all the building steps by myself, I can just download the PKGBUILD file and edit it so that when it looks for the source code, it downloads it from my fork instead of Google’s repository.

First, I forked the project on Github and pushed my changes. Then I downloaded the latest mozc snapshot from the AUR. The PKGBUILD originally contained these lines:

_zipcoderel=201607
_mozcrev=5d0e6164f5e88248990fa9488eef42dc7f042c8b
pkgbase=mozc
pkgname=mozc
true && pkgname=(‘mozc’)
pkgver=2.18.2598.102
pkgrel=1
arch=(‘i686’ ‘x86_64’)
url=”http://code.google.com/p/mozc/"
license=(‘BSD’ ‘custom’)
makedepends=(‘python2’ ‘git’ ‘ninja’ ‘clang’)
#source=(“${_svndir}/${_svnmod}::svn+${_svntrunk}”
source=(
mozc::git+https://github.com/google/mozc.git#commit=${_mozcrev}
http://downloads.sourceforge.net/project/pnsft-aur/mozc/x-ken-all-${_zipcoderel}.zip
http://downloads.sourceforge.net/project/pnsft-aur/mozc/jigyosyo-${_zipcoderel}.zip
)

There are only two lines that I needed to modify. The “source” variable has the URL of Google’s repo, I changed that to mine. Also, it uses a _mozcrev variable to specify a commit. I changed that too, so that it points to my latest commit. The result was this:

_zipcoderel=201607
_mozcrev=07f97a942573eead220cd2b108a926898788b2c9
pkgbase=mozc
pkgname=mozc
true && pkgname=(‘mozc’)
pkgver=2.18.2598.102
pkgrel=1
arch=(‘i686’ ‘x86_64’)
url=”http://code.google.com/p/mozc/"
license=(‘BSD’ ‘custom’)
makedepends=(‘python2’ ‘git’ ‘ninja’ ‘clang’)
#source=(“${_svndir}/${_svnmod}::svn+${_svntrunk}”
source=(
mozc::git+https://github.com/GAumala/mozc.git#commit=${_mozcrev}
http://downloads.sourceforge.net/project/pnsft-aur/mozc/x-ken-all-${_zipcoderel}.zip
http://downloads.sourceforge.net/project/pnsft-aur/mozc/jigyosyo-${_zipcoderel}.zip
)

Now all that’s left to do is build the package… but wait! There was a small error. Apparently the AUR package has not been updated recently and the latest version of the repository has a breaking change. The mozc_version_template.txt file has been renamed to mozc_version_template.bzl. The PKGBUILD file needs to parse this file to get the version number, but now that the extension has changed, it can no longer find it, so it throws an error. This can be easily solved by editing pkgver() to use the correct extension. It should look like this:

pkgver() {
. “${srcdir}/${pkgbase}/src/data/version/mozc_version_template.bzl”
printf “%s.%s.%s.%s” $MAJOR $MINOR $BUILD $REVISION
}

Now it points to the correct file. We are finally ready to build this and after restarting IBus, mozc will default to hiragana instead of direct input. Great! I don’t need a mouse anymore to change keyboard layouts. However, this means that everytime I want to update mozc, I must pull the changes to my fork and edit the PKGBUILD file to use the latest commit. I don’t update mozc very often, so I think it’s totally worth it.