wxPython i18n on windows
こないだの続き
python i18n on windows - gymnoの自由談
今度はGUIプログラムでwxPythonを使ってリソースを日本語化する
#!/usr/bin/env python # -*- coding: iso-8859-15 -*- # generated by wxGlade 0.6.3 on Tue Apr 21 00:57:37 2009 import wx import sys,os # begin wxGlade: extracode # end wxGlade MENU_FILE_EXIT=101 class MyFrame(wx.Frame): def __init__(self, *args, **kwds): # -- begin byHand basepath = os.path.abspath(os.path.dirname(sys.argv[0])) localedir = os.path.join(basepath, "locale") domain = "wxi18n" langid=wx.LANGUAGE_DEFAULT print "base = ",basepath print "localedir = " , localedir print "domain = ", domain print "langid = ", langid mylocale = wx.Locale(langid) #mylocale.AddCatalogLookupPathPrefix(localedir) #mylocale.AddCatalog(domain) # Set up Python's gettext mytranslation = gettext.translation(domain, localedir, [mylocale.GetCanonicalName()], fallback = True) mytranslation.install() # -- end byHand # begin wxGlade: MyFrame.__init__ kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) # Menu Bar self.myFrame_menubar = wx.MenuBar() wxglade_tmp_menu = wx.Menu() wxglade_tmp_menu.Append(MENU_FILE_EXIT, _("&Exit"), "", wx.ITEM_NORMAL) self.myFrame_menubar.Append(wxglade_tmp_menu, _("&File")) self.SetMenuBar(self.myFrame_menubar) # Menu Bar end self.panel_1 = wx.Panel(self, -1) self.__set_properties() self.__do_layout() # end wxGlade # -- begin byHand wx.EVT_MENU(self, MENU_FILE_EXIT, self.OnFileClose) # -- end byHand def __set_properties(self): # begin wxGlade: MyFrame.__set_properties self.SetTitle(_("wx locale sample")) # end wxGlade def __do_layout(self): # begin wxGlade: MyFrame.__do_layout sizer_1 = wx.BoxSizer(wx.VERTICAL) sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0) self.SetSizer(sizer_1) sizer_1.Fit(self) self.Layout() # end wxGlade # -- begin byHand def OnFileClose(self,event): self.Close(True) # -- end byHand # end of class MyFrame class MyApp(wx.App): def OnInit(self): wx.InitAllImageHandlers() myFrame = MyFrame(None, -1, "") self.SetTopWindow(myFrame) myFrame.Show() return 1 # end of class MyApp if __name__ == "__main__": import gettext #gettext.install("app") # replace with the appropriate catalog name app = MyApp(0) app.MainLoop()
gettextツール群についてはGetText for Windowsを利用した
python付属のものだとmsgmergeがなかったりして不便だったので
これらをc:直下に入れて c:/gnu/binにパスを通せば多少はlinuxっぽく便利に使える 本当はcygwinなぞ入れねばならないのだろうけど
今回はほぼ完全に
Internationalization - wxPyWikiに準拠
例によってwxGladeの使い方が覚えられないので
wxWidgets でクロスプラットフォーム GUIアプリを作ろう
を参考にしつつ
xgettextでpoをつくり 翻訳 この際
"Content-Type: text/plain; charset=Shift_JIS\n
をきちんと修正しないと怒られる
ファイルのバージョン管理をするならmsgcatによるソートが不可欠
参考 http://makunouchi.jp/zope3/8440297694
で cuiの時と同様 msgfmtでmoをつくり 今回は${アプリのディレクトリ}/locale/ja/LC_MESSAGES/に入れた
これが本当にLinuxに持っていっても動くかどうかは検証していない
とりあえずこれでいいや