1#!/usr/bin/env ruby 2 3require 'tk' 4 5demo_dir = File.dirname($0) 6msgcat_dir = [demo_dir, 'msgs_rb2'].join(File::Separator) 7top_win = nil 8#msgcat = TkMsgCatalog.new('::tk') 9msgcat = TkMsgCatalog.new('::tkmsgcat_demo') 10default_locale = msgcat.locale 11#msgcat.load_rb(msgcat_dir) 12msgcat.load(msgcat_dir) 13 14col_proc = TkComm.install_bind(proc{|w, color, frame, label| 15 TkComm.window(frame).background(color) 16 Tk.update 17 TkComm.window(label).text( 18 msgcat["%1$s:: %2$s", 'Color', 19 color.capitalize]) 20 w.flash; w.flash 21 Tk.callback_break; 22 }, "%W") 23 24del_proc = TkComm.install_cmd(proc{top_win.destroy; top_win = nil}) 25 26err_proc = TkComm.install_cmd(proc{fail(RuntimeError, 27 msgcat['Application Error'])}) 28 29show_sample = proc{|loc| 30 top_win = TkToplevel.new(:title=>loc) 31 32 msgcat.locale = loc 33 #msgcat.load_rb(msgcat_dir) 34 msgcat.load(msgcat_dir) 35 36 TkLabel.new(top_win){ 37 text "preferences:: #{msgcat.preferences.join(' ')}" 38 pack(:pady=>10, :padx=>10) 39 } 40 41 lbl = TkLabel.new(top_win, :text=>msgcat["%1$s:: %2$s", 42 'Color', '']).pack(:anchor=>'w') 43 44 bg = TkFrame.new(top_win).pack(:ipadx=>20, :ipady=>10, 45 :expand=>true, :fill=>:both) 46 47 TkFrame.new(bg){|f| 48 ['blue', 'green', 'red'].each{|col| 49 TkButton.new(f, :text=>msgcat[col]){ 50 bind('ButtonRelease-1', col_proc, "#{col} #{bg.path} #{lbl.path}") 51 }.pack(:fill=>:x) 52 } 53 }.pack(:anchor=>'center', :pady=>15) 54 55 TkFrame.new(top_win){|f| 56 TkButton.new(f, :text=>msgcat['Delete'], 57 :command=>del_proc).pack(:side=>:right, :padx=>5) 58 TkButton.new(f, :text=>msgcat['Error'], 59 :command=>err_proc).pack(:side=>:left, :padx=>5) 60 }.pack(:side=>:bottom, :fill=>:x) 61 62 top_win 63} 64 65 66# listbox for locale list 67TkLabel.new(:text=>"Please click a locale.").pack(:padx=>5, :pady=>3) 68 69TkFrame.new{|f| 70 TkButton.new(f, :text=>msgcat['Exit'], 71 :command=>proc{exit}).pack(:side=>:right, :padx=>5) 72}.pack(:side=>:bottom, :fill=>:x) 73 74f = TkFrame.new.pack(:side=>:top, :fill=>:both, :expand=>true) 75lbox = TkListbox.new(f).pack(:side=>:left, :fill=>:both, :expand=>true) 76lbox.yscrollbar(TkScrollbar.new(f, :width=>12).pack(:side=>:right, :fill=>:y)) 77 78lbox.bind('ButtonRelease-1'){|ev| 79 idx = lbox.index("@#{ev.x},#{ev.y}") 80 if idx == 0 81 loc = default_locale 82 else 83 loc = lbox.get(idx) 84 end 85 if top_win != nil && top_win.exist? 86 top_win.destroy 87 end 88 top_win = show_sample.call(loc) 89} 90 91lbox.insert('end', 'default') 92 93Dir.entries(msgcat_dir).sort.each{|f| 94 if f =~ /^(.*).msg$/ 95 lbox.insert('end', $1) 96 end 97} 98 99top_win = show_sample.call(default_locale) 100 101# start eventloop 102Tk.mainloop 103