locale.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017 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 records)
  23. #:use-module (guix packages)
  24. #:use-module (guix utils)
  25. #:use-module (gnu packages base)
  26. #:use-module (gnu packages compression)
  27. #:use-module (srfi srfi-26)
  28. #:use-module (ice-9 match)
  29. #:export (locale-definition
  30. locale-definition?
  31. locale-definition-name
  32. locale-definition-source
  33. locale-definition-charset
  34. locale-name->definition
  35. locale-directory
  36. %default-locale-libcs
  37. %default-locale-definitions))
  38. ;;; Commentary:
  39. ;;;
  40. ;;; Locale definitions, and compilation thereof.
  41. ;;;
  42. ;;; Code:
  43. (define-record-type* <locale-definition> locale-definition
  44. make-locale-definition
  45. locale-definition?
  46. (name locale-definition-name) ;string--e.g., "fr_FR.utf8"
  47. (source locale-definition-source) ;string--e.g., "fr_FR"
  48. (charset locale-definition-charset ;string--e.g., "UTF-8"
  49. (default "UTF-8")))
  50. (define %not-dot
  51. (char-set-complement (char-set #\.)))
  52. (define (denormalize-codeset codeset)
  53. "Attempt to guess the \"real\" name of CODESET, a normalized codeset as
  54. defined in (info \"(libc) Using gettextized software\")."
  55. (cond ((string=? codeset "utf8")
  56. "UTF-8")
  57. ((string-prefix? "iso8859" codeset)
  58. (string-append "ISO-8859-" (string-drop codeset 7)))
  59. ((string=? codeset "eucjp")
  60. "EUC-JP")
  61. (else ;cross fingers, hope for the best
  62. codeset)))
  63. (define (locale-name->definition name)
  64. "Return a <locale-definition> corresponding to NAME, guessing the charset,
  65. or #f on failure."
  66. (match (string-tokenize name %not-dot)
  67. ((source charset)
  68. ;; XXX: NAME is supposed to use the "normalized codeset", such as "utf8",
  69. ;; whereas the actual name used is different. Add a special case to make
  70. ;; the right guess for UTF-8.
  71. (locale-definition (name name)
  72. (source source)
  73. (charset (denormalize-codeset charset))))
  74. (_
  75. #f)))
  76. (define* (localedef-command locale
  77. #:key (libc (canonical-package glibc)))
  78. "Return a gexp that runs 'localedef' from LIBC to build LOCALE."
  79. #~(begin
  80. (format #t "building locale '~a'...~%"
  81. #$(locale-definition-name locale))
  82. (zero? (system* (string-append #+libc "/bin/localedef")
  83. "--no-archive" "--prefix" #$output
  84. "-i" #$(locale-definition-source locale)
  85. "-f" #$(locale-definition-charset locale)
  86. (string-append #$output "/" #$(version-major+minor
  87. (package-version libc))
  88. "/" #$(locale-definition-name locale))))))
  89. (define* (single-locale-directory locales
  90. #:key (libc (canonical-package glibc)))
  91. "Return a directory containing all of LOCALES for LIBC compiled.
  92. Because locale data formats are incompatible when switching from one libc to
  93. another, locale data is put in a sub-directory named after the 'version' field
  94. of LIBC."
  95. (define version
  96. (version-major+minor (package-version libc)))
  97. (define build
  98. #~(begin
  99. (mkdir #$output)
  100. (mkdir (string-append #$output "/" #$version))
  101. ;; 'localedef' executes 'gzip' to access compressed locale sources.
  102. (setenv "PATH" (string-append #$gzip "/bin"))
  103. (exit
  104. (and #$@(map (cut localedef-command <> #:libc libc)
  105. locales)))))
  106. (computed-file (string-append "locale-" version) build))
  107. (define* (locale-directory locales
  108. #:key (libcs %default-locale-libcs))
  109. "Return a locale directory containing all of LOCALES for each libc package
  110. listed in LIBCS.
  111. It is useful to list more than one libc when willing to support
  112. already-installed packages built against a different libc since the locale
  113. data format changes between libc versions."
  114. (match libcs
  115. ((libc)
  116. (single-locale-directory locales #:libc libc))
  117. ((libcs ..1)
  118. (let ((dirs (map (lambda (libc)
  119. (single-locale-directory locales #:libc libc))
  120. libcs)))
  121. (computed-file "locale-multiple-versions"
  122. (with-imported-modules '((guix build union))
  123. #~(begin
  124. (use-modules (guix build union))
  125. (union-build #$output (list #$@dirs))))
  126. #:options '(#:local-build? #t
  127. #:substitutable? #f))))))
  128. (define %default-locale-libcs
  129. ;; The libcs for which we build locales by default.
  130. (list (canonical-package glibc)))
  131. (define %default-locale-definitions
  132. ;; Arbitrary set of locales that are built by default. They are here mostly
  133. ;; to facilitate first-time use to some people, while others may have to add
  134. ;; a specific <locale-definition>.
  135. (letrec-syntax ((utf8-locale (syntax-rules ()
  136. ((_ name*)
  137. (locale-definition
  138. ;; Note: We choose "utf8", which is the
  139. ;; "normalized codeset".
  140. (name (string-append name* ".utf8"))
  141. (source name*)
  142. (charset "UTF-8")))))
  143. (utf8-locales (syntax-rules ()
  144. ((_ name ...)
  145. (list (utf8-locale name) ...)))))
  146. ;; Add "en_US.UTF-8" for compatibility with Guix 0.8.
  147. (cons (locale-definition
  148. (name "en_US.UTF-8")
  149. (source "en_US")
  150. (charset "UTF-8"))
  151. (utf8-locales "ca_ES"
  152. "cs_CZ"
  153. "da_DK"
  154. "de_DE"
  155. "el_GR"
  156. "en_AU"
  157. "en_CA"
  158. "en_GB"
  159. "en_US"
  160. "es_AR"
  161. "es_CL"
  162. "es_ES"
  163. "es_MX"
  164. "fi_FI"
  165. "fr_BE"
  166. "fr_CA"
  167. "fr_CH"
  168. "fr_FR"
  169. "ga_IE"
  170. "it_IT"
  171. "ja_JP"
  172. "ko_KR"
  173. "nb_NO"
  174. "nl_NL"
  175. "pl_PL"
  176. "pt_PT"
  177. "ro_RO"
  178. "ru_RU"
  179. "sv_SE"
  180. "tr_TR"
  181. "uk_UA"
  182. "vi_VN"
  183. "zh_CN"))))
  184. ;;; locale.scm ends here