Pastey Multi-language support for EuGTK

--# MULTI-LINGUAL (uses ini type files to translate captions)
 
-- Run this program as follows: eui multi es (for spanish) 
-- or gr (greek), ge (german), fr (french), ru (russian) 
-- If run without a 2-letter language code, lang.en will be the default. 
-- Additional lang.* files can be created or modified as needed, by the user. 
 
constant default_lang = canonical_path("~/demos/multi-language/lang.en") 
 
include GtkEngine.e 
include GtkSettings.e 
include std/io.e 
 
chdir(prog_dir) 
 
object ini = 0 
 
if length(command_line()) > 2 then -- language file was specified; 
 ini = format("~/demos/multi-language/lang.[3]",command_line()) 
 ini = canonical_path(ini) 
else 
 ini = default_lang -- use default if not specified; 
end if 
 
object bgcolor 
 
constant 
 win = create(GtkWindow,"name=MainWindow,border=10,$delete_event=Exit"), 
 pan = create(GtkBox,"orientation=vertical"), 
 left = create(GtkBox,"orientation=vertical"), 
 right = create(GtkBox,"orientation=vertical"), 
 img = create(GtkImage,"name=Image1,icon-name=face-kiss,pixel-size=64"), 
 lbl = create(GtkLabel,"name=Label1,font=8,text=Nothing to see here"), 
 box = create(GtkButtonBox), 
 options = create(GtkComboBoxText), 
 btn1 = create(GtkButton,"name=Button1,image=quit,label=_Exit,use_underline=1,$clicked=Exit"), 
 btn2 = create(GtkButton,"name=Button2,image=help,label=_Help,use_underline=1,$clicked=Help"), 
 btn3 = create(GtkButton,"name=Button3,image=style,label=_Background,use_underline=1,$clicked=BGColor") 
 
 add(options,{"en","es","bn","ru","gk","hn","fr","ge"}) 
 set(options,"active",1) 
 add(win,pan) 
 add(pan,{img,left,right}) 
 add(left,lbl) 
 add(right,options) 
 add(box,{btn1,btn2,btn3}) 
 pack(pan,-box) 
 
 settings:Load(ini)  
 set(lbl,"markup",read_file(ini)) 
 
 connect(options,"changed","LoadIni") 
 
show_all(win) 
main() 
 
--------------------------------- 
global function LoadIni(atom ctl) 
--------------------------------- 
ini = "~/demos/multi-language/lang." & get(ctl,"active text") 
ini = canonical_path(ini) 
settings:Load(ini) 
set(lbl,"markup",read_file(ini)) 
return 1 
end function 
 
---------------------------- 
global function BGColor() -- 
---------------------------- 
bgcolor = sprintf("#%06x",rand(#FFFFFF)) 
set(win,"background",bgcolor) 
return 1 
end function 
 
------------------------- 
global function Help() -- 
------------------------- 
object capt = settings:Get(ini,"MainWindow.HelpCaption") 
object title = settings:Get(ini,"MainWindow.HelpTitle") 
object text = settings:Get(ini,"MainWindow.HelpText") 
 
if atom(text) then 
 return Info(,,"Sorry","No translated help text was available","reverted to English") 
end if 
 
return Info(,capt,title,text) 
 
end function 
 
------------------------- 
global function Exit() -- 
------------------------- 
 
  settings:Save(ini,"MainWindow.size") 
  settings:Save(ini,"MainWindow.position") 
 
  if object(bgcolor) then -- color was changed, otherwise, skip; 
   settings:Save(ini,"MainWindow.background",bgcolor) 
  end if 
 
return Quit() 
end function 

1. Comment by irv 1 month ago

Note: controls need to be named if you plan to use ini type files to save their settings. This allows you to use name.property to refer to existing GTK widget properties, like size, position, or background. These are reloaded automatically when you call settings:Load(ini).

You can also declare non-properties which which can be manually saved and reloaded using settings:Save(ini,<non-property-name>) and settings:Get(ini,<non-property-name>). For example: MainWindow.HelpText