Library

Library should use the module Gettext.Library. It doesn't need any real implementation of ocaml-gettext. By this way, you can let the library user choose the most appropriate ocaml-gettext implementation. This point is essential : a library could be used as well in a GUI program or in short run command line program. These two examples don't require the same kind of implementation at all: GUI program loads most of their translated strings, command line program only use one among them. So by using the module Gettext.Library framework, you don't restrict programs to use one particular implementation of ocaml-gettext.

The library should define, using the functor Init provided:

After having instantiated the module Gettext.Library with the appropriate Init, you should use the function provided :

Warning

You must keep the function name s_, f_, sn_ and fn_. The extraction of translatable strings matches these names. If you don't keep it, the extraction of translatable strings will fail.

Example 3.5. library.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 LibraryGettext.Gettext;;

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

(* Example function *)
let library_only_function () = 
  
  (* Two simple examples : singular translation *)
  print_endline (s_ "Hello world !");
  Printf.printf (f_ "Hello %s !\n") "world";
  
  (* More complicated : plural translation, using strings *)
  print_endline (
     (sn_ "There is " "There are " 2)
    ^(string_of_int 2)
    ^(sn_ "plate." "plates." 2)
  );
  
  (* More simple forms of plural translation, using printf *)
  Printf.printf (fn_ "There is %d plate.\n" "There are %d plates.\n" 2) 2
;;

(* Another example function : used by program.ml *)
let hello_you name =
  Printf.printf (f_ "Hello %s\n") name
;;

Example 3.6. libraryGettext.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                                                                   *)
(**************************************************************************)

(* Create the module Gettext, using the textdomain "mydomain" *)
module Gettext = Gettext.Library(struct
  let textdomain   = "mydomain"
  let codeset      = None
  let dir          = None
  let dependencies = Gettext.init
end)
;;

All the calls to translation functions, use the textdomain provided in Init.

The only constraint when using ocaml-gettext in your library is to provide an access to the value Gettext.Library.init. This value is used as dependencies for other libraries and programs that depend on it. For example, since you use the library ocaml-gettext, your primary dependency is the function init provided in the top level (the function Gettext.string_of_exception is localised).

Warning

If you distribute your library, don't forget to mention that ocaml-gettext will only be able to translate the string defined in your library, if and only if the MO file build with is also installed. If not installed ocaml-gettext is useless.



[6] Strings which should be used by Printf function are checked to be sure that the returned strings are equivalent to the provided English string. In particular, every "%"-symbol should be the same in the provided string and the returned string. If not, it is the untranslated string which is returned.