locale.scm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. (map (match-lambda
  85. ((locale . codeset)
  86. (locale-string->locale locale codeset)))
  87. (call-with-input-file supported-locales read)))
  88. ;;;
  89. ;;; Language.
  90. ;;;
  91. (define (iso639-language-alpha2 assoc)
  92. (assoc-ref assoc 'alpha2))
  93. (define (iso639-language-alpha3 assoc)
  94. (assoc-ref assoc 'alpha3))
  95. (define (iso639-language-name assoc)
  96. (assoc-ref assoc 'name))
  97. (define (supported-locale? locales alpha2 alpha3)
  98. "Find a locale in LOCALES whose alpha2 field matches ALPHA-2 or alpha3 field
  99. matches ALPHA-3. The ISO639 standard specifies that ALPHA-2 is optional. Thus,
  100. if ALPHA-2 is #f, only consider ALPHA-3. Return #f if not matching locale was
  101. found."
  102. (find (lambda (locale)
  103. (let ((language (locale-language locale)))
  104. (or (and=> alpha2
  105. (lambda (code)
  106. (string=? language code)))
  107. (string=? language alpha3))))
  108. locales))
  109. (define (iso639->iso639-languages locales iso639-3 iso639-5)
  110. "Return a list of ISO639 association lists created from the parsing of
  111. ISO639-3 and ISO639-5 files."
  112. (call-with-input-file iso639-3
  113. (lambda (port-iso639-3)
  114. (call-with-input-file iso639-5
  115. (lambda (port-iso639-5)
  116. (filter-map
  117. (lambda (hash)
  118. (let ((alpha2 (assoc-ref hash "alpha_2"))
  119. (alpha3 (assoc-ref hash "alpha_3"))
  120. (name (assoc-ref hash "name")))
  121. (and (supported-locale? locales alpha2 alpha3)
  122. `((alpha2 . ,alpha2)
  123. (alpha3 . ,alpha3)
  124. (name . ,name)))))
  125. (append
  126. (vector->list
  127. (assoc-ref (json->scm port-iso639-3) "639-3"))
  128. (vector->list
  129. (assoc-ref (json->scm port-iso639-5) "639-5")))))))))
  130. (define (language-code->language-name languages language-code)
  131. "Using LANGUAGES as a list of ISO639 association lists, return the language
  132. name corresponding to the given LANGUAGE-CODE."
  133. (let ((iso639-language
  134. (find (lambda (language)
  135. (or
  136. (and=> (iso639-language-alpha2 language)
  137. (lambda (alpha2)
  138. (string=? alpha2 language-code)))
  139. (string=? (iso639-language-alpha3 language)
  140. language-code)))
  141. languages)))
  142. (iso639-language-name iso639-language)))
  143. ;;;
  144. ;;; Territory.
  145. ;;;
  146. (define (iso3166-territory-alpha2 assoc)
  147. (assoc-ref assoc 'alpha2))
  148. (define (iso3166-territory-alpha3 assoc)
  149. (assoc-ref assoc 'alpha3))
  150. (define (iso3166-territory-name assoc)
  151. (assoc-ref assoc 'name))
  152. (define (iso3166->iso3166-territories iso3166)
  153. "Return a list of ISO3166 association lists created from the parsing of
  154. ISO3166 file."
  155. (call-with-input-file iso3166
  156. (lambda (port)
  157. (map (lambda (hash)
  158. `((alpha2 . ,(assoc-ref hash "alpha_2"))
  159. (alpha3 . ,(assoc-ref hash "alpha_3"))
  160. (name . ,(assoc-ref hash "name"))))
  161. (vector->list
  162. (assoc-ref (json->scm port) "3166-1"))))))
  163. (define (territory-code->territory-name territories territory-code)
  164. "Using TERRITORIES as a list of ISO3166 association lists return the
  165. territory name corresponding to the given TERRITORY-CODE."
  166. (let ((iso3166-territory
  167. (find (lambda (territory)
  168. (or
  169. (and=> (iso3166-territory-alpha2 territory)
  170. (lambda (alpha2)
  171. (string=? alpha2 territory-code)))
  172. (string=? (iso3166-territory-alpha3 territory)
  173. territory-code)))
  174. territories)))
  175. (iso3166-territory-name iso3166-territory)))
  176. ;;;
  177. ;;; Configuration formatter.
  178. ;;;
  179. (define (locale->configuration locale)
  180. "Return the configuration field for LOCALE."
  181. `((locale ,locale)))