Module type Bogue.I18n.ContextInit

module type ContextInit = sig .. end

Functions available in every context.


val gettext : string -> string

gettext text tries to return the translation of the text string into the user's preferred language, in the current context. This will load the corresponding "locales" files, when available. If the translation is not found, other contexts will be examined. If everything fails, the original text string is returned.

val gettext_opt : string -> string option

Similar to gettext but returns None if the translation cannot be found.

val tt : string -> string Stdlib.Lazy.t

Lazy version of gettext. This is preferred over gettext if the string is used several times, because the translation is cached.

tt means "translate text".

val tf : 'a Stdlib.Lazy.t -> 'a

Shorthand for Lazy.force. Example:

let translated = tt "Hello" in
for _ = 1 to 10 do print_endline (tf translated)

tf means "translation force"

Translating printf formats

Sometimes the translation of a format string like "%u dollars off coupon" imposes to change the location of the special flags, as in "bon de réduction de %u dollars".

Therefore it's easier to translate the whole format string instead of working word by word. The I18n module provides facilities for this.

val t_uint : string -> (int -> string) Stdlib.Lazy.t

For instance:

let f = t_uint "%u dollars off coupon" in
print_endline ((tf f) 150)

will print, for French locale:

"bon de réduction de 150 dollars"

as soon as the French translation of "%u dollars off coupon" is declared to be "bon de réduction de %u dollars". See Declaring new translations.

val t_int : string -> (int -> string) Stdlib.Lazy.t

Same as t_uint but for usual (signed) integers (flag %i).

val t_str : string -> (string -> string) Stdlib.Lazy.t

Same as t_uint but for strings (flag %s).

val t_uint2 : string -> (int -> int -> string) Stdlib.Lazy.t

Similar to t_uint but the format string should contain two %s flags.

Declaring new translations

Translations can be added either programmatically using add_translation or by directly editing the configurations files.

val add_translation : I18n.locale -> string -> string -> unit

add_translation locale text translation will declare the string translation to be the translation of the string text within the current context and for the given locale. This will overwrite previously defined translations for text.

This function does not modifies the translation files.