1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017 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 records)
23 #:use-module (guix packages)
24 #:use-module (gnu packages base)
25 #:use-module (gnu packages compression)
26 #:use-module (srfi srfi-26)
27 #:use-module (ice-9 match)
28 #:export (locale-definition
30 locale-definition-name
31 locale-definition-source
32 locale-definition-charset
34 locale-name->definition
38 %default-locale-definitions))
42 ;;; Locale definitions, and compilation thereof.
46 (define-record-type* <locale-definition> locale-definition
47 make-locale-definition
49 (name locale-definition-name) ;string--e.g., "fr_FR.utf8"
50 (source locale-definition-source) ;string--e.g., "fr_FR"
51 (charset locale-definition-charset ;string--e.g., "UTF-8"
55 (char-set-complement (char-set #\.)))
57 (define (denormalize-codeset codeset)
58 "Attempt to guess the \"real\" name of CODESET, a normalized codeset as
59 defined in (info \"(libc) Using gettextized software\")."
60 (cond ((string=? codeset "utf8")
62 ((string-prefix? "iso8859" codeset)
63 (string-append "ISO-8859-" (string-drop codeset 7)))
64 ((string=? codeset "eucjp")
66 (else ;cross fingers, hope for the best
69 (define (locale-name->definition name)
70 "Return a <locale-definition> corresponding to NAME, guessing the charset,
72 (match (string-tokenize name %not-dot)
74 ;; XXX: NAME is supposed to use the "normalized codeset", such as "utf8",
75 ;; whereas the actual name used is different. Add a special case to make
76 ;; the right guess for UTF-8.
77 (locale-definition (name name)
79 (charset (denormalize-codeset charset))))
83 (define* (localedef-command locale
84 #:key (libc (canonical-package glibc)))
85 "Return a gexp that runs 'localedef' from LIBC to build LOCALE."
87 (format #t "building locale '~a'...~%"
88 #$(locale-definition-name locale))
89 (zero? (system* (string-append #+libc "/bin/localedef")
90 "--no-archive" "--prefix" #$output
91 "-i" #$(locale-definition-source locale)
92 "-f" #$(locale-definition-charset locale)
93 (string-append #$output "/"
94 #$(package-version libc) "/"
95 #$(locale-definition-name locale))))))
97 (define* (single-locale-directory locales
98 #:key (libc (canonical-package glibc)))
99 "Return a directory containing all of LOCALES for LIBC compiled.
101 Because locale data formats are incompatible when switching from one libc to
102 another, locale data is put in a sub-directory named after the 'version' field
105 (package-version libc))
111 (mkdir (string-append #$output "/" #$version))
113 ;; 'localedef' executes 'gzip' to access compressed locale sources.
114 (setenv "PATH" (string-append #$gzip "/bin"))
117 (and #$@(map (cut localedef-command <> #:libc libc)
120 (computed-file (string-append "locale-" version) build))
122 (define* (locale-directory locales
123 #:key (libcs %default-locale-libcs))
124 "Return a locale directory containing all of LOCALES for each libc package
127 It is useful to list more than one libc when willing to support
128 already-installed packages built against a different libc since the locale
129 data format changes between libc versions."
132 (single-locale-directory locales #:libc libc))
134 (let ((dirs (map (lambda (libc)
135 (single-locale-directory locales #:libc libc))
137 (computed-file "locale-multiple-versions"
138 (with-imported-modules '((guix build union))
140 (use-modules (guix build union))
141 (union-build #$output (list #$@dirs))))
142 #:options '(#:local-build? #t
143 #:substitutable? #f))))))
145 (define %default-locale-libcs
146 ;; The libcs for which we build locales by default.
147 (list (canonical-package glibc)))
149 (define %default-locale-definitions
150 ;; Arbitrary set of locales that are built by default. They are here mostly
151 ;; to facilitate first-time use to some people, while others may have to add
152 ;; a specific <locale-definition>.
153 (letrec-syntax ((utf8-locale (syntax-rules ()
156 ;; Note: We choose "utf8", which is the
157 ;; "normalized codeset".
158 (name (string-append name* ".utf8"))
160 (charset "UTF-8")))))
161 (utf8-locales (syntax-rules ()
163 (list (utf8-locale name) ...)))))
164 ;; Add "en_US.UTF-8" for compatibility with Guix 0.8.
165 (cons (locale-definition
169 (utf8-locales "ca_ES"
203 ;;; locale.scm ends here