locale.scm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.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 installer locale)
  20. #:use-module (gnu installer utils)
  21. #:use-module ((gnu build locale) #:select (normalize-codeset))
  22. #:use-module (guix records)
  23. #:use-module (json)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (ice-9 match)
  26. #:use-module (ice-9 regex)
  27. #:export (locale-language
  28. locale-territory
  29. locale-codeset
  30. locale-modifier
  31. locale->locale-string
  32. supported-locales->locales
  33. iso639->iso639-languages
  34. language-code->language-name
  35. iso3166->iso3166-territories
  36. territory-code->territory-name
  37. locale->configuration))
  38. ;;;
  39. ;;; Locale.
  40. ;;;
  41. ;; A glibc locale string has the following format:
  42. ;; language[_territory[.codeset][@modifier]].
  43. (define locale-regexp "^([^_@]+)(_([^\\.@]+))?(\\.([^@]+))?(@([^$]+))?$")
  44. ;; LOCALE will be better expressed in a (guix record) that in an association
  45. ;; list. However, loading large files containing records does not scale
  46. ;; well. The same thing goes for ISO639 and ISO3166 association lists used
  47. ;; later in this module.
  48. (define (locale-language assoc)
  49. (assoc-ref assoc 'language))
  50. (define (locale-territory assoc)
  51. (assoc-ref assoc 'territory))
  52. (define (locale-codeset assoc)
  53. (assoc-ref assoc 'codeset))
  54. (define (locale-modifier assoc)
  55. (assoc-ref assoc 'modifier))
  56. (define* (locale-string->locale string #:optional codeset)
  57. "Return the locale association list built from the parsing of STRING and,
  58. optionally, CODESET."
  59. (let ((matches (string-match locale-regexp string)))
  60. `((language . ,(match:substring matches 1))
  61. (territory . ,(match:substring matches 3))
  62. (codeset . ,(or codeset (match:substring matches 5)))
  63. (modifier . ,(match:substring matches 7)))))
  64. (define (locale->locale-string locale)
  65. "Reverse operation of locale-string->locale."
  66. (let ((language (locale-language locale))
  67. (territory (locale-territory locale))
  68. (codeset (locale-codeset locale))
  69. (modifier (locale-modifier locale)))
  70. (apply string-append
  71. `(,language
  72. ,@(if territory
  73. `("_" ,territory)
  74. '())
  75. ,@(if codeset
  76. `("." ,(normalize-codeset codeset))
  77. '())
  78. ,@(if modifier
  79. `("@" ,modifier)
  80. '())))))
  81. (define (supported-locales->locales supported-locales)
  82. "Given SUPPORTED-LOCALES, a file produced by 'glibc-supported-locales',
  83. return a list of locales where each locale is an alist."
  84. (filter-map (match-lambda
  85. (("C.UTF-8" . codeset) #f)
  86. ((locale . codeset)
  87. (locale-string->locale locale codeset)))
  88. (call-with-input-file supported-locales read)))
  89. ;;;
  90. ;;; Language.
  91. ;;;
  92. (define (iso639-language-alpha2 assoc)
  93. (assoc-ref assoc 'alpha2))
  94. (define (iso639-language-alpha3 assoc)
  95. (assoc-ref assoc 'alpha3))
  96. (define (iso639-language-name assoc)
  97. (assoc-ref assoc 'name))
  98. (define (supported-locale? locales alpha2 alpha3)
  99. "Find a locale in LOCALES whose alpha2 field matches ALPHA-2 or alpha3 field
  100. matches ALPHA-3. The ISO639 standard specifies that ALPHA-2 is optional. Thus,
  101. if ALPHA-2 is #f, only consider ALPHA-3. Return #f if not matching locale was
  102. found."
  103. (find (lambda (locale)
  104. (let ((language (locale-language locale)))
  105. (or (and=> alpha2
  106. (lambda (code)
  107. (string=? language code)))
  108. (string=? language alpha3))))
  109. locales))
  110. (define (iso639->iso639-languages locales iso639-3 iso639-5)
  111. "Return a list of ISO639 association lists created from the parsing of
  112. ISO639-3 and ISO639-5 files."
  113. (call-with-input-file iso639-3
  114. (lambda (port-iso639-3)
  115. (call-with-input-file iso639-5
  116. (lambda (port-iso639-5)
  117. (filter-map
  118. (lambda (hash)
  119. (let ((alpha2 (assoc-ref hash "alpha_2"))
  120. (alpha3 (assoc-ref hash "alpha_3"))
  121. (name (assoc-ref hash "name")))
  122. (and (supported-locale? locales alpha2 alpha3)
  123. `((alpha2 . ,alpha2)
  124. (alpha3 . ,alpha3)
  125. (name . ,name)))))
  126. (append
  127. (vector->list
  128. (assoc-ref (json->scm port-iso639-3) "639-3"))
  129. (vector->list
  130. (assoc-ref (json->scm port-iso639-5) "639-5")))))))))
  131. (define (language-code->language-name languages language-code)
  132. "Using LANGUAGES as a list of ISO639 association lists, return the language
  133. name corresponding to the given LANGUAGE-CODE."
  134. (let ((iso639-language
  135. (find (lambda (language)
  136. (or
  137. (and=> (iso639-language-alpha2 language)
  138. (lambda (alpha2)
  139. (string=? alpha2 language-code)))
  140. (string=? (iso639-language-alpha3 language)
  141. language-code)))
  142. languages)))
  143. (iso639-language-name iso639-language)))
  144. ;;;
  145. ;;; Territory.
  146. ;;;
  147. (define (iso3166-territory-alpha2 assoc)
  148. (assoc-ref assoc 'alpha2))
  149. (define (iso3166-territory-alpha3 assoc)
  150. (assoc-ref assoc 'alpha3))
  151. (define (iso3166-territory-name assoc)
  152. (assoc-ref assoc 'name))
  153. (define (iso3166->iso3166-territories iso3166)
  154. "Return a list of ISO3166 association lists created from the parsing of
  155. ISO3166 file."
  156. (call-with-input-file iso3166
  157. (lambda (port)
  158. (map (lambda (hash)
  159. `((alpha2 . ,(assoc-ref hash "alpha_2"))
  160. (alpha3 . ,(assoc-ref hash "alpha_3"))
  161. (name . ,(assoc-ref hash "name"))))
  162. (vector->list
  163. (assoc-ref (json->scm port) "3166-1"))))))
  164. (define (territory-code->territory-name territories territory-code)
  165. "Using TERRITORIES as a list of ISO3166 association lists return the
  166. territory name corresponding to the given TERRITORY-CODE."
  167. (let ((iso3166-territory
  168. (find (lambda (territory)
  169. (or
  170. (and=> (iso3166-territory-alpha2 territory)
  171. (lambda (alpha2)
  172. (string=? alpha2 territory-code)))
  173. (string=? (iso3166-territory-alpha3 territory)
  174. territory-code)))
  175. territories)))
  176. (iso3166-territory-name iso3166-territory)))
  177. ;;;
  178. ;;; Configuration formatter.
  179. ;;;
  180. (define (locale->configuration locale)
  181. "Return the configuration field for LOCALE."
  182. `((locale ,locale)))