api-i18n.texi 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @page
  7. @node Internationalization
  8. @section Support for Internationalization
  9. Guile provides an interface to GNU @code{gettext} for translating
  10. message strings (@pxref{Introduction,,, gettext, GNU @code{gettext}
  11. utilities}).
  12. Messages are collected in domains, so different libraries and programs
  13. maintain different message catalogues. The @var{domain} parameter in
  14. the functions below is a string (it becomes part of the message
  15. catalog filename).
  16. When @code{gettext} is not available, or if Guile was configured
  17. @samp{--without-nls}, dummy functions doing no translation are
  18. provided.
  19. @deffn {Scheme Procedure} gettext msg [domain [category]]
  20. @deffnx {C Function} scm_gettext (msg, domain, category)
  21. Return the translation of @var{msg} in @var{domain}. @var{domain} is
  22. optional and defaults to the domain set through @code{textdomain}
  23. below. @var{category} is optional and defaults to @code{LC_MESSAGES}
  24. (@pxref{Locales}).
  25. Normal usage is for @var{msg} to be a literal string.
  26. @command{xgettext} can extract those from the source to form a message
  27. catalogue ready for translators (@pxref{xgettext Invocation,, Invoking
  28. the @command{xgettext} Program, gettext, GNU @code{gettext}
  29. utilities}).
  30. @example
  31. (display (gettext "You are in a maze of twisty passages."))
  32. @end example
  33. @code{_} is a commonly used shorthand, an application can make that an
  34. alias for @code{gettext}. Or a library can make a definition that
  35. uses its specific @var{domain} (so an application can change the
  36. default without affecting the library).
  37. @example
  38. (define (_ msg) (gettext msg "mylibrary"))
  39. (display (_ "File not found."))
  40. @end example
  41. @code{_} is also a good place to perhaps strip disambiguating extra
  42. text from the message string, as for instance in @ref{GUI program
  43. problems,, How to use @code{gettext} in GUI programs, gettext, GNU
  44. @code{gettext} utilities}.
  45. @end deffn
  46. @deffn {Scheme Procedure} ngettext msg msgplural n [domain [category]]
  47. @deffnx {C Function} scm_ngettext (msg, msgplural, n, domain, category)
  48. Return the translation of @var{msg}/@var{msgplural} in @var{domain},
  49. with a plural form chosen appropriately for the number @var{n}.
  50. @var{domain} is optional and defaults to the domain set through
  51. @code{textdomain} below. @var{category} is optional and defaults to
  52. @code{LC_MESSAGES} (@pxref{Locales}).
  53. @var{msg} is the singular form, and @var{msgplural} the plural. When
  54. no translation is available, @var{msg} is used if @math{@var{n} = 1},
  55. or @var{msgplural} otherwise. When translated, the message catalogue
  56. can have a different rule, and can have more than two possible forms.
  57. As per @code{gettext} above, normal usage is for @var{msg} and
  58. @var{msgplural} to be literal strings, since @command{xgettext} can
  59. extract them from the source to build a message catalogue. For
  60. example,
  61. @example
  62. (define (done n)
  63. (format #t (ngettext "~a file processed\n"
  64. "~a files processed\n" n)
  65. n))
  66. (done 1) @print{} 1 file processed
  67. (done 3) @print{} 3 files processed
  68. @end example
  69. It's important to use @code{ngettext} rather than plain @code{gettext}
  70. for plurals, since the rules for singular and plural forms in English
  71. are not the same in other languages. Only @code{ngettext} will allow
  72. translators to give correct forms (@pxref{Plural forms,, Additional
  73. functions for plural forms, gettext, GNU @code{gettext} utilities}).
  74. @end deffn
  75. @deffn {Scheme Procedure} textdomain [domain]
  76. @deffnx {C Function} scm_textdomain (domain)
  77. Get or set the default gettext domain. When called with no parameter
  78. the current domain is returned. When called with a parameter,
  79. @var{domain} is set as the current domain, and that new value
  80. returned. For example,
  81. @example
  82. (textdomain "myprog")
  83. @result{} "myprog"
  84. @end example
  85. @end deffn
  86. @deffn {Scheme Procedure} bindtextdomain domain [directory]
  87. @deffnx {C Function} scm_bindtextdomain (domain, directory)
  88. Get or set the directory under which to find message files for
  89. @var{domain}. When called without a @var{directory} the current
  90. setting is returned. When called with a @var{directory},
  91. @var{directory} is set for @var{domain} and that new setting returned.
  92. For example,
  93. @example
  94. (bindtextdomain "myprog" "/my/tree/share/locale")
  95. @result{} "/my/tree/share/locale"
  96. @end example
  97. When using Autoconf/Automake, an application should arrange for the
  98. configured @code{localedir} to get into the program (by substituting,
  99. or by generating a config file) and set that for its domain. This
  100. ensures the catalogue can be found even when installed in a
  101. non-standard location.
  102. @end deffn
  103. @deffn {Scheme Procedure} bind-textdomain-codeset domain [encoding]
  104. @deffnx {C Function} scm_bind_textdomain_codeset (domain, encoding)
  105. Get or set the text encoding to be used by @code{gettext} for messages
  106. from @var{domain}. @var{encoding} is a string, the name of a coding
  107. system, for instance @nicode{"8859_1"}. (On a Unix/POSIX system the
  108. @command{iconv} program can list all available encodings.)
  109. When called without an @var{encoding} the current setting is returned,
  110. or @code{#f} if none yet set. When called with an @var{encoding}, it
  111. is set for @var{domain} and that new setting returned. For example,
  112. @example
  113. (bind-textdomain-codeset "myprog")
  114. @result{} #f
  115. (bind-textdomain-codeset "myprog" "latin-9")
  116. @result{} "latin-9"
  117. @end example
  118. The encoding requested can be different from the translated data file,
  119. messages will be recoded as necessary. But note that when there is no
  120. translation, @code{gettext} returns its @var{msg} unchanged, ie.@:
  121. without any recoding. For that reason source message strings are best
  122. as plain ASCII.
  123. Currently Guile has no understanding of multi-byte characters, and
  124. string functions won't recognise character boundaries in multi-byte
  125. strings. An application will at least be able to pass such strings
  126. through to some output though. Perhaps this will change in the
  127. future.
  128. @end deffn
  129. @c Local Variables:
  130. @c TeX-master: "guile.texi"
  131. @c End: