api-i18n.texi 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2009, 2010
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Internationalization
  7. @section Support for Internationalization
  8. @cindex internationalization
  9. @cindex i18n
  10. Guile provides internationalization@footnote{For concision and style,
  11. programmers often like to refer to internationalization as ``i18n''.}
  12. support for Scheme programs in two ways. First, procedures to
  13. manipulate text and data in a way that conforms to particular cultural
  14. conventions (i.e., in a ``locale-dependent'' way) are provided in the
  15. @code{(ice-9 i18n)}. Second, Guile allows the use of GNU
  16. @code{gettext} to translate program message strings.
  17. @menu
  18. * i18n Introduction:: Introduction to Guile's i18n support.
  19. * Text Collation:: Sorting strings and characters.
  20. * Character Case Mapping:: Case mapping.
  21. * Number Input and Output:: Parsing and printing numbers.
  22. * Accessing Locale Information:: Detailed locale information.
  23. * Gettext Support:: Translating message strings.
  24. @end menu
  25. @node i18n Introduction, Text Collation, Internationalization, Internationalization
  26. @subsection Internationalization with Guile
  27. In order to make use of the functions described thereafter, the
  28. @code{(ice-9 i18n)} module must be imported in the usual way:
  29. @example
  30. (use-modules (ice-9 i18n))
  31. @end example
  32. @cindex cultural conventions
  33. The @code{(ice-9 i18n)} module provides procedures to manipulate text
  34. and other data in a way that conforms to the cultural conventions
  35. chosen by the user. Each region of the world or language has its own
  36. customs to, for instance, represent real numbers, classify characters,
  37. collate text, etc. All these aspects comprise the so-called
  38. ``cultural conventions'' of that region or language.
  39. @cindex locale
  40. @cindex locale category
  41. Computer systems typically refer to a set of cultural conventions as a
  42. @dfn{locale}. For each particular aspect that comprise those cultural
  43. conventions, a @dfn{locale category} is defined. For instance, the
  44. way characters are classified is defined by the @code{LC_CTYPE}
  45. category, while the language in which program messages are issued to
  46. the user is defined by the @code{LC_MESSAGES} category
  47. (@pxref{Locales, General Locale Information} for details).
  48. @cindex locale object
  49. The procedures provided by this module allow the development of
  50. programs that adapt automatically to any locale setting. As we will
  51. see later, many of these procedures can optionally take a @dfn{locale
  52. object} argument. This additional argument defines the locale
  53. settings that must be followed by the invoked procedure. When it is
  54. omitted, then the current locale settings of the process are followed
  55. (@pxref{Locales, @code{setlocale}}).
  56. The following procedures allow the manipulation of such locale
  57. objects.
  58. @deffn {Scheme Procedure} make-locale category-list locale-name [base-locale]
  59. @deffnx {C Function} scm_make_locale (category_list, locale_name, base_locale)
  60. Return a reference to a data structure representing a set of locale
  61. datasets. @var{locale-name} should be a string denoting a particular
  62. locale (e.g., @code{"aa_DJ"}) and @var{category-list} should be either
  63. a list of locale categories or a single category as used with
  64. @code{setlocale} (@pxref{Locales, @code{setlocale}}). Optionally, if
  65. @code{base-locale} is passed, it should be a locale object denoting
  66. settings for categories not listed in @var{category-list}.
  67. The following invocation creates a locale object that combines the use
  68. of Swedish for messages and character classification with the
  69. default settings for the other categories (i.e., the settings of the
  70. default @code{C} locale which usually represents conventions in use in
  71. the USA):
  72. @example
  73. (make-locale (list LC_MESSAGE LC_CTYPE) "sv_SE")
  74. @end example
  75. The following example combines the use of Esperanto messages and
  76. conventions with monetary conventions from Croatia:
  77. @example
  78. (make-locale LC_MONETARY "hr_HR"
  79. (make-locale LC_ALL "eo_EO"))
  80. @end example
  81. A @code{system-error} exception (@pxref{Handling Errors}) is raised by
  82. @code{make-locale} when @var{locale-name} does not match any of the
  83. locales compiled on the system. Note that on non-GNU systems, this
  84. error may be raised later, when the locale object is actually used.
  85. @end deffn
  86. @deffn {Scheme Procedure} locale? obj
  87. @deffnx {C Function} scm_locale_p (obj)
  88. Return true if @var{obj} is a locale object.
  89. @end deffn
  90. @defvr {Scheme Variable} %global-locale
  91. @defvrx {C Variable} scm_global_locale
  92. This variable is bound to a locale object denoting the current process
  93. locale as installed using @code{setlocale ()} (@pxref{Locales}). It
  94. may be used like any other locale object, including as a third
  95. argument to @code{make-locale}, for instance.
  96. @end defvr
  97. @node Text Collation, Character Case Mapping, i18n Introduction, Internationalization
  98. @subsection Text Collation
  99. The following procedures provide support for text collation, i.e.,
  100. locale-dependent string and character sorting.
  101. @deffn {Scheme Procedure} string-locale<? s1 s2 [locale]
  102. @deffnx {C Function} scm_string_locale_lt (s1, s2, locale)
  103. @deffnx {Scheme Procedure} string-locale>? s1 s2 [locale]
  104. @deffnx {C Function} scm_string_locale_gt (s1, s2, locale)
  105. @deffnx {Scheme Procedure} string-locale-ci<? s1 s2 [locale]
  106. @deffnx {C Function} scm_string_locale_ci_lt (s1, s2, locale)
  107. @deffnx {Scheme Procedure} string-locale-ci>? s1 s2 [locale]
  108. @deffnx {C Function} scm_string_locale_ci_gt (s1, s2, locale)
  109. Compare strings @var{s1} and @var{s2} in a locale-dependent way. If
  110. @var{locale} is provided, it should be locale object (as returned by
  111. @code{make-locale}) and will be used to perform the comparison;
  112. otherwise, the current system locale is used. For the @code{-ci}
  113. variants, the comparison is made in a case-insensitive way.
  114. @end deffn
  115. @deffn {Scheme Procedure} string-locale-ci=? s1 s2 [locale]
  116. @deffnx {C Function} scm_string_locale_ci_eq (s1, s2, locale)
  117. Compare strings @var{s1} and @var{s2} in a case-insensitive, and
  118. locale-dependent way. If @var{locale} is provided, it should be
  119. a locale object (as returned by @code{make-locale}) and will be used to
  120. perform the comparison; otherwise, the current system locale is used.
  121. @end deffn
  122. @deffn {Scheme Procedure} char-locale<? c1 c2 [locale]
  123. @deffnx {C Function} scm_char_locale_lt (c1, c2, locale)
  124. @deffnx {Scheme Procedure} char-locale>? c1 c2 [locale]
  125. @deffnx {C Function} scm_char_locale_gt (c1, c2, locale)
  126. @deffnx {Scheme Procedure} char-locale-ci<? c1 c2 [locale]
  127. @deffnx {C Function} scm_char_locale_ci_lt (c1, c2, locale)
  128. @deffnx {Scheme Procedure} char-locale-ci>? c1 c2 [locale]
  129. @deffnx {C Function} scm_char_locale_ci_gt (c1, c2, locale)
  130. Compare characters @var{c1} and @var{c2} according to either
  131. @var{locale} (a locale object as returned by @code{make-locale}) or
  132. the current locale. For the @code{-ci} variants, the comparison is
  133. made in a case-insensitive way.
  134. @end deffn
  135. @deffn {Scheme Procedure} char-locale-ci=? c1 c2 [locale]
  136. @deffnx {C Function} scm_char_locale_ci_eq (c1, c2, locale)
  137. Return true if character @var{c1} is equal to @var{c2}, in a case
  138. insensitive way according to @var{locale} or to the current locale.
  139. @end deffn
  140. @node Character Case Mapping, Number Input and Output, Text Collation, Internationalization
  141. @subsection Character Case Mapping
  142. The procedures below provide support for ``character case mapping'',
  143. i.e., to convert characters or strings to their upper-case or
  144. lower-case equivalent. Note that SRFI-13 provides procedures that
  145. look similar (@pxref{Alphabetic Case Mapping}). However, the SRFI-13
  146. procedures are locale-independent. Therefore, they do not take into
  147. account specificities of the customs in use in a particular language
  148. or region of the world. For instance, while most languages using the
  149. Latin alphabet map lower-case letter ``i'' to upper-case letter ``I'',
  150. Turkish maps lower-case ``i'' to ``Latin capital letter I with dot
  151. above''. The following procedures allow programmers to provide
  152. idiomatic character mapping.
  153. @deffn {Scheme Procedure} char-locale-downcase chr [locale]
  154. @deffnx {C Function} scm_char_locale_upcase (chr, locale)
  155. Return the lowercase character that corresponds to @var{chr} according
  156. to either @var{locale} or the current locale.
  157. @end deffn
  158. @deffn {Scheme Procedure} char-locale-upcase chr [locale]
  159. @deffnx {C Function} scm_char_locale_downcase (chr, locale)
  160. Return the uppercase character that corresponds to @var{chr} according
  161. to either @var{locale} or the current locale.
  162. @end deffn
  163. @deffn {Scheme Procedure} char-locale-titlecase chr [locale]
  164. @deffnx {C Function} scm_char_locale_titlecase (chr, locale)
  165. Return the titlecase character that corresponds to @var{chr} according
  166. to either @var{locale} or the current locale.
  167. @end deffn
  168. @deffn {Scheme Procedure} string-locale-upcase str [locale]
  169. @deffnx {C Function} scm_string_locale_upcase (str, locale)
  170. Return a new string that is the uppercase version of @var{str}
  171. according to either @var{locale} or the current locale.
  172. @end deffn
  173. @deffn {Scheme Procedure} string-locale-downcase str [locale]
  174. @deffnx {C Function} scm_string_locale_downcase (str, locale)
  175. Return a new string that is the down-case version of @var{str}
  176. according to either @var{locale} or the current locale.
  177. @end deffn
  178. @deffn {Scheme Procedure} string-locale-titlecase str [locale]
  179. @deffnx {C Function} scm_string_locale_titlecase (str, locale)
  180. Return a new string that is the titlecase version of @var{str}
  181. according to either @var{locale} or the current locale.
  182. @end deffn
  183. @node Number Input and Output, Accessing Locale Information, Character Case Mapping, Internationalization
  184. @subsection Number Input and Output
  185. The following procedures allow programs to read and write numbers
  186. written according to a particular locale. As an example, in English,
  187. ``ten thousand and a half'' is usually written @code{10,000.5} while
  188. in French it is written @code{10 000,5}. These procedures allow such
  189. differences to be taken into account.
  190. @findex strtod
  191. @deffn {Scheme Procedure} locale-string->integer str [base [locale]]
  192. @deffnx {C Function} scm_locale_string_to_integer (str, base, locale)
  193. Convert string @var{str} into an integer according to either
  194. @var{locale} (a locale object as returned by @code{make-locale}) or
  195. the current process locale. If @var{base} is specified, then it
  196. determines the base of the integer being read (e.g., @code{16} for an
  197. hexadecimal number, @code{10} for a decimal number); by default,
  198. decimal numbers are read. Return two values (@pxref{Multiple
  199. Values}): an integer (on success) or @code{#f}, and the number of
  200. characters read from @var{str} (@code{0} on failure).
  201. This function is based on the C library's @code{strtol} function
  202. (@pxref{Parsing of Integers, @code{strtol},, libc, The GNU C Library
  203. Reference Manual}).
  204. @end deffn
  205. @findex strtod
  206. @deffn {Scheme Procedure} locale-string->inexact str [locale]
  207. @deffnx {C Function} scm_locale_string_to_inexact (str, locale)
  208. Convert string @var{str} into an inexact number according to either
  209. @var{locale} (a locale object as returned by @code{make-locale}) or
  210. the current process locale. Return two values (@pxref{Multiple
  211. Values}): an inexact number (on success) or @code{#f}, and the number
  212. of characters read from @var{str} (@code{0} on failure).
  213. This function is based on the C library's @code{strtod} function
  214. (@pxref{Parsing of Floats, @code{strtod},, libc, The GNU C Library
  215. Reference Manual}).
  216. @end deffn
  217. @deffn {Scheme Procedure} number->locale-string number [fraction-digits [locale]]
  218. Convert @var{number} (an inexact) into a string according to the
  219. cultural conventions of either @var{locale} (a locale object) or the
  220. current locale. Optionally, @var{fraction-digits} may be bound to an
  221. integer specifying the number of fractional digits to be displayed.
  222. @end deffn
  223. @deffn {Scheme Procedure} monetary-amount->locale-string amount intl? [locale]
  224. Convert @var{amount} (an inexact denoting a monetary amount) into a
  225. string according to the cultural conventions of either @var{locale} (a
  226. locale object) or the current locale. If @var{intl?} is true, then
  227. the international monetary format for the given locale is used
  228. (@pxref{Currency Symbol, international and locale monetary formats,,
  229. libc, The GNU C Library Reference Manual}).
  230. @end deffn
  231. @node Accessing Locale Information, Gettext Support, Number Input and Output, Internationalization
  232. @subsection Accessing Locale Information
  233. @findex nl_langinfo
  234. @cindex low-level locale information
  235. It is sometimes useful to obtain very specific information about a
  236. locale such as the word it uses for days or months, its format for
  237. representing floating-point figures, etc. The @code{(ice-9 i18n)}
  238. module provides support for this in a way that is similar to the libc
  239. functions @code{nl_langinfo ()} and @code{localeconv ()}
  240. (@pxref{Locale Information, accessing locale information from C,,
  241. libc, The GNU C Library Reference Manual}). The available functions
  242. are listed below.
  243. @deffn {Scheme Procedure} locale-encoding [locale]
  244. Return the name of the encoding (a string whose interpretation is
  245. system-dependent) of either @var{locale} or the current locale.
  246. @end deffn
  247. The following functions deal with dates and times.
  248. @deffn {Scheme Procedure} locale-day day [locale]
  249. @deffnx {Scheme Procedure} locale-day-short day [locale]
  250. @deffnx {Scheme Procedure} locale-month month [locale]
  251. @deffnx {Scheme Procedure} locale-month-short month [locale]
  252. Return the word (a string) used in either @var{locale} or the current
  253. locale to name the day (or month) denoted by @var{day} (or
  254. @var{month}), an integer between 1 and 7 (or 1 and 12). The
  255. @code{-short} variants provide an abbreviation instead of a full name.
  256. @end deffn
  257. @deffn {Scheme Procedure} locale-am-string [locale]
  258. @deffnx {Scheme Procedure} locale-pm-string [locale]
  259. Return a (potentially empty) string that is used to denote @i{ante
  260. meridiem} (or @i{post meridiem}) hours in 12-hour format.
  261. @end deffn
  262. @deffn {Scheme Procedure} locale-date+time-format [locale]
  263. @deffnx {Scheme Procedure} locale-date-format [locale]
  264. @deffnx {Scheme Procedure} locale-time-format [locale]
  265. @deffnx {Scheme Procedure} locale-time+am/pm-format [locale]
  266. @deffnx {Scheme Procedure} locale-era-date-format [locale]
  267. @deffnx {Scheme Procedure} locale-era-date+time-format [locale]
  268. @deffnx {Scheme Procedure} locale-era-time-format [locale]
  269. These procedures return format strings suitable to @code{strftime}
  270. (@pxref{Time}) that may be used to display (part of) a date/time
  271. according to certain constraints and to the conventions of either
  272. @var{locale} or the current locale (@pxref{The Elegant and Fast Way,
  273. the @code{nl_langinfo ()} items,, libc, The GNU C Library Reference
  274. Manual}).
  275. @end deffn
  276. @deffn {Scheme Procedure} locale-era [locale]
  277. @deffnx {Scheme Procedure} locale-era-year [locale]
  278. These functions return, respectively, the era and the year of the
  279. relevant era used in @var{locale} or the current locale. Most locales
  280. do not define this value. In this case, the empty string is returned.
  281. An example of a locale that does define this value is the Japanese
  282. one.
  283. @end deffn
  284. The following procedures give information about number representation.
  285. @deffn {Scheme Procedure} locale-decimal-point [locale]
  286. @deffnx {Scheme Procedure} locale-thousands-separator [locale]
  287. These functions return a string denoting the representation of the
  288. decimal point or that of the thousand separator (respectively) for
  289. either @var{locale} or the current locale.
  290. @end deffn
  291. @deffn {Scheme Procedure} locale-digit-grouping [locale]
  292. Return a (potentially circular) list of integers denoting how digits
  293. of the integer part of a number are to be grouped, starting at the
  294. decimal point and going to the left. The list contains integers
  295. indicating the size of the successive groups, from right to left. If
  296. the list is non-circular, then no grouping occurs for digits beyond
  297. the last group.
  298. For instance, if the returned list is a circular list that contains
  299. only @code{3} and the thousand separator is @code{","} (as is the case
  300. with English locales), then the number @code{12345678} should be
  301. printed @code{12,345,678}.
  302. @end deffn
  303. The following procedures deal with the representation of monetary
  304. amounts. Some of them take an additional @var{intl?} argument (a
  305. boolean) that tells whether the international or local monetary
  306. conventions for the given locale are to be used.
  307. @deffn {Scheme Procedure} locale-monetary-decimal-point [locale]
  308. @deffnx {Scheme Procedure} locale-monetary-thousands-separator [locale]
  309. @deffnx {Scheme Procedure} locale-monetary-grouping [locale]
  310. These are the monetary counterparts of the above procedures. These
  311. procedures apply to monetary amounts.
  312. @end deffn
  313. @deffn {Scheme Procedure} locale-currency-symbol intl? [locale]
  314. Return the currency symbol (a string) of either @var{locale} or the
  315. current locale.
  316. The following example illustrates the difference between the local and
  317. international monetary formats:
  318. @example
  319. (define us (make-locale LC_MONETARY "en_US"))
  320. (locale-currency-symbol #f us)
  321. @result{} "-$"
  322. (locale-currency-symbol #t us)
  323. @result{} "USD "
  324. @end example
  325. @end deffn
  326. @deffn {Scheme Procedure} locale-monetary-fractional-digits intl? [locale]
  327. Return the number of fractional digits to be used when printing
  328. monetary amounts according to either @var{locale} or the current
  329. locale. If the locale does not specify it, then @code{#f} is
  330. returned.
  331. @end deffn
  332. @deffn {Scheme Procedure} locale-currency-symbol-precedes-positive? intl? [locale]
  333. @deffnx {Scheme Procedure} locale-currency-symbol-precedes-negative? intl? [locale]
  334. @deffnx {Scheme Procedure} locale-positive-separated-by-space? intl? [locale]
  335. @deffnx {Scheme Procedure} locale-negative-separated-by-space? intl? [locale]
  336. These procedures return a boolean indicating whether the currency
  337. symbol should precede a positive/negative number, and whether a
  338. whitespace should be inserted between the currency symbol and a
  339. positive/negative amount.
  340. @end deffn
  341. @deffn {Scheme Procedure} locale-monetary-positive-sign [locale]
  342. @deffnx {Scheme Procedure} locale-monetary-negative-sign [locale]
  343. Return a string denoting the positive (respectively negative) sign
  344. that should be used when printing a monetary amount.
  345. @end deffn
  346. @deffn {Scheme Procedure} locale-positive-sign-position
  347. @deffnx {Scheme Procedure} locale-negative-sign-position
  348. These functions return a symbol telling where a sign of a
  349. positive/negative monetary amount is to appear when printing it. The
  350. possible values are:
  351. @table @code
  352. @item parenthesize
  353. The currency symbol and quantity should be surrounded by parentheses.
  354. @item sign-before
  355. Print the sign string before the quantity and currency symbol.
  356. @item sign-after
  357. Print the sign string after the quantity and currency symbol.
  358. @item sign-before-currency-symbol
  359. Print the sign string right before the currency symbol.
  360. @item sign-after-currency-symbol
  361. Print the sign string right after the currency symbol.
  362. @item unspecified
  363. Unspecified. We recommend you print the sign after the currency
  364. symbol.
  365. @end table
  366. @end deffn
  367. Finally, the two following procedures may be helpful when programming
  368. user interfaces:
  369. @deffn {Scheme Procedure} locale-yes-regexp [locale]
  370. @deffnx {Scheme Procedure} locale-no-regexp [locale]
  371. Return a string that can be used as a regular expression to recognize
  372. a positive (respectively, negative) response to a yes/no question.
  373. For the C locale, the default values are typically @code{"^[yY]"} and
  374. @code{"^[nN]"}, respectively.
  375. Here is an example:
  376. @example
  377. (use-modules (ice-9 rdelim))
  378. (format #t "Does Guile rock?~%")
  379. (let lp ((answer (read-line)))
  380. (cond ((string-match (locale-yes-regexp) answer)
  381. (format #t "High fives!~%"))
  382. ((string-match (locale-no-regexp) answer)
  383. (format #t "How about now? Does it rock yet?~%")
  384. (lp (read-line)))
  385. (else
  386. (format #t "What do you mean?~%")
  387. (lp (read-line)))))
  388. @end example
  389. For an internationalized yes/no string output, @code{gettext} should
  390. be used (@pxref{Gettext Support}).
  391. @end deffn
  392. Example uses of some of these functions are the implementation of the
  393. @code{number->locale-string} and @code{monetary-amount->locale-string}
  394. procedures (@pxref{Number Input and Output}), as well as that the
  395. SRFI-19 date and time conversion to/from strings (@pxref{SRFI-19}).
  396. @node Gettext Support, , Accessing Locale Information, Internationalization
  397. @subsection Gettext Support
  398. Guile provides an interface to GNU @code{gettext} for translating
  399. message strings (@pxref{Introduction,,, gettext, GNU @code{gettext}
  400. utilities}).
  401. Messages are collected in domains, so different libraries and programs
  402. maintain different message catalogues. The @var{domain} parameter in
  403. the functions below is a string (it becomes part of the message
  404. catalog filename).
  405. When @code{gettext} is not available, or if Guile was configured
  406. @samp{--without-nls}, dummy functions doing no translation are
  407. provided. When @code{gettext} support is available in Guile, the
  408. @code{i18n} feature is provided (@pxref{Feature Tracking}).
  409. @deffn {Scheme Procedure} gettext msg [domain [category]]
  410. @deffnx {C Function} scm_gettext (msg, domain, category)
  411. Return the translation of @var{msg} in @var{domain}. @var{domain} is
  412. optional and defaults to the domain set through @code{textdomain}
  413. below. @var{category} is optional and defaults to @code{LC_MESSAGES}
  414. (@pxref{Locales}).
  415. Normal usage is for @var{msg} to be a literal string.
  416. @command{xgettext} can extract those from the source to form a message
  417. catalogue ready for translators (@pxref{xgettext Invocation,, Invoking
  418. the @command{xgettext} Program, gettext, GNU @code{gettext}
  419. utilities}).
  420. @example
  421. (display (gettext "You are in a maze of twisty passages."))
  422. @end example
  423. @code{_} is a commonly used shorthand, an application can make that an
  424. alias for @code{gettext}. Or a library can make a definition that
  425. uses its specific @var{domain} (so an application can change the
  426. default without affecting the library).
  427. @example
  428. (define (_ msg) (gettext msg "mylibrary"))
  429. (display (_ "File not found."))
  430. @end example
  431. @code{_} is also a good place to perhaps strip disambiguating extra
  432. text from the message string, as for instance in @ref{GUI program
  433. problems,, How to use @code{gettext} in GUI programs, gettext, GNU
  434. @code{gettext} utilities}.
  435. @end deffn
  436. @deffn {Scheme Procedure} ngettext msg msgplural n [domain [category]]
  437. @deffnx {C Function} scm_ngettext (msg, msgplural, n, domain, category)
  438. Return the translation of @var{msg}/@var{msgplural} in @var{domain},
  439. with a plural form chosen appropriately for the number @var{n}.
  440. @var{domain} is optional and defaults to the domain set through
  441. @code{textdomain} below. @var{category} is optional and defaults to
  442. @code{LC_MESSAGES} (@pxref{Locales}).
  443. @var{msg} is the singular form, and @var{msgplural} the plural. When
  444. no translation is available, @var{msg} is used if @math{@var{n} = 1},
  445. or @var{msgplural} otherwise. When translated, the message catalogue
  446. can have a different rule, and can have more than two possible forms.
  447. As per @code{gettext} above, normal usage is for @var{msg} and
  448. @var{msgplural} to be literal strings, since @command{xgettext} can
  449. extract them from the source to build a message catalogue. For
  450. example,
  451. @example
  452. (define (done n)
  453. (format #t (ngettext "~a file processed\n"
  454. "~a files processed\n" n)
  455. n))
  456. (done 1) @print{} 1 file processed
  457. (done 3) @print{} 3 files processed
  458. @end example
  459. It's important to use @code{ngettext} rather than plain @code{gettext}
  460. for plurals, since the rules for singular and plural forms in English
  461. are not the same in other languages. Only @code{ngettext} will allow
  462. translators to give correct forms (@pxref{Plural forms,, Additional
  463. functions for plural forms, gettext, GNU @code{gettext} utilities}).
  464. @end deffn
  465. @deffn {Scheme Procedure} textdomain [domain]
  466. @deffnx {C Function} scm_textdomain (domain)
  467. Get or set the default gettext domain. When called with no parameter
  468. the current domain is returned. When called with a parameter,
  469. @var{domain} is set as the current domain, and that new value
  470. returned. For example,
  471. @example
  472. (textdomain "myprog")
  473. @result{} "myprog"
  474. @end example
  475. @end deffn
  476. @deffn {Scheme Procedure} bindtextdomain domain [directory]
  477. @deffnx {C Function} scm_bindtextdomain (domain, directory)
  478. Get or set the directory under which to find message files for
  479. @var{domain}. When called without a @var{directory} the current
  480. setting is returned. When called with a @var{directory},
  481. @var{directory} is set for @var{domain} and that new setting returned.
  482. For example,
  483. @example
  484. (bindtextdomain "myprog" "/my/tree/share/locale")
  485. @result{} "/my/tree/share/locale"
  486. @end example
  487. When using Autoconf/Automake, an application should arrange for the
  488. configured @code{localedir} to get into the program (by substituting,
  489. or by generating a config file) and set that for its domain. This
  490. ensures the catalogue can be found even when installed in a
  491. non-standard location.
  492. @end deffn
  493. @deffn {Scheme Procedure} bind-textdomain-codeset domain [encoding]
  494. @deffnx {C Function} scm_bind_textdomain_codeset (domain, encoding)
  495. Get or set the text encoding to be used by @code{gettext} for messages
  496. from @var{domain}. @var{encoding} is a string, the name of a coding
  497. system, for instance @nicode{"8859_1"}. (On a Unix/POSIX system the
  498. @command{iconv} program can list all available encodings.)
  499. When called without an @var{encoding} the current setting is returned,
  500. or @code{#f} if none yet set. When called with an @var{encoding}, it
  501. is set for @var{domain} and that new setting returned. For example,
  502. @example
  503. (bind-textdomain-codeset "myprog")
  504. @result{} #f
  505. (bind-textdomain-codeset "myprog" "latin-9")
  506. @result{} "latin-9"
  507. @end example
  508. The encoding requested can be different from the translated data file,
  509. messages will be recoded as necessary. But note that when there is no
  510. translation, @code{gettext} returns its @var{msg} unchanged, ie.@:
  511. without any recoding. For that reason source message strings are best
  512. as plain ASCII.
  513. Currently Guile has no understanding of multi-byte characters, and
  514. string functions won't recognise character boundaries in multi-byte
  515. strings. An application will at least be able to pass such strings
  516. through to some output though. Perhaps this will change in the
  517. future.
  518. @end deffn
  519. @c Local Variables:
  520. @c TeX-master: "guile.texi"
  521. @c ispell-local-dictionary: "american"
  522. @c End: