api-i18n.texi 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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,
  4. @c 2009, 2010, 2017 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_MESSAGES 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. By default, print as many fractional digits as
  221. necessary, up to an upper bound. Optionally, @var{fraction-digits} may
  222. be bound to an integer specifying the number of fractional digits to be
  223. displayed.
  224. @end deffn
  225. @deffn {Scheme Procedure} monetary-amount->locale-string amount intl? [locale]
  226. Convert @var{amount} (an inexact denoting a monetary amount) into a
  227. string according to the cultural conventions of either @var{locale} (a
  228. locale object) or the current locale. If @var{intl?} is true, then
  229. the international monetary format for the given locale is used
  230. (@pxref{Currency Symbol, international and locale monetary formats,,
  231. libc, The GNU C Library Reference Manual}).
  232. @end deffn
  233. @node Accessing Locale Information, Gettext Support, Number Input and Output, Internationalization
  234. @subsection Accessing Locale Information
  235. @findex nl_langinfo
  236. @cindex low-level locale information
  237. It is sometimes useful to obtain very specific information about a
  238. locale such as the word it uses for days or months, its format for
  239. representing floating-point figures, etc. The @code{(ice-9 i18n)}
  240. module provides support for this in a way that is similar to the libc
  241. functions @code{nl_langinfo ()} and @code{localeconv ()}
  242. (@pxref{Locale Information, accessing locale information from C,,
  243. libc, The GNU C Library Reference Manual}). The available functions
  244. are listed below.
  245. @deffn {Scheme Procedure} locale-encoding [locale]
  246. Return the name of the encoding (a string whose interpretation is
  247. system-dependent) of either @var{locale} or the current locale.
  248. @end deffn
  249. The following functions deal with dates and times.
  250. @deffn {Scheme Procedure} locale-day day [locale]
  251. @deffnx {Scheme Procedure} locale-day-short day [locale]
  252. @deffnx {Scheme Procedure} locale-month month [locale]
  253. @deffnx {Scheme Procedure} locale-month-short month [locale]
  254. Return the word (a string) used in either @var{locale} or the current
  255. locale to name the day (or month) denoted by @var{day} (or
  256. @var{month}), an integer between 1 and 7 (or 1 and 12). The
  257. @code{-short} variants provide an abbreviation instead of a full name.
  258. @end deffn
  259. @deffn {Scheme Procedure} locale-am-string [locale]
  260. @deffnx {Scheme Procedure} locale-pm-string [locale]
  261. Return a (potentially empty) string that is used to denote @i{ante
  262. meridiem} (or @i{post meridiem}) hours in 12-hour format.
  263. @end deffn
  264. @deffn {Scheme Procedure} locale-date+time-format [locale]
  265. @deffnx {Scheme Procedure} locale-date-format [locale]
  266. @deffnx {Scheme Procedure} locale-time-format [locale]
  267. @deffnx {Scheme Procedure} locale-time+am/pm-format [locale]
  268. @deffnx {Scheme Procedure} locale-era-date-format [locale]
  269. @deffnx {Scheme Procedure} locale-era-date+time-format [locale]
  270. @deffnx {Scheme Procedure} locale-era-time-format [locale]
  271. These procedures return format strings suitable to @code{strftime}
  272. (@pxref{Time}) that may be used to display (part of) a date/time
  273. according to certain constraints and to the conventions of either
  274. @var{locale} or the current locale (@pxref{The Elegant and Fast Way,
  275. the @code{nl_langinfo ()} items,, libc, The GNU C Library Reference
  276. Manual}).
  277. @end deffn
  278. @deffn {Scheme Procedure} locale-era [locale]
  279. @deffnx {Scheme Procedure} locale-era-year [locale]
  280. These functions return, respectively, the era and the year of the
  281. relevant era used in @var{locale} or the current locale. Most locales
  282. do not define this value. In this case, the empty string is returned.
  283. An example of a locale that does define this value is the Japanese
  284. one.
  285. @end deffn
  286. The following procedures give information about number representation.
  287. @deffn {Scheme Procedure} locale-decimal-point [locale]
  288. @deffnx {Scheme Procedure} locale-thousands-separator [locale]
  289. These functions return a string denoting the representation of the
  290. decimal point or that of the thousand separator (respectively) for
  291. either @var{locale} or the current locale.
  292. @end deffn
  293. @deffn {Scheme Procedure} locale-digit-grouping [locale]
  294. Return a (potentially circular) list of integers denoting how digits
  295. of the integer part of a number are to be grouped, starting at the
  296. decimal point and going to the left. The list contains integers
  297. indicating the size of the successive groups, from right to left. If
  298. the list is non-circular, then no grouping occurs for digits beyond
  299. the last group.
  300. For instance, if the returned list is a circular list that contains
  301. only @code{3} and the thousand separator is @code{","} (as is the case
  302. with English locales), then the number @code{12345678} should be
  303. printed @code{12,345,678}.
  304. @end deffn
  305. The following procedures deal with the representation of monetary
  306. amounts. Some of them take an additional @var{intl?} argument (a
  307. boolean) that tells whether the international or local monetary
  308. conventions for the given locale are to be used.
  309. @deffn {Scheme Procedure} locale-monetary-decimal-point [locale]
  310. @deffnx {Scheme Procedure} locale-monetary-thousands-separator [locale]
  311. @deffnx {Scheme Procedure} locale-monetary-grouping [locale]
  312. These are the monetary counterparts of the above procedures. These
  313. procedures apply to monetary amounts.
  314. @end deffn
  315. @deffn {Scheme Procedure} locale-currency-symbol intl? [locale]
  316. Return the currency symbol (a string) of either @var{locale} or the
  317. current locale.
  318. The following example illustrates the difference between the local and
  319. international monetary formats:
  320. @example
  321. (define us (make-locale LC_MONETARY "en_US"))
  322. (locale-currency-symbol #f us)
  323. @result{} "-$"
  324. (locale-currency-symbol #t us)
  325. @result{} "USD "
  326. @end example
  327. @end deffn
  328. @deffn {Scheme Procedure} locale-monetary-fractional-digits intl? [locale]
  329. Return the number of fractional digits to be used when printing
  330. monetary amounts according to either @var{locale} or the current
  331. locale. If the locale does not specify it, then @code{#f} is
  332. returned.
  333. @end deffn
  334. @deffn {Scheme Procedure} locale-currency-symbol-precedes-positive? intl? [locale]
  335. @deffnx {Scheme Procedure} locale-currency-symbol-precedes-negative? intl? [locale]
  336. @deffnx {Scheme Procedure} locale-positive-separated-by-space? intl? [locale]
  337. @deffnx {Scheme Procedure} locale-negative-separated-by-space? intl? [locale]
  338. These procedures return a boolean indicating whether the currency
  339. symbol should precede a positive/negative number, and whether a
  340. whitespace should be inserted between the currency symbol and a
  341. positive/negative amount.
  342. @end deffn
  343. @deffn {Scheme Procedure} locale-monetary-positive-sign [locale]
  344. @deffnx {Scheme Procedure} locale-monetary-negative-sign [locale]
  345. Return a string denoting the positive (respectively negative) sign
  346. that should be used when printing a monetary amount.
  347. @end deffn
  348. @deffn {Scheme Procedure} locale-positive-sign-position
  349. @deffnx {Scheme Procedure} locale-negative-sign-position
  350. These functions return a symbol telling where a sign of a
  351. positive/negative monetary amount is to appear when printing it. The
  352. possible values are:
  353. @table @code
  354. @item parenthesize
  355. The currency symbol and quantity should be surrounded by parentheses.
  356. @item sign-before
  357. Print the sign string before the quantity and currency symbol.
  358. @item sign-after
  359. Print the sign string after the quantity and currency symbol.
  360. @item sign-before-currency-symbol
  361. Print the sign string right before the currency symbol.
  362. @item sign-after-currency-symbol
  363. Print the sign string right after the currency symbol.
  364. @item unspecified
  365. Unspecified. We recommend you print the sign after the currency
  366. symbol.
  367. @end table
  368. @end deffn
  369. Finally, the two following procedures may be helpful when programming
  370. user interfaces:
  371. @deffn {Scheme Procedure} locale-yes-regexp [locale]
  372. @deffnx {Scheme Procedure} locale-no-regexp [locale]
  373. Return a string that can be used as a regular expression to recognize
  374. a positive (respectively, negative) response to a yes/no question.
  375. For the C locale, the default values are typically @code{"^[yY]"} and
  376. @code{"^[nN]"}, respectively.
  377. Here is an example:
  378. @example
  379. (use-modules (ice-9 rdelim))
  380. (format #t "Does Guile rock?~%")
  381. (let lp ((answer (read-line)))
  382. (cond ((string-match (locale-yes-regexp) answer)
  383. (format #t "High fives!~%"))
  384. ((string-match (locale-no-regexp) answer)
  385. (format #t "How about now? Does it rock yet?~%")
  386. (lp (read-line)))
  387. (else
  388. (format #t "What do you mean?~%")
  389. (lp (read-line)))))
  390. @end example
  391. For an internationalized yes/no string output, @code{gettext} should
  392. be used (@pxref{Gettext Support}).
  393. @end deffn
  394. Example uses of some of these functions are the implementation of the
  395. @code{number->locale-string} and @code{monetary-amount->locale-string}
  396. procedures (@pxref{Number Input and Output}), as well as that the
  397. SRFI-19 date and time conversion to/from strings (@pxref{SRFI-19}).
  398. @node Gettext Support, , Accessing Locale Information, Internationalization
  399. @subsection Gettext Support
  400. Guile provides an interface to GNU @code{gettext} for translating
  401. message strings (@pxref{Introduction,,, gettext, GNU @code{gettext}
  402. utilities}).
  403. Messages are collected in domains, so different libraries and programs
  404. maintain different message catalogues. The @var{domain} parameter in
  405. the functions below is a string (it becomes part of the message
  406. catalog filename).
  407. When @code{gettext} is not available, or if Guile was configured
  408. @samp{--without-nls}, dummy functions doing no translation are
  409. provided. When @code{gettext} support is available in Guile, the
  410. @code{i18n} feature is provided (@pxref{Feature Tracking}).
  411. @deffn {Scheme Procedure} gettext msg [domain [category]]
  412. @deffnx {C Function} scm_gettext (msg, domain, category)
  413. Return the translation of @var{msg} in @var{domain}. @var{domain} is
  414. optional and defaults to the domain set through @code{textdomain}
  415. below. @var{category} is optional and defaults to @code{LC_MESSAGES}
  416. (@pxref{Locales}).
  417. Normal usage is for @var{msg} to be a literal string.
  418. @command{xgettext} can extract those from the source to form a message
  419. catalogue ready for translators (@pxref{xgettext Invocation,, Invoking
  420. the @command{xgettext} Program, gettext, GNU @code{gettext}
  421. utilities}).
  422. @example
  423. (display (gettext "You are in a maze of twisty passages."))
  424. @end example
  425. It is conventional to use @code{G_} as a shorthand for
  426. @code{gettext}.@footnote{Users of @code{gettext} might be a bit
  427. surprised that @code{G_} is the conventional abbreviation for
  428. @code{gettext}. In most other languages, the conventional shorthand is
  429. @code{_}. Guile uses @code{G_} because @code{_} is already taken, as it
  430. is bound to a syntactic keyword used by @code{syntax-rules},
  431. @code{match}, and other macros.} Libraries can define @code{G_} in such
  432. a way to look up translations using its specific @var{domain}, allowing
  433. different parts of a program to have different translation sources.
  434. @example
  435. (define (G_ msg) (gettext msg "mylibrary"))
  436. (display (G_ "File not found."))
  437. @end example
  438. @code{G_} is also a good place to perhaps strip disambiguating extra
  439. text from the message string, as for instance in @ref{GUI program
  440. problems,, How to use @code{gettext} in GUI programs, gettext, GNU
  441. @code{gettext} utilities}.
  442. @end deffn
  443. @deffn {Scheme Procedure} ngettext msg msgplural n [domain [category]]
  444. @deffnx {C Function} scm_ngettext (msg, msgplural, n, domain, category)
  445. Return the translation of @var{msg}/@var{msgplural} in @var{domain},
  446. with a plural form chosen appropriately for the number @var{n}.
  447. @var{domain} is optional and defaults to the domain set through
  448. @code{textdomain} below. @var{category} is optional and defaults to
  449. @code{LC_MESSAGES} (@pxref{Locales}).
  450. @var{msg} is the singular form, and @var{msgplural} the plural. When
  451. no translation is available, @var{msg} is used if @math{@var{n} = 1},
  452. or @var{msgplural} otherwise. When translated, the message catalogue
  453. can have a different rule, and can have more than two possible forms.
  454. As per @code{gettext} above, normal usage is for @var{msg} and
  455. @var{msgplural} to be literal strings, since @command{xgettext} can
  456. extract them from the source to build a message catalogue. For
  457. example,
  458. @example
  459. (define (done n)
  460. (format #t (ngettext "~a file processed\n"
  461. "~a files processed\n" n)
  462. n))
  463. (done 1) @print{} 1 file processed
  464. (done 3) @print{} 3 files processed
  465. @end example
  466. It's important to use @code{ngettext} rather than plain @code{gettext}
  467. for plurals, since the rules for singular and plural forms in English
  468. are not the same in other languages. Only @code{ngettext} will allow
  469. translators to give correct forms (@pxref{Plural forms,, Additional
  470. functions for plural forms, gettext, GNU @code{gettext} utilities}).
  471. @end deffn
  472. @deffn {Scheme Procedure} textdomain [domain]
  473. @deffnx {C Function} scm_textdomain (domain)
  474. Get or set the default gettext domain. When called with no parameter
  475. the current domain is returned. When called with a parameter,
  476. @var{domain} is set as the current domain, and that new value
  477. returned. For example,
  478. @example
  479. (textdomain "myprog")
  480. @result{} "myprog"
  481. @end example
  482. @end deffn
  483. @deffn {Scheme Procedure} bindtextdomain domain [directory]
  484. @deffnx {C Function} scm_bindtextdomain (domain, directory)
  485. Get or set the directory under which to find message files for
  486. @var{domain}. When called without a @var{directory} the current
  487. setting is returned. When called with a @var{directory},
  488. @var{directory} is set for @var{domain} and that new setting returned.
  489. For example,
  490. @example
  491. (bindtextdomain "myprog" "/my/tree/share/locale")
  492. @result{} "/my/tree/share/locale"
  493. @end example
  494. When using Autoconf/Automake, an application should arrange for the
  495. configured @code{localedir} to get into the program (by substituting,
  496. or by generating a config file) and set that for its domain. This
  497. ensures the catalogue can be found even when installed in a
  498. non-standard location.
  499. @end deffn
  500. @deffn {Scheme Procedure} bind-textdomain-codeset domain [encoding]
  501. @deffnx {C Function} scm_bind_textdomain_codeset (domain, encoding)
  502. Get or set the text encoding to be used by @code{gettext} for messages
  503. from @var{domain}. @var{encoding} is a string, the name of a coding
  504. system, for instance @nicode{"8859_1"}. (On a Unix/POSIX system the
  505. @command{iconv} program can list all available encodings.)
  506. When called without an @var{encoding} the current setting is returned,
  507. or @code{#f} if none yet set. When called with an @var{encoding}, it
  508. is set for @var{domain} and that new setting returned. For example,
  509. @example
  510. (bind-textdomain-codeset "myprog")
  511. @result{} #f
  512. (bind-textdomain-codeset "myprog" "latin-9")
  513. @result{} "latin-9"
  514. @end example
  515. The encoding requested can be different from the translated data file,
  516. messages will be recoded as necessary. But note that when there is no
  517. translation, @code{gettext} returns its @var{msg} unchanged, ie.@:
  518. without any recoding. For that reason source message strings are best
  519. as plain ASCII.
  520. Currently Guile has no understanding of multi-byte characters, and
  521. string functions won't recognise character boundaries in multi-byte
  522. strings. An application will at least be able to pass such strings
  523. through to some output though. Perhaps this will change in the
  524. future.
  525. @end deffn
  526. @c Local Variables:
  527. @c TeX-master: "guile.texi"
  528. @c ispell-local-dictionary: "american"
  529. @c End: