locale.scm 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 newt locale)
  19. #:use-module (gnu installer locale)
  20. #:use-module (gnu installer steps)
  21. #:use-module (gnu installer newt page)
  22. #:use-module (guix i18n)
  23. #:use-module (newt)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-26)
  26. #:use-module (srfi srfi-34)
  27. #:use-module (srfi srfi-35)
  28. #:use-module (ice-9 match)
  29. #:export (run-locale-page))
  30. (define (run-language-page languages language->text)
  31. (let ((title (G_ "Locale language")))
  32. (run-listbox-selection-page
  33. #:title title
  34. #:info-text (G_ "Choose the locale's language to be used for the \
  35. installation process. A locale is a regional variant of your language \
  36. encompassing number, date and currency format, among other details.
  37. Based on the language you choose, you will possibly be asked to \
  38. select a locale's territory, codeset and modifier in the next \
  39. steps. The locale will also be used as the default one for the \
  40. installed system.")
  41. #:info-textbox-width 70
  42. #:listbox-items languages
  43. #:listbox-item->text language->text
  44. #:sort-listbox-items? #f
  45. #:button-text (G_ "Exit")
  46. #:button-callback-procedure
  47. (lambda _
  48. (raise
  49. (condition
  50. (&installer-step-abort)))))))
  51. (define (run-territory-page territories territory->text)
  52. (let ((title (G_ "Locale location")))
  53. (run-listbox-selection-page
  54. #:title title
  55. #:info-text (G_ "Choose your locale's location. This is a shortlist of \
  56. locations based on the language you selected.")
  57. #:listbox-items territories
  58. #:listbox-item->text territory->text
  59. #:button-text (G_ "Back")
  60. #:button-callback-procedure
  61. (lambda _
  62. (raise
  63. (condition
  64. (&installer-step-abort)))))))
  65. (define (run-codeset-page codesets)
  66. (let ((title (G_ "Locale codeset")))
  67. (run-listbox-selection-page
  68. #:title title
  69. #:info-text (G_ "Choose your locale's codeset. If UTF-8 is available, \
  70. it should be preferred.")
  71. #:listbox-items codesets
  72. #:listbox-item->text identity
  73. #:listbox-default-item "UTF-8"
  74. #:button-text (G_ "Back")
  75. #:button-callback-procedure
  76. (lambda _
  77. (raise
  78. (condition
  79. (&installer-step-abort)))))))
  80. (define (run-modifier-page modifiers modifier->text)
  81. (let ((title (G_ "Locale modifier")))
  82. (run-listbox-selection-page
  83. #:title title
  84. #:info-text (G_ "Choose your locale's modifier. The most frequent \
  85. modifier is euro. It indicates that you want to use Euro as the currency \
  86. symbol.")
  87. #:listbox-items modifiers
  88. #:listbox-item->text modifier->text
  89. #:button-text (G_ "Back")
  90. #:button-callback-procedure
  91. (lambda _
  92. (raise
  93. (condition
  94. (&installer-step-abort)))))))
  95. (define* (run-locale-page #:key
  96. supported-locales
  97. iso639-languages
  98. iso3166-territories)
  99. "Run a page asking the user to select a locale language and possibly
  100. territory, codeset and modifier. Use SUPPORTED-LOCALES as the list of glibc
  101. available locales. ISO639-LANGUAGES is an association list associating a
  102. locale code to a locale name. ISO3166-TERRITORIES is an association list
  103. associating a territory code with a territory name. The formated locale, under
  104. glibc format is returned."
  105. (define (break-on-locale-found locales)
  106. "Raise the &installer-step-break condition if LOCALES contains exactly one
  107. element."
  108. (and (= (length locales) 1)
  109. (raise
  110. (condition (&installer-step-break)))))
  111. (define (filter-locales locales result)
  112. "Filter the list of locale records LOCALES using the RESULT returned by
  113. the installer-steps defined below."
  114. (filter
  115. (lambda (locale)
  116. (and-map identity
  117. `(,(string=? (locale-language locale)
  118. (result-step result 'language))
  119. ,@(if (result-step-done? result 'territory)
  120. (list (equal? (locale-territory locale)
  121. (result-step result 'territory)))
  122. '())
  123. ,@(if (result-step-done? result 'codeset)
  124. (list (equal? (locale-codeset locale)
  125. (result-step result 'codeset)))
  126. '())
  127. ,@(if (result-step-done? result 'modifier)
  128. (list (equal? (locale-modifier locale)
  129. (result-step result 'modifier)))
  130. '()))))
  131. locales))
  132. (define (result->locale-string locales result)
  133. "Supposing that LOCALES contains exactly one locale record, turn it into a
  134. glibc locale string and return it."
  135. (match (filter-locales locales result)
  136. ((locale)
  137. (locale->locale-string locale))))
  138. (define (sort-languages languages)
  139. "Extract some languages from LANGUAGES list and place them ahead."
  140. (let* ((first-languages '("en"))
  141. (other-languages (lset-difference equal?
  142. languages
  143. first-languages)))
  144. `(,@first-languages ,@other-languages)))
  145. (define locale-steps
  146. (list
  147. (installer-step
  148. (id 'language)
  149. (compute
  150. (lambda _
  151. (run-language-page
  152. (sort-languages
  153. (delete-duplicates (map locale-language supported-locales)))
  154. (cut language-code->language-name iso639-languages <>)))))
  155. (installer-step
  156. (id 'territory)
  157. (compute
  158. (lambda (result _)
  159. (let ((locales (filter-locales supported-locales result)))
  160. ;; Stop the process if the language returned by the previous step
  161. ;; is matching one and only one supported locale.
  162. (break-on-locale-found locales)
  163. ;; Otherwise, ask the user to select a territory among those
  164. ;; supported by the previously selected language.
  165. (run-territory-page
  166. (delete-duplicates (map locale-territory locales))
  167. (lambda (territory-code)
  168. (if territory-code
  169. (territory-code->territory-name iso3166-territories
  170. territory-code)
  171. (G_ "No location"))))))))
  172. (installer-step
  173. (id 'codeset)
  174. (compute
  175. (lambda (result _)
  176. (let ((locales (filter-locales supported-locales result)))
  177. ;; Same as above but we now have a language and a territory to
  178. ;; narrow down the search of a locale.
  179. (break-on-locale-found locales)
  180. ;; Otherwise, ask for a codeset.
  181. (run-codeset-page
  182. (delete-duplicates (map locale-codeset locales)))))))
  183. (installer-step
  184. (id 'modifier)
  185. (compute
  186. (lambda (result _)
  187. (let ((locales (filter-locales supported-locales result)))
  188. ;; Same thing with a language, a territory and a codeset this time.
  189. (break-on-locale-found locales)
  190. ;; Otherwise, ask for a modifier.
  191. (run-modifier-page
  192. (delete-duplicates (map locale-modifier locales))
  193. (lambda (modifier)
  194. (or modifier (G_ "No modifier"))))))))))
  195. ;; If run-installer-steps returns locally, it means that the user had to go
  196. ;; through all steps (language, territory, codeset and modifier) to select a
  197. ;; locale. In that case, like if we exited by raising &installer-step-break
  198. ;; condition, turn the result into a glibc locale string and return it.
  199. (result->locale-string
  200. supported-locales
  201. (run-installer-steps #:steps locale-steps)))