However it didn't work. I checked the .xsession and found that the .xim was not called at all. After grepping in /etc, I found that KDE4 uses its own Xsession under /etc/kde4/kdm instead of the standard one under /etc/X11/xdm. After I added this line
[ -f $HOME/.xim ] && . $HOME/.xim
before calling startkde in the Xsession file, it worked like a charm!