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 use glade file, so you should extract the strings from this file using xgettext
You use a hand written interface written in OCaml, the extraction of the strings follow the same way as a library.
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: a library to translate messages *) (* *) (* Copyright (C) 2003-2008 Sylvain Le Gall <sylvain@le-gall.net> *) (* *) (* This library is free software; you can redistribute it and/or *) (* modify it under the terms of the GNU Lesser General Public *) (* License as published by the Free Software Foundation; either *) (* version 2.1 of the License, or (at your option) any later version; *) (* with the OCaml static compilation exception. *) (* *) (* This library is distributed in the hope that it will be useful, *) (* but WITHOUT ANY WARRANTY; without even the implied warranty of *) (* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *) (* Lesser General Public License for more details. *) (* *) (* You should have received a copy of the GNU Lesser General Public *) (* License along with this library; if not, write to the Free Software *) (* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *) (* USA *) (**************************************************************************) 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 () ;;
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.