locale.scm 7.1 KB

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