gnu: Add maven-resources-plugin.
[jackhill/guix/guix.git] / gnu / installer / locale.scm
index 2b45b22..ccffb6d 100644 (file)
@@ -1,5 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
+;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -18,6 +19,7 @@
 
 (define-module (gnu installer locale)
   #:use-module (gnu installer utils)
+  #:use-module ((gnu build locale) #:select (normalize-codeset))
   #:use-module (guix records)
   #:use-module (json)
   #:use-module (srfi srfi-1)
 (define (locale-modifier assoc)
   (assoc-ref assoc 'modifier))
 
-(define (locale-string->locale string)
-  "Return the locale association list built from the parsing of STRING."
+(define* (locale-string->locale string #:optional codeset)
+  "Return the locale association list built from the parsing of STRING and,
+optionally, CODESET."
   (let ((matches (string-match locale-regexp string)))
     `((language  . ,(match:substring matches 1))
       (territory . ,(match:substring matches 3))
-      (codeset   . ,(match:substring matches 5))
+      (codeset   . ,(or codeset (match:substring matches 5)))
       (modifier  . ,(match:substring matches 7)))))
 
 (define (locale->locale-string locale)
                    `("_" ,territory)
                    '())
              ,@(if codeset
-                   `("." ,codeset)
+                   `("." ,(normalize-codeset codeset))
                    '())
              ,@(if modifier
                    `("@" ,modifier)
                    '())))))
 
 (define (supported-locales->locales supported-locales)
-  "Parse the SUPPORTED-LOCALES file from the glibc and return the matching
-list of LOCALE association lists."
- (call-with-input-file supported-locales
-    (lambda (port)
-      (let ((lines (read-lines port)))
-        (map (lambda (line)
-               (match (string-split line #\ )
-                 ((locale-string codeset)
-                  (let ((line-locale (locale-string->locale locale-string)))
-                    (assoc-set! line-locale 'codeset codeset)))))
-             lines)))))
+  "Given SUPPORTED-LOCALES, a file produced by 'glibc-supported-locales',
+return a list of locales where each locale is an alist."
+  (map (match-lambda
+         ((locale . codeset)
+          (locale-string->locale locale codeset)))
+       (call-with-input-file supported-locales read)))
 
 \f
 ;;;
@@ -136,16 +134,18 @@ ISO639-3 and ISO639-5 files."
         (lambda (port-iso639-5)
           (filter-map
            (lambda (hash)
-             (let ((alpha2 (hash-ref hash "alpha_2"))
-                   (alpha3 (hash-ref hash "alpha_3"))
-                   (name   (hash-ref hash "name")))
+             (let ((alpha2 (assoc-ref hash "alpha_2"))
+                   (alpha3 (assoc-ref hash "alpha_3"))
+                   (name   (assoc-ref hash "name")))
                (and (supported-locale? locales alpha2 alpha3)
                     `((alpha2 . ,alpha2)
                       (alpha3 . ,alpha3)
                       (name   . ,name)))))
            (append
-            (hash-ref (json->scm port-iso639-3) "639-3")
-            (hash-ref (json->scm port-iso639-5) "639-5"))))))))
+            (vector->list
+             (assoc-ref (json->scm port-iso639-3) "639-3"))
+            (vector->list
+             (assoc-ref (json->scm port-iso639-5) "639-5")))))))))
 
 (define (language-code->language-name languages language-code)
   "Using LANGUAGES as a list of ISO639 association lists, return the language
@@ -181,10 +181,11 @@ ISO3166 file."
   (call-with-input-file iso3166
     (lambda (port)
       (map (lambda (hash)
-             `((alpha2 . ,(hash-ref hash "alpha_2"))
-               (alpha3 . ,(hash-ref hash "alpha_3"))
-               (name   . ,(hash-ref hash "name"))))
-           (hash-ref (json->scm port) "3166-1")))))
+             `((alpha2 . ,(assoc-ref hash "alpha_2"))
+               (alpha3 . ,(assoc-ref hash "alpha_3"))
+               (name   . ,(assoc-ref hash "name"))))
+           (vector->list
+            (assoc-ref (json->scm port) "3166-1"))))))
 
 (define (territory-code->territory-name territories territory-code)
   "Using TERRITORIES as a list of ISO3166 association lists return the