locale.scm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2019, 2020 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 the previous and current libc to ease transition.
  132. (list glibc-2.29 glibc))
  133. (define %default-locale-definitions
  134. ;; Arbitrary set of locales that are built by default. They are here mostly
  135. ;; to facilitate first-time use to some people, while others may have to add
  136. ;; a specific <locale-definition>.
  137. (letrec-syntax ((utf8-locale (syntax-rules ()
  138. ((_ name*)
  139. (locale-definition
  140. ;; Note: We choose "utf8", which is the
  141. ;; "normalized codeset".
  142. (name (string-append name* ".utf8"))
  143. (source name*)
  144. (charset "UTF-8")))))
  145. (utf8-locales (syntax-rules ()
  146. ((_ name ...)
  147. (list (utf8-locale name) ...)))))
  148. ;; Add "en_US.UTF-8" for compatibility with Guix 0.8.
  149. (cons (locale-definition
  150. (name "en_US.UTF-8")
  151. (source "en_US")
  152. (charset "UTF-8"))
  153. (utf8-locales "ca_ES"
  154. "cs_CZ"
  155. "da_DK"
  156. "de_DE"
  157. "el_GR"
  158. "en_AU"
  159. "en_CA"
  160. "en_GB"
  161. "en_US"
  162. "es_AR"
  163. "es_CL"
  164. "es_ES"
  165. "es_MX"
  166. "fi_FI"
  167. "fr_BE"
  168. "fr_CA"
  169. "fr_CH"
  170. "fr_FR"
  171. "ga_IE"
  172. "it_IT"
  173. "ja_JP"
  174. "ko_KR"
  175. "nb_NO"
  176. "nl_NL"
  177. "pl_PL"
  178. "pt_PT"
  179. "ro_RO"
  180. "ru_RU"
  181. "sv_SE"
  182. "tr_TR"
  183. "uk_UA"
  184. "vi_VN"
  185. "zh_CN"))))
  186. ;;;
  187. ;;; Locales supported by glibc.
  188. ;;;
  189. (define* (glibc-supported-locales #:optional (glibc glibc))
  190. "Return a file-like object that contains a list of locale name/encoding
  191. pairs such as (\"oc_FR.UTF-8\" . \"UTF-8\"). Each pair corresponds to a
  192. locale supported by GLIBC."
  193. (define build
  194. (with-imported-modules (source-module-closure
  195. '((guix build gnu-build-system)
  196. (gnu build locale)))
  197. #~(begin
  198. (use-modules (guix build gnu-build-system)
  199. (gnu build locale)
  200. (ice-9 pretty-print))
  201. (define unpack
  202. (assq-ref %standard-phases 'unpack))
  203. (setenv "PATH"
  204. (string-append #+(file-append tar "/bin") ":"
  205. #+(file-append xz "/bin") ":"
  206. #+(file-append gzip "/bin")))
  207. (unpack #:source #+(package-source glibc))
  208. (let ((locales (call-with-input-file "localedata/SUPPORTED"
  209. read-supported-locales)))
  210. (call-with-output-file #$output
  211. (lambda (port)
  212. (pretty-print locales port)))))))
  213. (computed-file "glibc-supported-locales.scm" build))
  214. ;;; locale.scm ends here