1 ;;;; i18n.scm --- internationalization support -*- coding: utf-8 -*-
3 ;;;; Copyright (C) 2006, 2007, 2009, 2010, 2012 Free Software Foundation, Inc.
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 3 of the License, or (at your option) any later version.
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.
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
19 ;;; Author: Ludovic Courtès <ludo@gnu.org>
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.
31 (define-module (ice-9 i18n)
32 :use-module (ice-9 optargs)
33 :export (;; `locale' type
38 string-locale<? string-locale>?
39 string-locale-ci<? string-locale-ci>? string-locale-ci=?
41 char-locale<? char-locale>?
42 char-locale-ci<? char-locale-ci>? char-locale-ci=?
45 char-locale-downcase char-locale-upcase char-locale-titlecase
46 string-locale-downcase string-locale-upcase string-locale-titlecase
49 locale-string->integer locale-string->inexact
55 locale-day-short locale-day locale-month-short locale-month
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
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
78 locale-digit-grouping locale-decimal-point
79 locale-thousands-separator
83 locale-yes-regexp locale-no-regexp))
86 (eval-when (eval load compile)
87 (load-extension (string-append "libguile-" (effective-version))
95 (define (locale-encoding . locale)
96 (apply nl-langinfo CODESET locale))
103 ;; Helper macro: Define a procedure named NAME that maps its argument to
104 ;; NL-ITEMS (when `nl-langinfo' is provided).
105 (define-macro (define-vector-langinfo-mapping name nl-items)
106 (let* ((item-count (length nl-items))
107 (defines `(define %nl-items (vector #f ,@nl-items)))
108 (make-body (lambda (result)
109 `(if (and (integer? item) (exact? item))
110 (if (and (>= item 1) (<= item ,item-count))
112 (throw 'out-of-range "out of range" item))
113 (throw 'wrong-type-arg "wrong argument type" item)))))
114 `(define (,name item . locale)
116 ,(make-body '(apply nl-langinfo (vector-ref %nl-items item) locale)))))
119 (define-vector-langinfo-mapping locale-day-short
120 (ABDAY_1 ABDAY_2 ABDAY_3 ABDAY_4 ABDAY_5 ABDAY_6 ABDAY_7))
122 (define-vector-langinfo-mapping locale-day
123 (DAY_1 DAY_2 DAY_3 DAY_4 DAY_5 DAY_6 DAY_7))
125 (define-vector-langinfo-mapping locale-month-short
126 (ABMON_1 ABMON_2 ABMON_3 ABMON_4 ABMON_5 ABMON_6
127 ABMON_7 ABMON_8 ABMON_9 ABMON_10 ABMON_11 ABMON_12))
129 (define-vector-langinfo-mapping locale-month
130 (MON_1 MON_2 MON_3 MON_4 MON_5 MON_6 MON_7 MON_8 MON_9 MON_10 MON_11 MON_12))
138 ;; Define a procedure NAME that gets langinfo item ITEM. Gnulib's
139 ;; `nl_langinfo' guarantees that all these items are supported.
140 (define-syntax-rule (define-simple-langinfo-mapping name item)
141 (define* (name #:optional (locale %global-locale))
142 (nl-langinfo item locale)))
144 (define-simple-langinfo-mapping locale-am-string
146 (define-simple-langinfo-mapping locale-pm-string
148 (define-simple-langinfo-mapping locale-date+time-format
150 (define-simple-langinfo-mapping locale-date-format
152 (define-simple-langinfo-mapping locale-time-format
154 (define-simple-langinfo-mapping locale-time+am/pm-format
156 (define-simple-langinfo-mapping locale-era
158 (define-simple-langinfo-mapping locale-era-year
160 (define-simple-langinfo-mapping locale-era-date+time-format
162 (define-simple-langinfo-mapping locale-era-date-format
164 (define-simple-langinfo-mapping locale-era-time-format
170 ;;; Monetary information.
173 ;; Define a procedure NAME that gets item LOCAL-ITEM or INTL-ITEM,
174 ;; depending on whether the caller asked for the international version
175 ;; or not. Since Gnulib's `nl_langinfo' module doesn't guarantee that
176 ;; all these items are available, use DEFAULT/LOCAL and DEFAULT/INTL as
177 ;; default values when the system does not support them.
178 (define-macro (define-monetary-langinfo-mapping name local-item intl-item
179 default/local default/intl)
181 (let ((intl (if (defined? intl-item)
182 `(apply nl-langinfo ,intl-item locale)
184 (local (if (defined? local-item)
185 `(apply nl-langinfo ,local-item locale)
187 `(if intl? ,intl ,local))))
189 `(define (,name intl? . locale)
192 ;; FIXME: How can we use ALT_DIGITS?
193 (define-monetary-langinfo-mapping locale-currency-symbol
194 CRNCYSTR INT_CURR_SYMBOL
196 (define-monetary-langinfo-mapping locale-monetary-fractional-digits
197 FRAC_DIGITS INT_FRAC_DIGITS
200 (define-simple-langinfo-mapping locale-monetary-positive-sign
202 (define-simple-langinfo-mapping locale-monetary-negative-sign
204 (define-simple-langinfo-mapping locale-monetary-decimal-point
206 (define-simple-langinfo-mapping locale-monetary-thousands-separator
208 (define-simple-langinfo-mapping locale-monetary-digit-grouping
211 (define-monetary-langinfo-mapping locale-currency-symbol-precedes-positive?
212 P_CS_PRECEDES INT_P_CS_PRECEDES
214 (define-monetary-langinfo-mapping locale-currency-symbol-precedes-negative?
215 N_CS_PRECEDES INT_N_CS_PRECEDES
219 (define-monetary-langinfo-mapping locale-positive-separated-by-space?
220 ;; Whether a space should be inserted between a positive amount and the
222 P_SEP_BY_SPACE INT_P_SEP_BY_SPACE
224 (define-monetary-langinfo-mapping locale-negative-separated-by-space?
225 ;; Whether a space should be inserted between a negative amount and the
227 N_SEP_BY_SPACE INT_N_SEP_BY_SPACE
230 (define-monetary-langinfo-mapping locale-positive-sign-position
231 ;; Position of the positive sign wrt. currency symbol and quantity in a
233 P_SIGN_POSN INT_P_SIGN_POSN
234 'unspecified 'unspecified)
235 (define-monetary-langinfo-mapping locale-negative-sign-position
236 ;; Position of the negative sign wrt. currency symbol and quantity in a
238 N_SIGN_POSN INT_N_SIGN_POSN
239 'unspecified 'unspecified)
242 (define (%number-integer-part int grouping separator)
243 ;; Process INT (a string denoting a number's integer part) and return a new
244 ;; string with digit grouping and separators according to GROUPING (a list,
245 ;; potentially circular) and SEPARATOR (a string).
247 ;; Process INT from right to left.
251 (cond ((string=? int "") (apply string-append result))
252 ((null? grouping) (apply string-append int result))
254 (let* ((len (string-length int))
255 (cut (min (car grouping) len)))
256 (loop (substring int 0 (- len cut))
258 (let ((sub (substring int (- len cut) len)))
260 (cons* separator sub result)
261 (cons sub result)))))))))
263 (define (add-monetary-sign+currency amount figure intl? locale)
264 ;; Add a sign and currency symbol around FIGURE. FIGURE should be a
265 ;; formatted unsigned amount (a string) representing AMOUNT.
266 (let* ((positive? (> amount 0))
268 (cond ((> amount 0) (locale-monetary-positive-sign locale))
269 ((< amount 0) (locale-monetary-negative-sign locale))
271 (currency (locale-currency-symbol intl? locale))
274 locale-currency-symbol-precedes-positive?
275 locale-currency-symbol-precedes-negative?))
278 locale-positive-separated-by-space?
279 locale-negative-separated-by-space?))
282 locale-positive-sign-position
283 locale-negative-sign-position))
285 (if (separated? intl? locale) " " ""))
288 (if (currency-precedes? intl? locale)
289 (string-append currency currency-space amt)
290 (string-append amt currency-space currency)))))
292 (case (sign-position intl? locale)
294 (string-append "(" (append-currency figure) ")"))
296 (string-append sign (append-currency figure)))
297 ((sign-after unspecified)
298 ;; following glibc's recommendation for `unspecified'.
299 (if (currency-precedes? intl? locale)
300 (string-append currency currency-space sign figure)
301 (string-append figure currency-space currency sign)))
302 ((sign-before-currency-symbol)
303 (if (currency-precedes? intl? locale)
304 (string-append sign currency currency-space figure)
305 (string-append figure currency-space sign currency))) ;; unlikely
306 ((sign-after-currency-symbol)
307 (if (currency-precedes? intl? locale)
308 (string-append currency sign currency-space figure)
309 (string-append figure currency-space currency sign)))
311 (error "unsupported sign position" (sign-position intl? locale))))))
314 (define* (monetary-amount->locale-string amount intl?
315 #:optional (locale %global-locale))
316 "Convert @var{amount} (an inexact) into a string according to the cultural
317 conventions of either @var{locale} (a locale object) or the current locale.
318 If @var{intl?} is true, then the international monetary format for the given
321 (let* ((fraction-digits
322 (or (locale-monetary-fractional-digits intl? locale) 2))
325 (if (or (string=? dec "") (eq? 0 fraction-digits))
327 (string-append (locale-monetary-decimal-point locale)
328 (if (< fraction-digits (string-length dec))
329 (substring dec 0 fraction-digits)
332 (external-repr (number->string (if (> amount 0) amount (- amount))))
333 (int+dec (string-split external-repr #\.))
335 (dec (decimal-part (if (null? (cdr int+dec))
338 (grouping (locale-monetary-digit-grouping locale))
339 (separator (locale-monetary-thousands-separator locale)))
341 (add-monetary-sign+currency amount
343 (%number-integer-part int grouping
351 ;;; Number formatting.
354 (define-simple-langinfo-mapping locale-digit-grouping
356 (define-simple-langinfo-mapping locale-decimal-point
358 (define-simple-langinfo-mapping locale-thousands-separator
361 (define* (number->locale-string number
362 #:optional (fraction-digits #t)
363 (locale %global-locale))
364 "Convert @var{number} (an inexact) into a string according to the cultural
365 conventions of either @var{locale} (a locale object) or the current locale.
366 Optionally, @var{fraction-digits} may be bound to an integer specifying the
367 number of fractional digits to be displayed."
370 (cond ((> number 0) "")
375 (if (or (string=? dec "") (eq? 0 fraction-digits))
377 (string-append (locale-decimal-point locale)
378 (if (and (integer? fraction-digits)
380 (string-length dec)))
381 (substring dec 0 fraction-digits)
384 (let* ((external-repr (number->string (if (> number 0)
387 (int+dec (string-split external-repr #\.))
389 (dec (decimal-part (if (null? (cdr int+dec))
392 (grouping (locale-digit-grouping locale))
393 (separator (locale-thousands-separator locale)))
396 (%number-integer-part int grouping separator)
404 (define-simple-langinfo-mapping locale-yes-regexp
406 (define-simple-langinfo-mapping locale-no-regexp
409 ;; `YESSTR' and `NOSTR' are considered deprecated so we don't provide them.
411 ;;; i18n.scm ends here