4.4 Translation of Program Messages

(require 'i18n) or (require 'gettext)

This module provides support for translating English messages into local languages using the (GNU) gettext and ngettext functions. The xgettext program from the GNU gettext package extracts the messages passed to gettext and ngettext from the Scheme source code, and compiles into a format where dual language speakers at the Translation Project provide a translation for each (English) string. After the translations are written, the msgfmt program produces a binary file which is read by this module. You will need to use those programs if you wish to use this Slib module.

This capability is intended for the benefit of a Scheme program’s users, not its developers. Warnings and error messages generated by Slib modules are not translated.

At present this module supports only the localization of textual messages. Localization of number, time, and date formats by Slib modules may be supported in the future.

The functions setlocale, textdomain, bindtextdomain, gettext, and ngettext are native to Guile. The following initialization example works in SCM using this Slib implementation, and in Guile (with Slib) using Guile’s native functions:

(require 'i18n)
(setlocale LC_MESSAGES "")
(textdomain "myprogram")
(cond ((and (provided? 'getenv) (getenv "TEXTDOMAINDIR")) =>
       (lambda (tdd) (bindtextdomain (textdomain) tdd))))
Constant: LC_CTYPE
Constant: LC_NUMERIC
Constant: LC_TIME
Constant: LC_COLLATE
Constant: LC_MONETARY
Constant: LC_MESSAGES
Constant: LC_ALL

Constants suitable for passing as the first argument to setlocale. Only LC_MESSAGES and LC_ALL are currently active.

Function: setlocale category [locale]

Get or set the current locale. The category argument should be the value of LC_MESSAGES. Locales are strings, such as ‘sv_SE’. Passing the empty string for locale retrieves a value based on the environment variables LANGUAGE, LC_ALL, LC_MESSAGES, and LANG.

Function: textdomain [domain]

Get or set the default gettext domain. When called with no parameter the current domain is returned. When called with a domain string, it is set as the current domain and returned.

The convention seems to be that domain is the lower-case name of the program having the messages.

Function: bindtextdomain domain [directory]

Get or set the directory tree in which to find message files for domain. When called without a directory, the current setting is returned. When called with a directory, directory is set for domain and directory is returned.

The GNU default is /usr/local/share/locale/.

Function: gettext msg [domain]

Return the translation of msg in domain. domain is optional and defaults to the domain set through textdomain.

Normal usage is for msg to be a literal string. The xgettext program extracts those from the source to form a message catalogue ready for translators.

Function: ngettext msg msgplural n [domain]

Return the translation of msg or msgplural in domain, with a plural form chosen appropriately for the number N. domain is optional and defaults to the domain set through textdomain.

msg is the singular form, and msgplural the plural. When no translation is available, msg is used if N = 1, or msgplural otherwise. When translated, the message catalogue can have a different rule, and can have more than two possible forms.