Graphical user interface

Graphical user interface works just as a program or a library. The only difference is that the file which contains the graphical user interface should not be written in OCaml. You have two cases :

You should use the first alternative : it is easier for a translator to extract strings, without having to compile your application ( it enables translators that don't know OCaml to help you ). In order to do so, you should use the native xgettext binary ( provided with gettext ). It should support the format of the translatable strings found in your GUI file ( for example, xgettext supports glade file ).

But for now you can only use the second alternative, because OCaml is not yet supported in xgettext. This should be fixed, once ocaml-gettext will be enough stable to become a back-end for xgettext [7].

In the two cases, you just have to add your GUI file ( in OCaml or native form ) to POTFILES.

Graphical user interfaces are good candidates for settings a fixed Init.codeset. Typically, GTK2 interfaces require to have these parameters set to UTF-8.

Example 3.12. gui.ml

(**************************************************************************)
(*  Ocaml-gettext : example code                                           *)
(*                                                                         *)
(*  Copyright (C) 2003, 2004, 2005 Sylvain Le Gall <sylvain@le-gall.net>   *)
(*                                                                         *)
(*  You can redistribute this library and/or modify it under the terms of  *)
(*  the GNU LGPL v2.1 with the OCaml static compilation exception.         *)
(*                                                                         *)
(*  Contact: sylvain@le-gall.net                                           *)
(**************************************************************************)

open GuiGettext.Gettext;;

(* Give access to the init of GuiGettext *)
let init = 
  Gettext.init
;;

(* Build a simple window that display your name *)
let hello_you name = 
  let spf x = Printf.sprintf x
  in
  let window = GWindow.window ~title:(s_ "Hello world !") ~border_width:12 () 
  in
  let label = GMisc.label ~text:(spf (f_ "Hello %s") name) ~packing:window#add ()
  in
  ignore (window#event#connect#delete ~callback:(fun _ -> false));
  ignore (window#connect#destroy ~callback:(fun _ -> GMain.Main.quit ()));
  window#show ();
  GMain.Main.main ()
;;

Example 3.13. ./program --gettext-dir ../build/share/locale --gettext-lang C --my-name Sylvain


Example 3.14. ./program --gettext-dir ../build/share/locale --gettext-lang fr --my-name Sylvain


Warning

If you build lablgtk™ application, you must keep in mind that some locale settings are made in the function GMain.Init. Those settings could override those done through the command line options. In order to correctly use ocaml-gettext, you must be sure to call GMain.Init before using Arg.parse with the ocaml-gettext args.



[7] For now, extracting strings from OCaml source file and glade file, requires to patch gettext™. You can find this path in patches. This patch will be sent to upstream author once it will be considered enough stable.