1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; 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 monads)
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
32 locale-definition-name
33 locale-definition-source
34 locale-definition-charset
36 locale-name->definition
40 %default-locale-definitions))
44 ;;; Locale definitions, and compilation thereof.
48 (define-record-type* <locale-definition> locale-definition
49 make-locale-definition
51 (name locale-definition-name) ;string--e.g., "fr_FR.utf8"
52 (source locale-definition-source) ;string--e.g., "fr_FR"
53 (charset locale-definition-charset ;string--e.g., "UTF-8"
57 (char-set-complement (char-set #\.)))
59 (define (denormalize-codeset codeset)
60 "Attempt to guess the \"real\" name of CODESET, a normalized codeset as
61 defined in (info \"(libc) Using gettextized software\")."
62 (cond ((string=? codeset "utf8")
64 ((string-prefix? "iso8859" codeset)
65 (string-append "ISO-8859-" (string-drop codeset 7)))
66 ((string=? codeset "eucjp")
68 (else ;cross fingers, hope for the best
71 (define (locale-name->definition name)
72 "Return a <locale-definition> corresponding to NAME, guessing the charset,
74 (match (string-tokenize name %not-dot)
76 ;; XXX: NAME is supposed to use the "normalized codeset", such as "utf8",
77 ;; whereas the actual name used is different. Add a special case to make
78 ;; the right guess for UTF-8.
79 (locale-definition (name name)
81 (charset (denormalize-codeset charset))))
85 (define* (localedef-command locale
86 #:key (libc (canonical-package glibc)))
87 "Return a gexp that runs 'localedef' from LIBC to build LOCALE."
88 (define (maybe-version-directory)
89 ;; XXX: For libc prior to 2.22, GuixSD did not store locale data in a
90 ;; version-specific sub-directory. Check whether this is the case.
91 ;; TODO: Remove this hack once libc 2.21 is buried.
92 (let ((version (package-version libc)))
93 (if (version>=? version "2.22")
98 (format #t "building locale '~a'...~%"
99 #$(locale-definition-name locale))
100 (zero? (system* (string-append #$libc "/bin/localedef")
101 "--no-archive" "--prefix" #$output
102 "-i" #$(locale-definition-source locale)
103 "-f" #$(locale-definition-charset locale)
104 (string-append #$output "/"
105 #$@(maybe-version-directory)
106 #$(locale-definition-name locale))))))
108 (define* (single-locale-directory locales
109 #:key (libc (canonical-package glibc)))
110 "Return a directory containing all of LOCALES for LIBC compiled.
112 Because locale data formats are incompatible when switching from one libc to
113 another, locale data is put in a sub-directory named after the 'version' field
116 (package-version libc))
122 ;; XXX: For libcs < 2.22, locale data is stored in the top-level
124 ;; TODO: Remove this hack once libc 2.21 is buried.
125 #$(if (version>=? version "2.22")
126 #~(mkdir (string-append #$output "/" #$version))
127 #~(symlink "." (string-append #$output "/" #$version)))
129 ;; 'localedef' executes 'gzip' to access compressed locale sources.
130 (setenv "PATH" (string-append #$gzip "/bin"))
133 (and #$@(map (cut localedef-command <> #:libc libc)
136 (gexp->derivation (string-append "locale-" version) build
139 (define* (locale-directory locales
140 #:key (libcs %default-locale-libcs))
141 "Return a locale directory containing all of LOCALES for each libc package
144 It is useful to list more than one libc when willing to support
145 already-installed packages built against a different libc since the locale
146 data format changes between libc versions."
149 (single-locale-directory locales #:libc libc))
151 (mlet %store-monad ((dirs (mapm %store-monad
153 (single-locale-directory locales
156 (gexp->derivation "locale-multiple-versions"
158 (use-modules (guix build union))
159 (union-build #$output (list #$@dirs)))
160 #:modules '((guix build union))
162 #:substitutable? #f)))))
164 (define %default-locale-libcs
165 ;; The libcs for which we build locales by default.
166 (list (canonical-package glibc)))
168 (define %default-locale-definitions
169 ;; Arbitrary set of locales that are built by default. They are here mostly
170 ;; to facilitate first-time use to some people, while others may have to add
171 ;; a specific <locale-definition>.
172 (letrec-syntax ((utf8-locale (syntax-rules ()
175 ;; Note: We choose "utf8", which is the
176 ;; "normalized codeset".
177 (name (string-append name* ".utf8"))
179 (charset "UTF-8")))))
180 (utf8-locales (syntax-rules ()
182 (list (utf8-locale name) ...)))))
183 ;; Add "en_US.UTF-8" for compatibility with Guix 0.8.
184 (cons (locale-definition
188 (utf8-locales "ca_ES"
222 ;;; locale.scm ends here