Search code examples
localizationgtkpygtkarabichebrew

PyGTK - localization with Right To Left languages (BiDi)


(Almost?) all of the material in the Web regarding PyGTK localization is discussing usage of gettext - i.e., how to properly show the translated strings.

But that's not enough... There are certain languages (Hebrew, Arabic and more) that are written from Right To Left, and therefore, the widgets should be 'swapped'. Packing 'Start' should be at the rightmost, and continue to the left.

I assume that locale.setlocale(locale.LC_ALL, '') should solve the problem. However, it didn't work (on Hebrew Windows 7 machine).

Here is a sample code, that tries to change the locale to Hebrew and displays 2 buttons - but they are still from Left To Right:

import gtk
import locale

locale.setlocale(locale.LC_ALL, 'Hebrew_Israel.1255')
print locale.setlocale(locale.LC_ALL)

window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.connect("destroy", lambda w: gtk.main_quit())

box1 = gtk.HBox(False, 0)
window.add(box1)

button1 = gtk.Button("first")
box1.pack_start(button1, True, True, 0)

button2 = gtk.Button("second")
box1.pack_start(button2, True, True, 0)

window.show_all()
gtk.main()

Solution

  • gtk.widget_set_default_direction(gtk.TEXT_DIR_RTL)

    This sets the default direction for widgets that don't call set_direction.