Python-Ref > GUI programming with PyGTK > Localization using gettext > GUI localization
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

GUI localization

The following code is the localized version of the second example on UIManager.
Expand/Shrink
  1   # ensure that PyGTK 2.0 is loaded - not an older version
  2   import pygtk
  3   pygtk.require('2.0')
  4   # import the GTK module
  5   import gtk
  6   import gettext
  7   gettext.install( "localization2-1", "./", False)
  8   
  9   class MyGUI:
 10   
 11     ui = '''<ui>
 12       <menubar name="MenuBar">
 13         <menu action="Label">
 14           <menuitem action="Fill"/>
 15           <separator/>
 16           <menuitem action="Clear"/>
 17         </menu>
 18         <menu action="About" name="AboutMenu">
 19           <menuitem action="Version"/>
 20           <menuitem action="ShowAbout"/>
 21         </menu>
 22       </menubar>
 23       <toolbar name="Toolbar">
 24         <toolitem action="Fill"/>
 25         <toolitem action="Clear"/>
 26         <separator/>
 27         <toolitem action="Version"/>
 28         <toolitem action="ShowAbout"/>
 29       </toolbar>
 30       </ui>'''
 31   
 32     def __init__( self, title):
 33       self.window = gtk.Window()
 34       self.title = title
 35       self.window.set_title( title)
 36       self.window.set_size_request( 260, 160)
 37       self.window.connect( "destroy", self.destroy)
 38       self.create_interior()
 39       self.window.show_all()
 40   
 41     def create_interior( self):
 42       self.mainbox = gtk.VBox()
 43       self.window.add( self.mainbox)
 44       # create action group
 45       self.actiongroup = gtk.ActionGroup('MyActionGroup')
 46       self.actiongroup.add_actions(
 47         [('About', None, _('_About')),
 48          ('Label', None, _('_Label')),
 49          ('Clear', gtk.STOCK_CLEAR, _('_Clear'), None, _('Clear the label'), self.clear_label),
 50          ('Fill',  gtk.STOCK_COLOR_PICKER, _('_Fill'),  None, _('Fill the label'), self.fill_label),
 51          ('ShowAbout', gtk.STOCK_ABOUT, _('_About'), None, _('Show about information'), self.show_about),
 52          ('Version', gtk.STOCK_INDEX, _('_Version'), None, _('Show version'), self.show_version),
 53          ])
 54       # the uimanager
 55       self.uimanager = gtk.UIManager()
 56       accelgroup = self.uimanager.get_accel_group()
 57       self.window.add_accel_group( accelgroup)
 58   
 59       self.uimanager.insert_action_group( self.actiongroup, 0)
 60       self.uimanager.add_ui_from_string( self.ui)
 61       # menu bar
 62       self.menu_bar = self.uimanager.get_widget( "/MenuBar")
 63       self.mainbox.pack_start( self.menu_bar, expand=False, fill=False)
 64       self.menu_bar.show()
 65       # right justify the about menu
 66       self.uimanager.get_widget('/MenuBar/AboutMenu').set_right_justified( True)
 67       # toolbar
 68       toolbar = self.uimanager.get_widget('/Toolbar')
 69       self.mainbox.pack_start( toolbar, expand=False)
 70       # label for answer
 71       self.answer_label = gtk.Label( _("Hello menus"))
 72       self.mainbox.pack_start( self.answer_label, padding=10)
 73       self.answer_label.show()
 74       # show the box
 75       self.mainbox.show()
 76   
 77     def main( self):
 78       gtk.main()
 79   
 80     def destroy( self, w):
 81       gtk.main_quit()
 82   
 83     def clear_label( self, w):
 84       self.answer_label.set_label( "")
 85   
 86     def fill_label( self, w):
 87       self.answer_label.set_label( "Hello")
 88   
 89     def show_about( self, w):
 90       self.answer_label.set_label( "Created by Beda Kosata")
 91   
 92     def show_version( self, w):
 93       self.answer_label.set_label( "2.1")
 94   
 95   if __name__ == "__main__":
 96     m = MyGUI( _("UIManager example"))
 97     m.main()
Screenshot:
Program screenshot localization2-1en.png
Doba běhu: 7330.6 ms
Expand/Shrink
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2008-04-29 08:36+0200\n"
"PO-Revision-Date: 2008-04-29 08:39+0200\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: localization2-1.py:49
msgid "Clear the label"
msgstr "Vyčisti popisek"

#: localization2-1.py:50
msgid "Fill the label"
msgstr "Naplň popisek"

#: localization2-1.py:51
msgid "Show about information"
msgstr "Zobraz informace o programu"

#: localization2-1.py:52
msgid "Show version"
msgstr "Zobraz verzi"

#: localization2-1.py:71
msgid "Hello menus"
msgstr "Ahoj menu"

#: localization2-1.py:96
msgid "UIManager example"
msgstr "Příklad na UIManager"

#: localization2-1.py:47 localization2-1.py:51
msgid "_About"
msgstr "O _Aplikaci"

#: localization2-1.py:48
msgid "_Label"
msgstr "_Popisek"

#: localization2-1.py:49
msgid "_Clear"
msgstr "V_yčistit"

#: localization2-1.py:50
msgid "_Fill"
msgstr "_Naplnit"

#: localization2-1.py:52
msgid "_Version"
msgstr "_Verze"
Argumenty příkazové řádky:
LC_ALL=cs python localization2-1.py 
  1   # ensure that PyGTK 2.0 is loaded - not an older version
  2   import pygtk
  3   pygtk.require('2.0')
  4   # import the GTK module
  5   import gtk
  6   import gettext
  7   gettext.install( "localization2-1", "./", False)
  8   
  9   class MyGUI:
 10   
 11     ui = '''<ui>
 12       <menubar name="MenuBar">
 13         <menu action="Label">
 14           <menuitem action="Fill"/>
 15           <separator/>
 16           <menuitem action="Clear"/>
 17         </menu>
 18         <menu action="About" name="AboutMenu">
 19           <menuitem action="Version"/>
 20           <menuitem action="ShowAbout"/>
 21         </menu>
 22       </menubar>
 23       <toolbar name="Toolbar">
 24         <toolitem action="Fill"/>
 25         <toolitem action="Clear"/>
 26         <separator/>
 27         <toolitem action="Version"/>
 28         <toolitem action="ShowAbout"/>
 29       </toolbar>
 30       </ui>'''
 31   
 32     def __init__( self, title):
 33       self.window = gtk.Window()
 34       self.title = title
 35       self.window.set_title( title)
 36       self.window.set_size_request( 260, 160)
 37       self.window.connect( "destroy", self.destroy)
 38       self.create_interior()
 39       self.window.show_all()
 40   
 41     def create_interior( self):
 42       self.mainbox = gtk.VBox()
 43       self.window.add( self.mainbox)
 44       # create action group
 45       self.actiongroup = gtk.ActionGroup('MyActionGroup')
 46       self.actiongroup.add_actions(
 47         [('About', None, _('_About')),
 48          ('Label', None, _('_Label')),
 49          ('Clear', gtk.STOCK_CLEAR, _('_Clear'), None, _('Clear the label'), self.clear_label),
 50          ('Fill',  gtk.STOCK_COLOR_PICKER, _('_Fill'),  None, _('Fill the label'), self.fill_label),
 51          ('ShowAbout', gtk.STOCK_ABOUT, _('_About'), None, _('Show about information'), self.show_about),
 52          ('Version', gtk.STOCK_INDEX, _('_Version'), None, _('Show version'), self.show_version),
 53          ])
 54       # the uimanager
 55       self.uimanager = gtk.UIManager()
 56       accelgroup = self.uimanager.get_accel_group()
 57       self.window.add_accel_group( accelgroup)
 58   
 59       self.uimanager.insert_action_group( self.actiongroup, 0)
 60       self.uimanager.add_ui_from_string( self.ui)
 61       # menu bar
 62       self.menu_bar = self.uimanager.get_widget( "/MenuBar")
 63       self.mainbox.pack_start( self.menu_bar, expand=False, fill=False)
 64       self.menu_bar.show()
 65       # right justify the about menu
 66       self.uimanager.get_widget('/MenuBar/AboutMenu').set_right_justified( True)
 67       # toolbar
 68       toolbar = self.uimanager.get_widget('/Toolbar')
 69       self.mainbox.pack_start( toolbar, expand=False)
 70       # label for answer
 71       self.answer_label = gtk.Label( _("Hello menus"))
 72       self.mainbox.pack_start( self.answer_label, padding=10)
 73       self.answer_label.show()
 74       # show the box
 75       self.mainbox.show()
 76   
 77     def main( self):
 78       gtk.main()
 79   
 80     def destroy( self, w):
 81       gtk.main_quit()
 82   
 83     def clear_label( self, w):
 84       self.answer_label.set_label( "")
 85   
 86     def fill_label( self, w):
 87       self.answer_label.set_label( "Hello")
 88   
 89     def show_about( self, w):
 90       self.answer_label.set_label( "Created by Beda Kosata")
 91   
 92     def show_version( self, w):
 93       self.answer_label.set_label( "2.1")
 94   
 95   if __name__ == "__main__":
 96     m = MyGUI( _("UIManager example"))
 97     m.main()
stderr:

(localization2-1.py:8201): Gtk-WARNING **: Locale not supported by C library.
	Using the fallback 'C' locale.
Screenshot:
Program screenshot localization2-1cs.png
Doba běhu: 1553.1 ms