package gettext-stub
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=08dd9df55b2af1838e2312be4be942b4375dbc18c2aed0ca1924488750e34f5d
sha512=4a09eab6d6f0d6ec435ca3d70305e2f97cbcc04bea72f85efcf649a0ead2faa322a0b054eb953b719f6dea98fb08de32fc80b4cf967681465c5a51e335aaf8d4
doc/README.html
OCaml-gettext - Internationalization library for OCaml (i18n)
Internationalization of a program means that the program have the possibility to handle different language. It can output messages which depend on the language of the user. Typically, if a program can output "bonjour" for a french user, and "hello" for an english user, this program is internationalized.
GNU gettext is one of the standard solutions for i18n. You just need to use special functions to translate strings. These functions are used when the program is running to do the translation and when compiling the program to extract the strings automatically. In ocaml-gettext these functions are "s_", "f_","sn_" and "fn_". They are both used to translate at runtime and to extract strings for translation.
ocaml-gettext provides enough service to build a internationalized program. It comes with :
- a pure Ocaml implementation, based on Camomile,
- an alternative implementation with a binding to GNU gettext library,
ocaml-gettexta tool to extract strings from Ocaml source.
Installation
The recommended way to install ocaml-gettext is via the [opam package manager][opam]:
$ opam install gettext gettext-camomile gettext-stubBuilding without Camomile
This project uses standard dune mechanism for building. It provides 3 different packages:
gettext: the base modulegettext-camomile; gettext using Camomile.gettext-stub: gettext using the C library.
If you want to not use Camomile, you can just compile or test the other module, using dune:
$ dune test --only-packages=gettext,gettext-stubDocumentation
- API documentation is available online.
- Reference manual
Examples
In all the examples, we are using shorthands function s_, sn_, f_ and fn_. It is important to keep this precise names because they are used to extract strings for translation.
This library demonstrates how to define i18n module for a library:
- Define a module LibraryGettext that defines the
textdomainanddependenciesof the module. In this casemydomainfor thetextdomainand the only dependencies areGettext.init. In the main library module Library:
- Use
open LibraryGettext.Gettextto give access to all the shorthands. - Make an
initpublic, to allow user of the library to initialize i18n for this library. Thisinitwill be used as adependenciesfor other module.
- Use
The GUI example is very similar to the library. The example is library.
This program demonstrates how to define i18n module and a concrete implementation for gettext:
Define a module ProgramGettext that defines:
- a
textdomainin this casemydomain - a list of
dependenciesin this caseExamplesLibrary.Library.initandExamplesGUI.Gui.init. - a real implementation in this case
GettextCamomile.Map.
- a
In the main module Program:
- Use
open ProgramGettext.Gettextto give access to all the shorthands. - Call
ProgramGettext.Gettext.init, useArg.parsewithgettext_argsreturned by this function.
- Use
The last pieces needed are the string files. They are traditionally stored in a po directory with a specific Makefile:
- The Makefile contains the most common targets.
- The POTFILES defines which files to look for translatable strings.
- The LINGUAS defines what language file (
.po) are present. - Running
make allwill generate a.potfile and compile the various language files.
To test the examples:
# Update the PO/POT files.
$> make update-po
# Define location of the .mo files.
$> export OCAML_LOCALEPATH=./_build/share/locale
# Run the default program.
$> dune exec --workspace dune-workspace.dev examples/program/program.exe -- --my-name Chet
Hello world!
Hello world!
There is 1 plate.
There are 2 plates.
There is 1 plate.
There are 2 plates.
Hello Chet
# Run the program for french language.
$> LANGUAGE=fr dune exec --workspace dune-workspace.dev examples/program/program.exe -- --my-name Chet
Bonjour le monde !
Bonjour world !
Il y a 1 assiette.
Il y a 2 assiettes.
Il y a 1 assiette.
Il y a 2 assiettes.
Bonjour Chet