locale.scm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  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.
  10. ;;;
  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.
  15. ;;;
  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/>.
  18. (define-module (gnu build locale)
  19. #:use-module (guix build utils)
  20. #:use-module (srfi srfi-1)
  21. #:use-module (ice-9 rdelim)
  22. #:use-module (ice-9 match)
  23. #:use-module (ice-9 regex)
  24. #:export (build-locale
  25. normalize-codeset
  26. locale->name+codeset
  27. read-supported-locales))
  28. (define locale-rx
  29. ;; Regexp matching a locale line in 'localedata/SUPPORTED'.
  30. (make-regexp
  31. "^[[:space:]]*([[:graph:]]+)/([[:graph:]]+)[[:space:]]*\\\\$"))
  32. (define (read-supported-locales port)
  33. "Read the 'localedata/SUPPORTED' file from PORT. That file is actually a
  34. makefile snippet, with one locale per line, and a header that can be
  35. discarded."
  36. (let loop ((locales '()))
  37. (define line
  38. (read-line port))
  39. (cond ((eof-object? line)
  40. (reverse locales))
  41. ((string-prefix? "#" (string-trim line)) ;comment
  42. (loop locales))
  43. ((string-contains line "=") ;makefile variable assignment
  44. (loop locales))
  45. (else
  46. (match (regexp-exec locale-rx line)
  47. (#f
  48. (loop locales))
  49. (m
  50. (loop (alist-cons (match:substring m 1)
  51. (match:substring m 2)
  52. locales))))))))
  53. (define (normalize-codeset codeset)
  54. "Compute the \"normalized\" variant of CODESET."
  55. ;; info "(libc) Using gettextized software", for the algorithm used to
  56. ;; compute the normalized codeset.
  57. (letrec-syntax ((-> (syntax-rules ()
  58. ((_ proc value)
  59. (proc value))
  60. ((_ proc rest ...)
  61. (proc (-> rest ...))))))
  62. (-> (lambda (str)
  63. (if (string-every char-set:digit str)
  64. (string-append "iso" str)
  65. str))
  66. string-downcase
  67. (lambda (str)
  68. (string-filter char-set:letter+digit str))
  69. codeset)))
  70. (define* (build-locale locale
  71. #:key
  72. (localedef "localedef")
  73. (directory ".")
  74. (codeset "UTF-8")
  75. (name (string-append locale "." codeset)))
  76. "Compute locale data for LOCALE and CODESET--e.g., \"en_US\" and
  77. \"UTF-8\"--with LOCALEDEF, and store it in DIRECTORY under NAME."
  78. (format #t "building locale '~a'...~%" name)
  79. (invoke localedef "--no-archive" "--prefix" directory
  80. "-i" locale "-f" codeset
  81. (string-append directory "/" name)))
  82. (define (locale->name+codeset locale)
  83. "Split a locale name such as \"aa_ER@saaho.UTF-8\" into two values: the
  84. language/territory/modifier part, and the codeset."
  85. (match (string-rindex locale #\.)
  86. (#f (values locale #f))
  87. (dot (values (string-take locale dot)
  88. (string-drop locale (+ dot 1))))))