locale.scm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2018 Mark H Weaver <mhw@netris.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu system locale)
  20. #:use-module (guix gexp)
  21. #:use-module (guix store)
  22. #:use-module (guix modules)
  23. #:use-module (guix records)
  24. #:use-module (guix packages)
  25. #:use-module (guix utils)
  26. #:use-module (gnu packages base)
  27. #:use-module (gnu packages compression)
  28. #:use-module (srfi srfi-26)
  29. #:use-module (ice-9 match)
  30. #:export (locale-definition
  31. locale-definition?
  32. locale-definition-name
  33. locale-definition-source
  34. locale-definition-charset
  35. locale-name->definition
  36. locale-directory
  37. %default-locale-libcs
  38. %default-locale-definitions
  39. glibc-supported-locales))
  40. ;;; Commentary:
  41. ;;;
  42. ;;; Locale definitions, and compilation thereof.
  43. ;;;
  44. ;;; Code:
  45. (define-record-type* <locale-definition> locale-definition
  46. make-locale-definition
  47. locale-definition?
  48. (name locale-definition-name) ;string--e.g., "fr_FR.utf8"
  49. (source locale-definition-source) ;string--e.g., "fr_FR"
  50. (charset locale-definition-charset ;string--e.g., "UTF-8"
  51. (default "UTF-8")))
  52. (define %not-dot
  53. (char-set-complement (char-set #\.)))
  54. (define (denormalize-codeset codeset)
  55. "Attempt to guess the \"real\" name of CODESET, a normalized codeset as
  56. defined in (info \"(libc) Using gettextized software\")."
  57. (cond ((string=? codeset "utf8")
  58. "UTF-8")
  59. ((string-prefix? "iso8859" codeset)
  60. (string-append "ISO-8859-" (string-drop codeset 7)))
  61. ((string=? codeset "eucjp")
  62. "EUC-JP")
  63. (else ;cross fingers, hope for the best
  64. codeset)))
  65. (define (locale-name->definition name)
  66. "Return a <locale-definition> corresponding to NAME, guessing the charset,
  67. or #f on failure."
  68. (match (string-tokenize name %not-dot)
  69. ((source charset)
  70. ;; XXX: NAME is supposed to use the "normalized codeset", such as "utf8",
  71. ;; whereas the actual name used is different. Add a special case to make
  72. ;; the right guess for UTF-8.
  73. (locale-definition (name name)
  74. (source source)
  75. (charset (denormalize-codeset charset))))
  76. (_
  77. #f)))
  78. (define* (single-locale-directory locales
  79. #:key (libc glibc))
  80. "Return a directory containing all of LOCALES for LIBC compiled.
  81. Because locale data formats are incompatible when switching from one libc to
  82. another, locale data is put in a sub-directory named after the 'version' field
  83. of LIBC."
  84. (define version
  85. (version-major+minor (package-version libc)))
  86. (define build
  87. (with-imported-modules (source-module-closure
  88. '((gnu build locale)))
  89. #~(begin
  90. (use-modules (gnu build locale))
  91. (mkdir #$output)
  92. (mkdir (string-append #$output "/" #$version))
  93. ;; 'localedef' executes 'gzip' to access compressed locale sources.
  94. (setenv "PATH"
  95. (string-append #+gzip "/bin:" #+libc "/bin"))
  96. (setvbuf (current-output-port) 'line)
  97. (setvbuf (current-error-port) 'line)
  98. (for-each (lambda (locale codeset name)
  99. (build-locale locale
  100. #:codeset codeset
  101. #:name name
  102. #:directory
  103. (string-append #$output "/" #$version)))
  104. '#$(map locale-definition-source locales)
  105. '#$(map locale-definition-charset locales)
  106. '#$(map locale-definition-name locales)))))
  107. (computed-file (string-append "locale-" version) build))
  108. (define* (locale-directory locales
  109. #:key (libcs %default-locale-libcs))
  110. "Return a locale directory containing all of LOCALES for each libc package
  111. listed in LIBCS.
  112. It is useful to list more than one libc when willing to support
  113. already-installed packages built against a different libc since the locale
  114. data format changes between libc versions."
  115. (match libcs
  116. ((libc)
  117. (single-locale-directory locales #:libc libc))
  118. ((libcs ..1)
  119. (let ((dirs (map (lambda (libc)
  120. (single-locale-directory locales #:libc libc))
  121. libcs)))
  122. (computed-file "locale-multiple-versions"
  123. (with-imported-modules '((guix build union))
  124. #~(begin
  125. (use-modules (guix build union))
  126. (union-build #$output (list #$@dirs))))
  127. #:options '(#:local-build? #t
  128. #:substitutable? #f))))))
  129. (define %default-locale-libcs
  130. ;; The libcs for which we build locales by default.
  131. (list glibc-2.31 glibc))
  132. (define %default-locale-definitions
  133. ;; Arbitrary set of locales that are built by default. They are here mostly
  134. ;; to facilitate first-time use to some people, while others may have to add
  135. ;; a specific <locale-definition>.
  136. (letrec-syntax ((utf8-locale (syntax-rules ()
  137. ((_ name*)
  138. (locale-definition
  139. ;; Note: We choose "utf8", which is the
  140. ;; "normalized codeset".
  141. (name (string-append name* ".utf8"))
  142. (source name*)
  143. (charset "UTF-8")))))
  144. (utf8-locales (syntax-rules ()
  145. ((_ name ...)
  146. (list (utf8-locale name) ...)))))
  147. ;; Add "en_US.UTF-8" for compatibility with Guix 0.8.
  148. (cons (locale-definition
  149. (name "en_US.UTF-8")
  150. (source "en_US")
  151. (charset "UTF-8"))
  152. (utf8-locales "ca_ES"
  153. "cs_CZ"
  154. "da_DK"
  155. "de_DE"
  156. "el_GR"
  157. "en_AU"
  158. "en_CA"
  159. "en_GB"
  160. "en_US"
  161. "es_AR"
  162. "es_CL"
  163. "es_ES"
  164. "es_MX"
  165. "fi_FI"
  166. "fr_BE"
  167. "fr_CA"
  168. "fr_CH"
  169. "fr_FR"
  170. "ga_IE"
  171. "it_IT"
  172. "ja_JP"
  173. "ko_KR"
  174. "nb_NO"
  175. "nl_NL"
  176. "pl_PL"
  177. "pt_PT"
  178. "ro_RO"
  179. "ru_RU"
  180. "sv_SE"
  181. "tr_TR"
  182. "uk_UA"
  183. "vi_VN"
  184. "zh_CN"))))
  185. ;;;
  186. ;;; Locales supported by glibc.
  187. ;;;
  188. (define* (glibc-supported-locales #:optional (glibc glibc))
  189. "Return a file-like object that contains a list of locale name/encoding
  190. pairs such as (\"oc_FR.UTF-8\" . \"UTF-8\"). Each pair corresponds to a
  191. locale supported by GLIBC."
  192. (define build
  193. (with-imported-modules (source-module-closure
  194. '((guix build gnu-build-system)
  195. (gnu build locale)))
  196. #~(begin
  197. (use-modules (guix build gnu-build-system)
  198. (gnu build locale)
  199. (ice-9 pretty-print))
  200. (define unpack
  201. (assq-ref %standard-phases 'unpack))
  202. (setenv "PATH"
  203. (string-append #+(file-append tar "/bin") ":"
  204. #+(file-append xz "/bin") ":"
  205. #+(file-append gzip "/bin")))
  206. (unpack #:source #+(package-source glibc))
  207. (let ((locales (call-with-input-file "localedata/SUPPORTED"
  208. read-supported-locales)))
  209. (call-with-output-file #$output
  210. (lambda (port)
  211. (pretty-print locales port)))))))
  212. (computed-file "glibc-supported-locales.scm" build))
  213. ;;; locale.scm ends here