Debian Codegraph @

%3a29/localization.sls

raw
1 (library (srfi :29 localization)
2 (export current-language current-country current-locale-details
3 declare-bundle! store-bundle store-bundle! load-bundle!
4 localized-template)
5 (import (rnrs) (srfi :6))
6
7 (define (current-locale-details . args)
8 (error 'current-locale-details
9 "procedure not supplied by reference implementation"))
10
11 (define-syntax store-bundle (identifier-syntax store-bundle!))
12
13 ;; reference implementation taken from srfi :29 documenation
14
15 ;; Copyright (C) Scott G. Miller (2002). All Rights Reserved.
16 ;;
17 ;; This document and translations of it may be copied and furnished to
18 ;; others, and derivative works that comment on or otherwise explain it or
19 ;; assist in its implementation may be prepared, copied, published and
20 ;; distributed, in whole or in part, without restriction of any kind,
21 ;; provided that the above copyright notice and this paragraph are included
22 ;; on all such copies and derivative works. However, this document itself may
23 ;; not be modified in any way, such as by removing the copyright notice or
24 ;; references to the Scheme Request For Implementation process or editors,
25 ;; except as needed for the purpose of developing SRFIs in which case the
26 ;; procedures for copyrights defined in the SRFI process must be followed, or
27 ;; as required to translate it into languages other than English.
28 ;;
29 ;; The limited permissions granted above are perpetual and will not be
30 ;; revoked by the authors or their successors or assigns.
31 ;;
32 ;; This document and the information contained herein is provided on an "AS
33 ;; IS" basis and THE AUTHOR AND THE SRFI EDITORS DISCLAIM ALL WARRANTIES,
34 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE
35 ;; OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED
36 ;; WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
37 ;;
38
39 ;; The association list in which bundles will be stored
40 (define *localization-bundles* '())
41
42 ;; The current-language and current-country functions provided
43 ;; here must be rewritten for each Scheme system to default to the
44 ;; actual locale of the session
45 (define current-language
46 (let ((current-language-value 'en))
47 (lambda args
48 (if (null? args)
49 current-language-value
50 (set! current-language-value (car args))))))
51
52 (define current-country
53 (let ((current-country-value 'us))
54 (lambda args
55 (if (null? args)
56 current-country-value
57 (set! current-country-value (car args))))))
58
59 ;; The load-bundle! and store-bundle! both return #f in this
60 ;; reference implementation. A compliant implementation need
61 ;; not rewrite these procedures.
62 (define load-bundle!
63 (lambda (bundle-specifier)
64 #f))
65
66 (define store-bundle!
67 (lambda (bundle-specifier)
68 #f))
69
70 ;; Declare a bundle of templates with a given bundle specifier
71 (define declare-bundle!
72 (letrec ((remove-old-bundle
73 (lambda (specifier bundle)
74 (cond ((null? bundle) '())
75 ((equal? (caar bundle) specifier)
76 (cdr bundle))
77 (else (cons (car bundle)
78 (remove-old-bundle specifier
79 (cdr bundle))))))))
80 (lambda (bundle-specifier bundle-assoc-list)
81 (set! *localization-bundles*
82 (cons (cons bundle-specifier bundle-assoc-list)
83 (remove-old-bundle bundle-specifier
84 *localization-bundles*))))))
85
86 ;;Retrieve a localized template given its package name and a template name
87 (define localized-template
88 (letrec ((rdc
89 (lambda (ls)
90 (if (null? (cdr ls))
91 '()
92 (cons (car ls) (rdc (cdr ls))))))
93 (find-bundle
94 (lambda (specifier template-name)
95 (cond ((assoc specifier *localization-bundles*) =>
96 (lambda (bundle) bundle))
97 ((null? specifier) #f)
98 (else (find-bundle (rdc specifier)
99 template-name))))))
100 (lambda (package-name template-name)
101 (let loop ((specifier (cons package-name
102 (list (current-language)
103 (current-country)))))
104 (and (not (null? specifier))
105 (let ((bundle (find-bundle specifier template-name)))
106 (and bundle
107 (cond ((assq template-name bundle) => cdr)
108 ((null? (cdr specifier)) #f)
109 (else (loop (rdc specifier)))))))))))
110
111 ;;An SRFI-28 and SRFI-29 compliant version of format. It requires
112 ;;SRFI-23 for error reporting.
113 (define format
114 (lambda (format-string . objects)
115 (let ((buffer (open-output-string)))
116 (let loop ((format-list (string->list format-string))
117 (objects objects)
118 (object-override #f))
119 (cond ((null? format-list) (get-output-string buffer))
120 ((char=? (car format-list) #\~)
121 (cond ((null? (cdr format-list))
122 (error 'format "Incomplete escape sequence"))
123 ((char-numeric? (cadr format-list))
124 (let posloop ((fl (cddr format-list))
125 (pos (string->number
126 (string (cadr format-list)))))
127 (cond ((null? fl)
128 (error 'format "Incomplete escape sequence"))
129 ((and (eq? (car fl) '#\@)
130 (null? (cdr fl)))
131 (error 'format "Incomplete escape sequence"))
132 ((and (eq? (car fl) '#\@)
133 (eq? (cadr fl) '#\*))
134 (loop (cddr fl) objects (list-ref objects pos)))
135 (else
136 (posloop (cdr fl)
137 (+ (* 10 pos)
138 (string->number
139 (string (car fl)))))))))
140 (else
141 (case (cadr format-list)
142 ((#\a)
143 (cond (object-override
144 (begin
145 (display object-override buffer)
146 (loop (cddr format-list) objects #f)))
147 ((null? objects)
148 (error 'format "No value for escape sequence"))
149 (else
150 (begin
151 (display (car objects) buffer)
152 (loop (cddr format-list)
153 (cdr objects) #f)))))
154 ((#\s)
155 (cond (object-override
156 (begin
157 (display object-override buffer)
158 (loop (cddr format-list) objects #f)))
159 ((null? objects)
160 (error 'format "No value for escape sequence"))
161 (else
162 (begin
163 (write (car objects) buffer)
164 (loop (cddr format-list)
165 (cdr objects) #f)))))
166 ((#\%)
167 (if object-override
168 (error 'format "Escape sequence following positional override does not require a value"))
169 (display #\newline buffer)
170 (loop (cddr format-list) objects #f))
171 ((#\~)
172 (if object-override
173 (error 'format "Escape sequence following positional override does not require a value"))
174 (display #\~ buffer)
175 (loop (cddr format-list) objects #f))
176 (else
177 (error 'format "Unrecognized escape sequence"))))))
178 (else (display (car format-list) buffer)
179 (loop (cdr format-list) objects #f)))))))
180 )