Add `SCM_SET_SUBR_GENERIC ()' to replace `SCM_SUBR_GENERIC ()' as an lvalue.
[bpt/guile.git] / ice-9 / i18n.scm
1 ;;;; i18n.scm --- internationalization support
2
3 ;;;; Copyright (C) 2006, 2007 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 2.1 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 ;;; Author: Ludovic Courtès <ludovic.courtes@laas.fr>
20
21 ;;; Commentary:
22 ;;;
23 ;;; This module provides a number of routines that support
24 ;;; internationalization (e.g., locale-dependent text collation, character
25 ;;; mapping, etc.). It also defines `locale' objects, representing locale
26 ;;; settings, that may be passed around to most of these procedures.
27 ;;;
28
29 ;;; Code:
30
31 (define-module (ice-9 i18n)
32 :use-module (ice-9 optargs)
33 :export (;; `locale' type
34 make-locale locale?
35 %global-locale
36
37 ;; text collation
38 string-locale<? string-locale>?
39 string-locale-ci<? string-locale-ci>? string-locale-ci=?
40
41 char-locale<? char-locale>?
42 char-locale-ci<? char-locale-ci>? char-locale-ci=?
43
44 ;; character mapping
45 char-locale-downcase char-locale-upcase
46 string-locale-downcase string-locale-upcase
47
48 ;; reading numbers
49 locale-string->integer locale-string->inexact
50
51 ;; charset/encoding
52 locale-encoding
53
54 ;; days and months
55 locale-day-short locale-day locale-month-short locale-month
56
57 ;; date and time
58 locale-am-string locale-pm-string
59 locale-date+time-format locale-date-format locale-time-format
60 locale-time+am/pm-format
61 locale-era locale-era-year
62 locale-era-date-format locale-era-date+time-format
63 locale-era-time-format
64
65 ;; monetary
66 locale-currency-symbol
67 locale-monetary-decimal-point locale-monetary-thousands-separator
68 locale-monetary-grouping locale-monetary-fractional-digits
69 locale-currency-symbol-precedes-positive?
70 locale-currency-symbol-precedes-negative?
71 locale-positive-separated-by-space?
72 locale-negative-separated-by-space?
73 locale-monetary-positive-sign locale-monetary-negative-sign
74 locale-positive-sign-position locale-negative-sign-position
75 monetary-amount->locale-string
76
77 ;; number formatting
78 locale-digit-grouping locale-decimal-point
79 locale-thousands-separator
80 number->locale-string
81
82 ;; miscellaneous
83 locale-yes-regexp locale-no-regexp))
84
85
86 (load-extension "libguile-i18n-v-0" "scm_init_i18n")
87
88 \f
89 ;;;
90 ;;; Charset/encoding.
91 ;;;
92
93 (define (locale-encoding . locale)
94 (apply nl-langinfo CODESET locale))
95
96 \f
97 ;;;
98 ;;; Months and days.
99 ;;;
100
101 ;; Helper macro: Define a procedure named NAME that maps its argument to
102 ;; NL-ITEMS (when `nl-langinfo' is provided) or DEFAULTS (when `nl-langinfo'
103 ;; is not provided).
104 (define-macro (define-vector-langinfo-mapping name nl-items defaults)
105 (let* ((item-count (length nl-items))
106 (defines (if (provided? 'nl-langinfo)
107 `(define %nl-items (vector #f ,@nl-items))
108 `(define %defaults (vector #f ,@defaults))))
109 (make-body (lambda (result)
110 `(if (and (integer? item) (exact? item))
111 (if (and (>= item 1) (<= item ,item-count))
112 ,result
113 (throw 'out-of-range "out of range" item))
114 (throw 'wrong-type-arg "wrong argument type" item)))))
115 `(define (,name item . locale)
116 ,defines
117 ,(make-body (if (provided? 'nl-langinfo)
118 '(apply nl-langinfo (vector-ref %nl-items item) locale)
119 '(vector-ref %defaults item))))))
120
121
122 (define-vector-langinfo-mapping locale-day-short
123 (ABDAY_1 ABDAY_2 ABDAY_3 ABDAY_4 ABDAY_5 ABDAY_6 ABDAY_7)
124 ("Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat"))
125
126 (define-vector-langinfo-mapping locale-day
127 (DAY_1 DAY_2 DAY_3 DAY_4 DAY_5 DAY_6 DAY_7)
128 ("Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"))
129
130 (define-vector-langinfo-mapping locale-month-short
131 (ABMON_1 ABMON_2 ABMON_3 ABMON_4 ABMON_5 ABMON_6
132 ABMON_7 ABMON_8 ABMON_9 ABMON_10 ABMON_11 ABMON_12)
133 ("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"))
134
135 (define-vector-langinfo-mapping locale-month
136 (MON_1 MON_2 MON_3 MON_4 MON_5 MON_6 MON_7 MON_8 MON_9 MON_10 MON_11 MON_12)
137 ("January" "February" "March" "April" "May" "June" "July" "August"
138 "September" "October" "November" "December"))
139
140
141 \f
142 ;;;
143 ;;; Date and time.
144 ;;;
145
146 ;; Helper macro: Define a procedure NAME that gets langinfo item ITEM.
147 (define-macro (define-simple-langinfo-mapping name item default)
148 (let ((body (if (and (provided? 'nl-langinfo) (defined? item))
149 `(apply nl-langinfo ,item locale)
150 default)))
151 `(define (,name . locale)
152 ,body)))
153
154 (define-simple-langinfo-mapping locale-am-string
155 AM_STR "AM")
156 (define-simple-langinfo-mapping locale-pm-string
157 PM_STR "PM")
158 (define-simple-langinfo-mapping locale-date+time-format
159 D_T_FMT "%a %b %e %H:%M:%S %Y")
160 (define-simple-langinfo-mapping locale-date-format
161 D_FMT "%m/%d/%y")
162 (define-simple-langinfo-mapping locale-time-format
163 T_FMT "%H:%M:%S")
164 (define-simple-langinfo-mapping locale-time+am/pm-format
165 T_FMT_AMPM "%I:%M:%S %p")
166 (define-simple-langinfo-mapping locale-era
167 ERA "")
168 (define-simple-langinfo-mapping locale-era-year
169 ERA_YEAR "")
170 (define-simple-langinfo-mapping locale-era-date+time-format
171 ERA_D_T_FMT "")
172 (define-simple-langinfo-mapping locale-era-date-format
173 ERA_D_FMT "")
174 (define-simple-langinfo-mapping locale-era-time-format
175 ERA_T_FMT "")
176
177
178 \f
179 ;;;
180 ;;; Monetary information.
181 ;;;
182
183 (define-macro (define-monetary-langinfo-mapping name local-item intl-item
184 default/local default/intl)
185 (let ((body
186 (let ((intl (if (and (provided? 'nl-langinfo) (defined? intl-item))
187 `(apply nl-langinfo ,intl-item locale)
188 default/intl))
189 (local (if (and (provided? 'nl-langinfo) (defined? local-item))
190 `(apply nl-langinfo ,local-item locale)
191 default/local)))
192 `(if intl? ,intl ,local))))
193
194 `(define (,name intl? . locale)
195 ,body)))
196
197 ;; FIXME: How can we use ALT_DIGITS?
198 (define-monetary-langinfo-mapping locale-currency-symbol
199 CRNCYSTR INT_CURR_SYMBOL
200 "-" "")
201 (define-monetary-langinfo-mapping locale-monetary-fractional-digits
202 FRAC_DIGITS INT_FRAC_DIGITS
203 2 2)
204
205 (define-simple-langinfo-mapping locale-monetary-positive-sign
206 POSITIVE_SIGN "+")
207 (define-simple-langinfo-mapping locale-monetary-negative-sign
208 NEGATIVE_SIGN "-")
209 (define-simple-langinfo-mapping locale-monetary-decimal-point
210 MON_DECIMAL_POINT "")
211 (define-simple-langinfo-mapping locale-monetary-thousands-separator
212 MON_THOUSANDS_SEP "")
213 (define-simple-langinfo-mapping locale-monetary-digit-grouping
214 MON_GROUPING '())
215
216 (define-monetary-langinfo-mapping locale-currency-symbol-precedes-positive?
217 P_CS_PRECEDES INT_P_CS_PRECEDES
218 #t #t)
219 (define-monetary-langinfo-mapping locale-currency-symbol-precedes-negative?
220 N_CS_PRECEDES INT_N_CS_PRECEDES
221 #t #t)
222
223
224 (define-monetary-langinfo-mapping locale-positive-separated-by-space?
225 ;; Whether a space should be inserted between a positive amount and the
226 ;; currency symbol.
227 P_SEP_BY_SPACE INT_P_SEP_BY_SPACE
228 #t #t)
229 (define-monetary-langinfo-mapping locale-negative-separated-by-space?
230 ;; Whether a space should be inserted between a negative amount and the
231 ;; currency symbol.
232 N_SEP_BY_SPACE INT_N_SEP_BY_SPACE
233 #t #t)
234
235 (define-monetary-langinfo-mapping locale-positive-sign-position
236 ;; Position of the positive sign wrt. currency symbol and quantity in a
237 ;; monetary amount.
238 P_SIGN_POSN INT_P_SIGN_POSN
239 'unspecified 'unspecified)
240 (define-monetary-langinfo-mapping locale-negative-sign-position
241 ;; Position of the negative sign wrt. currency symbol and quantity in a
242 ;; monetary amount.
243 N_SIGN_POSN INT_N_SIGN_POSN
244 'unspecified 'unspecified)
245
246
247 (define (%number-integer-part int grouping separator)
248 ;; Process INT (a string denoting a number's integer part) and return a new
249 ;; string with digit grouping and separators according to GROUPING (a list,
250 ;; potentially circular) and SEPARATOR (a string).
251
252 ;; Process INT from right to left.
253 (let loop ((int int)
254 (grouping grouping)
255 (result '()))
256 (cond ((string=? int "") (apply string-append result))
257 ((null? grouping) (apply string-append int result))
258 (else
259 (let* ((len (string-length int))
260 (cut (min (car grouping) len)))
261 (loop (substring int 0 (- len cut))
262 (cdr grouping)
263 (let ((sub (substring int (- len cut) len)))
264 (if (> len cut)
265 (cons* separator sub result)
266 (cons sub result)))))))))
267
268 (define (add-monetary-sign+currency amount figure intl? locale)
269 ;; Add a sign and currency symbol around FIGURE. FIGURE should be a
270 ;; formatted unsigned amount (a string) representing AMOUNT.
271 (let* ((positive? (> amount 0))
272 (sign
273 (cond ((> amount 0) (locale-monetary-positive-sign locale))
274 ((< amount 0) (locale-monetary-negative-sign locale))
275 (else "")))
276 (currency (locale-currency-symbol intl? locale))
277 (currency-precedes?
278 (if positive?
279 locale-currency-symbol-precedes-positive?
280 locale-currency-symbol-precedes-negative?))
281 (separated?
282 (if positive?
283 locale-positive-separated-by-space?
284 locale-negative-separated-by-space?))
285 (sign-position
286 (if positive?
287 locale-positive-sign-position
288 locale-negative-sign-position))
289 (currency-space
290 (if (separated? intl? locale) " " ""))
291 (append-currency
292 (lambda (amt)
293 (if (currency-precedes? intl? locale)
294 (string-append currency currency-space amt)
295 (string-append amt currency-space currency)))))
296
297 (case (sign-position intl? locale)
298 ((parenthesize)
299 (string-append "(" (append-currency figure) ")"))
300 ((sign-before)
301 (string-append sign (append-currency figure)))
302 ((sign-after unspecified)
303 ;; following glibc's recommendation for `unspecified'.
304 (if (currency-precedes? intl? locale)
305 (string-append currency currency-space sign figure)
306 (string-append figure currency-space currency sign)))
307 ((sign-before-currency-symbol)
308 (if (currency-precedes? intl? locale)
309 (string-append sign currency currency-space figure)
310 (string-append figure currency-space sign currency))) ;; unlikely
311 ((sign-after-currency-symbol)
312 (if (currency-precedes? intl? locale)
313 (string-append currency sign currency-space figure)
314 (string-append figure currency-space currency sign)))
315 (else
316 (error "unsupported sign position" (sign-position intl? locale))))))
317
318
319 (define* (monetary-amount->locale-string amount intl?
320 #:optional (locale %global-locale))
321 "Convert @var{amount} (an inexact) into a string according to the cultural
322 conventions of either @var{locale} (a locale object) or the current locale.
323 If @var{intl?} is true, then the international monetary format for the given
324 locale is used."
325
326 (let* ((fraction-digits
327 (or (locale-monetary-fractional-digits intl? locale) 2))
328 (decimal-part
329 (lambda (dec)
330 (if (or (string=? dec "") (eq? 0 fraction-digits))
331 ""
332 (string-append (locale-monetary-decimal-point locale)
333 (if (< fraction-digits (string-length dec))
334 (substring dec 0 fraction-digits)
335 dec)))))
336
337 (external-repr (number->string (if (> amount 0) amount (- amount))))
338 (int+dec (string-split external-repr #\.))
339 (int (car int+dec))
340 (dec (decimal-part (if (null? (cdr int+dec))
341 ""
342 (cadr int+dec))))
343 (grouping (locale-monetary-digit-grouping locale))
344 (separator (locale-monetary-thousands-separator locale)))
345
346 (add-monetary-sign+currency amount
347 (string-append
348 (%number-integer-part int grouping
349 separator)
350 dec)
351 intl? locale)))
352
353
354 \f
355 ;;;
356 ;;; Number formatting.
357 ;;;
358
359 (define-simple-langinfo-mapping locale-digit-grouping
360 GROUPING '())
361 (define-simple-langinfo-mapping locale-decimal-point
362 RADIXCHAR ".")
363 (define-simple-langinfo-mapping locale-thousands-separator
364 THOUSEP "")
365
366 (define* (number->locale-string number
367 #:optional (fraction-digits #t)
368 (locale %global-locale))
369 "Convert @var{number} (an inexact) into a string according to the cultural
370 conventions of either @var{locale} (a locale object) or the current locale.
371 Optionally, @var{fraction-digits} may be bound to an integer specifying the
372 number of fractional digits to be displayed."
373
374 (let* ((sign
375 (cond ((> number 0) "")
376 ((< number 0) "-")
377 (else "")))
378 (decimal-part
379 (lambda (dec)
380 (if (or (string=? dec "") (eq? 0 fraction-digits))
381 ""
382 (string-append (locale-decimal-point locale)
383 (if (and (integer? fraction-digits)
384 (< fraction-digits
385 (string-length dec)))
386 (substring dec 0 fraction-digits)
387 dec))))))
388
389 (let* ((external-repr (number->string (if (> number 0)
390 number
391 (- number))))
392 (int+dec (string-split external-repr #\.))
393 (int (car int+dec))
394 (dec (decimal-part (if (null? (cdr int+dec))
395 ""
396 (cadr int+dec))))
397 (grouping (locale-digit-grouping locale))
398 (separator (locale-thousands-separator locale)))
399
400 (string-append sign
401 (%number-integer-part int grouping separator)
402 dec))))
403
404 \f
405 ;;;
406 ;;; Miscellaneous.
407 ;;;
408
409 (define-simple-langinfo-mapping locale-yes-regexp
410 YESEXPR "^[yY]")
411 (define-simple-langinfo-mapping locale-no-regexp
412 NOEXPR "^[nN]")
413
414 ;; `YESSTR' and `NOSTR' are considered deprecated so we don't provide them.
415
416
417 ;;; Local Variables:
418 ;;; coding: latin-1
419 ;;; End:
420
421 ;;; i18n.scm ends here