| 1 |
;; text.gettext -- gettext superset implemented in Scheme |
| 2 |
;; |
| 3 |
;; Copyright (c) 2003 by Alex Shinn <[email protected]> |
| 4 |
;; BSD-style license. See the file COPYING for details. |
| 5 |
|
| 6 |
;; This is *not* gettext, nor does it use the C gettext library. |
| 7 |
;; |
| 8 |
;; This is a full gettext superset written in pure Scheme from reading |
| 9 |
;; the gettext documentation - I have never looked at the gettext source |
| 10 |
;; code, so this may be used under a more liberal BSD-style license as |
| 11 |
;; above. |
| 12 |
;; |
| 13 |
;; This library includes various extensions, including the ability to |
| 14 |
;; support multiple domains, locales and search paths; the ability to |
| 15 |
;; read both .po and .mo files directly as message catalogs; and a more |
| 16 |
;; Schemeish dispatch interface. |
| 17 |
;; |
| 18 |
;; The multiple domain interface is useful because it allows multiple |
| 19 |
;; applications to share message catalogs while still extending their |
| 20 |
;; own messages. Many applications use many of the same messages, such |
| 21 |
;; as those for menu names, and these messages can easily be leveraged |
| 22 |
;; in Scheme as follows: |
| 23 |
;; |
| 24 |
;; (textdomain '("myapp" "gimp")) ; search 1st myapp, then gimp |
| 25 |
;; (gettext "/File/Close") ; "Close" from gimp unless overridden |
| 26 |
;; |
| 27 |
;; Multiple locales can be useful while translations are still in |
| 28 |
;; progress. It is not fair to assume that English (or whatever the |
| 29 |
;; native source uses) is the best alternative for a message that has |
| 30 |
;; not yet been translated, so the locale may also be a list: |
| 31 |
;; |
| 32 |
;; (textdomain "myapp" '("ru" "uk")) ; search 1st Russian then Ukranian, |
| 33 |
;; (gettext "Hello, World!") ; which are somewhat similar |
| 34 |
;; |
| 35 |
;; Note in both cases the domain and locale may be either a single |
| 36 |
;; string (as in the C gettext) or a list of strings in order of |
| 37 |
;; decreasing priority. Also TEXTDOMAIN takes locale as an optional 2nd |
| 38 |
;; parameter (to override the Unix environment variable), and in fact |
| 39 |
;; the full parameter list is as follows: |
| 40 |
;; |
| 41 |
;; (textdomain domain [locale] [dirs] [cdir] [cached?] [lookup-cached?]) |
| 42 |
;; |
| 43 |
;; DOMAIN is a string or list of strings specifying the domain (name of |
| 44 |
;; .mo or .po files) as in C gettext. |
| 45 |
;; |
| 46 |
;; LOCALE is a string or list of strings in the standard Unix format of |
| 47 |
;; LANG[_REGION][.ENCODING] |
| 48 |
;; |
| 49 |
;; DIRS is the search path of directories which should hold the |
| 50 |
;; LOCALE/CDIR/ directories which contain the actual message catalogs. |
| 51 |
;; This is always appended with the system default, e.g. |
| 52 |
;; "/usr/share/locale", and may also inherit from the GETTEXT_PATH |
| 53 |
;; colon-delimited environment variable. |
| 54 |
;; |
| 55 |
;; CDIR is the catagory directory, defaulting to either the LC_CATEGORY |
| 56 |
;; environment variable or the appropriate system default |
| 57 |
;; (e.g. LC_MESSAGES). You generally won't need this. |
| 58 |
;; |
| 59 |
;; CACHED? means to cache individual messages, and defaults to #t. |
| 60 |
;; |
| 61 |
;; LOOKUP-CACHED? means to cache the lookup dispatch generated by these |
| 62 |
;; parameters, and defaults to #t. |
| 63 |
;; |
| 64 |
;; TEXTDOMAIN just passes these parameters to the internal MAKE-GETTEXT, |
| 65 |
;; and binds the result to the global dispatch used by GETTEXT. You may |
| 66 |
;; build these closures manually for convenience in using multiple |
| 67 |
;; separate domains or locales at once (useful for server environments): |
| 68 |
;; |
| 69 |
;; (define my-gettext (make-gettext "myapp")) |
| 70 |
;; (define _ (my-gettext 'getter)) |
| 71 |
;; (_ "Hello, World!") |
| 72 |
|
| 73 |
(define-module text.gettext |
| 74 |
(use srfi.13) ;; string library |
| 75 |
(use rfc.822) ;; message headers parsing (same syntax for .po meta-data) |
| 76 |
(use file.util) ;; file-is-readable? |
| 77 |
(use binary.io) ;; unpacking .mo files |
| 78 |
(use gauche.charconv) ;; :encoding on i/o procedures |
| 79 |
(use util.combinations) ;; cartesian-product for file lists |
| 80 |
(export |
| 81 |
;;; standard gettext interface |
| 82 |
gettext textdomain dgettext dcgettext bindtextdomain |
| 83 |
ngettext ;;dngettext dcngettext |
| 84 |
;;; more flexible interface for building lookups |
| 85 |
make-gettext)) |
| 86 |
(select-module text.gettext) |
| 87 |
|
| 88 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 89 |
;; Customize this to the appropriate value for your system: |
| 90 |
|
| 91 |
(define message-path '("/usr/share/locale")) |
| 92 |
|
| 93 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 94 |
;; store meta info for gettext files |
| 95 |
|
| 96 |
(define-class <gettext-file> () |
| 97 |
((filename :init-keyword :filename :initform #f :accessor filename-of) |
| 98 |
(locale :init-keyword :locale :initform #f :accessor locale-of) |
| 99 |
(encoding :init-keyword :encoding :initform #f :accessor encoding-of) |
| 100 |
(properties :init-keyword :properties :initform #f :accessor properties-of) |
| 101 |
(type :init-keyword :type :initform #f :accessor type-of) |
| 102 |
(plural-index :init-keyword :plural-index :initform #f :accessor plural-index-of) |
| 103 |
)) |
| 104 |
|
| 105 |
(define (make-gettext-file filename locale) |
| 106 |
(make <gettext-file> :filename filename :locale locale)) |
| 107 |
|
| 108 |
(define (gettext-file-guess-encoding gfile) |
| 109 |
(rxmatch-case (locale-of gfile) |
| 110 |
;; explicit encoding used in locale name |
| 111 |
(#/\.(.*)$/ (#f encoding) encoding) |
| 112 |
;; no encoding, but Japanese so we can auto-detect |
| 113 |
(#/^ja.*/ (#f) "*JP") |
| 114 |
;; otherwise use internal encoding (maybe default to utf-8?) |
| 115 |
(else (gauche-character-encoding)))) |
| 116 |
|
| 117 |
(define (gettext-file-update-properties! f) |
| 118 |
(let ([filename (filename-of f)] |
| 119 |
[encoding (gettext-file-guess-encoding f)] |
| 120 |
[properties '()]) |
| 121 |
(set! (type-of f) (if (string-suffix? ".mo" filename) 'mo 'po)) |
| 122 |
;;(warn "gettext: file: ~S guess-encoding: ~S" filename encoding) |
| 123 |
(when (file-is-readable? filename) |
| 124 |
(and-let* ([property-msg (lookup-message f "" #f encoding)]) |
| 125 |
(set! properties |
| 126 |
(call-with-input-string property-msg rfc822-header->list)) |
| 127 |
(and-let* ([type-ls (assoc "content-type" properties)] |
| 128 |
[type (cadr type-ls)] |
| 129 |
[m (#/charset=([^\s]+)/ type)]) |
| 130 |
;;(warn "gettext: found encoding: ~S" (rxmatch-substring m 1)) |
| 131 |
(set! encoding (rxmatch-substring m 1))) |
| 132 |
;;(warn "gettext: file: ~S encoding: ~S" filename encoding) |
| 133 |
(set! (encoding-of f) encoding))) |
| 134 |
;;(warn "gettext: properties: ~S" properties) |
| 135 |
(set! (properties-of f) properties))) |
| 136 |
|
| 137 |
(define (get-plural-index! gfile) |
| 138 |
(or |
| 139 |
(and-let* ([properties (properties-of gfile)] |
| 140 |
[plural-forms0 (assoc-ref properties "plural-forms")] |
| 141 |
[plural-forms (if (pair? plural-forms0) |
| 142 |
(string-join plural-forms0) |
| 143 |
plural-forms0)] |
| 144 |
[m (#/\bplural=([^\;]*)\;/i plural-forms)] |
| 145 |
[f (C->Scheme (m 1))]) |
| 146 |
;;(warn "gettext: found plural-form: ~S" (m 1)) |
| 147 |
(set! (plural-index-of gfile) f) |
| 148 |
f) |
| 149 |
(^n 0))) |
| 150 |
|
| 151 |
;; take a list or a single argument which is interpretted as a one |
| 152 |
;; element list |
| 153 |
(define (listify arg) |
| 154 |
(if (or (pair? arg) (null? arg)) arg (list arg))) |
| 155 |
|
| 156 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 157 |
;; the default gettext lookup |
| 158 |
|
| 159 |
(define domain-message-paths (make-hash-table 'equal?)) |
| 160 |
|
| 161 |
(define default-accessor #f) |
| 162 |
|
| 163 |
(define (gettext msgid) |
| 164 |
(default-accessor 'get msgid)) |
| 165 |
(define (dgettext domain msgid) |
| 166 |
((make-gettext domain) 'get msgid)) |
| 167 |
(define (dcgettext domain msgid locale) |
| 168 |
((make-gettext domain (list locale)) 'get msgid)) |
| 169 |
|
| 170 |
;; plural forms |
| 171 |
(define (ngettext . opt) |
| 172 |
(apply default-accessor 'nget opt)) |
| 173 |
(define (ndgettext domain . opt) |
| 174 |
(apply (make-gettext domain) 'nget opt)) |
| 175 |
(define (ndcgettext domain msgid locale . opt) |
| 176 |
(apply (make-gettext domain (list locale)) 'nget msgid opt)) |
| 177 |
|
| 178 |
;; bind the default domain |
| 179 |
(define (textdomain domain . opt) |
| 180 |
(rlet1 accessor (apply make-gettext domain opt) |
| 181 |
(set! default-accessor accessor))) |
| 182 |
|
| 183 |
(define (bindtextdomain domain dirs) |
| 184 |
(hash-table-put! domain-message-paths domain (listify dirs))) |
| 185 |
|
| 186 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 187 |
;; the gettext .po parser |
| 188 |
|
| 189 |
(define (lookup-po-message file msg msg2 encoding) |
| 190 |
;; resisting jokes about indigent messages... |
| 191 |
|
| 192 |
;; grab the 2nd scheme object in a string |
| 193 |
(define (tail-str str) |
| 194 |
(with-input-from-string str (lambda () (read) (read)))) |
| 195 |
|
| 196 |
;; read a sequence of lines in "" starting w/ an initial string. |
| 197 |
;; doesn't affect trailing lines. |
| 198 |
(define (read-str str) |
| 199 |
(let reader ([res (list str)]) |
| 200 |
(or (and-let* ([ch (peek-char)] |
| 201 |
[(not (eof-object? ch))] |
| 202 |
[(eqv? ch #\")] |
| 203 |
[line (read-line)] |
| 204 |
[(#/^\s*".*"/ line)]) |
| 205 |
(reader (cons (call-with-input-string line read) res))) |
| 206 |
(string-concatenate-reverse res)))) |
| 207 |
|
| 208 |
(define (read-plural default) |
| 209 |
(let reader ([res (list default)]) |
| 210 |
(cond [(and-let* ([(eqv? (peek-char) #\m)] |
| 211 |
[line (read-line)]) |
| 212 |
(#/^msgstr\[(\d+)\]\s+(.*)/i line)) |
| 213 |
=> (^m (reader (cons (cons (string->number (m 1)) |
| 214 |
(read-str (call-with-input-string |
| 215 |
(m 2) read))) |
| 216 |
res)))] |
| 217 |
[else (reverse res)]))) |
| 218 |
|
| 219 |
;; read from the file if it exists |
| 220 |
;;(warn "(lookup-po-message ~S ~S ~S)" file msg encoding) |
| 221 |
(and |
| 222 |
(file-is-readable? file) |
| 223 |
(guard (err [else (warn "error reading from file ~S: ~S" file err) #f]) |
| 224 |
(with-input-from-file file |
| 225 |
(^[] |
| 226 |
;;(warn "reading: ~S" file) |
| 227 |
(let loop ([line (read-line)]) |
| 228 |
(cond [(eof-object? line) #f] |
| 229 |
[(string-prefix? "msgid " line) |
| 230 |
(let1 msgid (read-str (tail-str line)) |
| 231 |
;;(warn "msgid: ~S\n" msgid) |
| 232 |
(cond [(string=? msgid msg) |
| 233 |
(let loop2 ([line (read-line)]) |
| 234 |
(cond [(eof-object? line) #f] |
| 235 |
[(string-prefix? "msgid_plural " line) |
| 236 |
(read-plural (read-str (tail-str line)))] |
| 237 |
[(string-prefix? "msgstr " line) |
| 238 |
(read-str (tail-str line))] |
| 239 |
[else (loop2 (read-line))]))] |
| 240 |
[else (loop (read-line))]))] |
| 241 |
[else (loop (read-line))]))) |
| 242 |
:encoding encoding)))) |
| 243 |
|
| 244 |
(define (lookup-mo-message file msg msg2 encoding) |
| 245 |
;;(warn "(lookup-mo-message ~S ~S ~S)" file msg encoding) |
| 246 |
(and |
| 247 |
(file-is-readable? file) |
| 248 |
(guard (err [else (warn "error reading from file ~S: ~S" file err) #f]) |
| 249 |
(with-input-from-file file |
| 250 |
(^[] |
| 251 |
(define (search read-int) |
| 252 |
(let* ([key (if msg2 (string-append msg "\0" msg2) msg)] |
| 253 |
[format (read-int)] |
| 254 |
[count (read-int)] |
| 255 |
[src-offset (read-int)] |
| 256 |
[trans-offset (read-int)] |
| 257 |
[hash-size (read-int)] |
| 258 |
[hash-offset (read-int)] |
| 259 |
[diff (- trans-offset src-offset)] |
| 260 |
[end (+ src-offset (* (- count 1) 8))]) |
| 261 |
(define (string-at pos) |
| 262 |
(port-seek (current-input-port) pos) |
| 263 |
(let* ([len (read-int)] |
| 264 |
[off (read-int)]) |
| 265 |
(port-seek (current-input-port) off) |
| 266 |
;;(warn "string-at: pos: ~S => len: ~S off: ~S" pos len off) |
| 267 |
(ces-convert (read-block len) encoding))) |
| 268 |
;;(warn "search ~S ~S ~S ~S ~S" format count src-offset trans-offset end) |
| 269 |
(cond ;; check endpoints |
| 270 |
[(string=? key (string-at src-offset)) |
| 271 |
(string-at (+ src-offset diff))] |
| 272 |
[(and (> end src-offset) (string=? key (string-at end))) |
| 273 |
(string-at (+ end diff))] |
| 274 |
(else ;; binary search |
| 275 |
(let loop ([lo 0] [hi (- count 1)]) |
| 276 |
;;(warn "(loop ~S ~S)" lo hi) |
| 277 |
(if (>= lo hi) |
| 278 |
#f |
| 279 |
(let* ([mid (+ lo (quotient (- hi lo) 2))] |
| 280 |
[pos (+ src-offset (* mid 8))] |
| 281 |
[str (string-at pos)]) |
| 282 |
(cond [(string<? key str) |
| 283 |
(if (>= mid hi) #f (loop lo mid))] |
| 284 |
[(string>? key str) |
| 285 |
(if (<= mid lo) #f (loop mid hi))] |
| 286 |
[else ;; match |
| 287 |
(string-at (+ pos diff))])))))))) |
| 288 |
(let1 magic (read-binary-uint32 (current-input-port) 'big-endian) |
| 289 |
(case magic |
| 290 |
[(#xde120495) (search (cut read-binary-uint32 #f 'little-endian))] |
| 291 |
[(#x950412de) (search (cut read-binary-uint32 #f 'big-endian))] |
| 292 |
[else (warn "invalid magic: ~S" magic) #f]))))))) |
| 293 |
|
| 294 |
(define (lookup-message gfile msg msg2 :optional (encoding (encoding-of gfile))) |
| 295 |
((if (eq? (type-of gfile) 'mo) lookup-mo-message lookup-po-message) |
| 296 |
(filename-of gfile) msg msg2 encoding)) |
| 297 |
|
| 298 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 299 |
;; the subset C parser for ngettext plural forms |
| 300 |
|
| 301 |
(define (C->Scheme str) |
| 302 |
(define (read-number c) |
| 303 |
(let loop ([ls (list c)]) |
| 304 |
(let1 c2 (peek-char) |
| 305 |
(cond [(and (not (eof-object? c2)) (char-numeric? c2)) |
| 306 |
(read-char) (loop (cons c2 ls))] |
| 307 |
[else (string->number (list->string (reverse ls)))])))) |
| 308 |
(define (read-comment) |
| 309 |
(read-char) |
| 310 |
(let loop ([c (read-char)]) |
| 311 |
(if (eof-object? c) |
| 312 |
c ;; maybe signal error |
| 313 |
(if (eqv? c #\*) |
| 314 |
(let1 c2 (read-char) |
| 315 |
(if (eqv? c2 #\/) #f (loop c2))) |
| 316 |
(loop (read-char)))))) |
| 317 |
(define (next-token) |
| 318 |
(let1 c (read-char) |
| 319 |
(if (eof-object? c) |
| 320 |
c |
| 321 |
(case c |
| 322 |
[(#\() 'open] |
| 323 |
[(#\)) 'close] |
| 324 |
[(#\/) (if (eqv? (peek-char) #\*) (read-comment) '/)] |
| 325 |
[(#\- #\+ #\* #\% #\? #\:) |
| 326 |
(string->symbol (string c))] |
| 327 |
[(#\&) (if (eqv? (peek-char) c) (begin (read-char) 'and) 'logand)] |
| 328 |
[(#\|) (if (eqv? (peek-char) c) (begin (read-char) 'or) 'logior)] |
| 329 |
[(#\! #\> #\<) |
| 330 |
(cond [(eqv? (peek-char) #\=) |
| 331 |
(read-char) (string->symbol (string c #\=))] |
| 332 |
[else (string->symbol (string c))])] |
| 333 |
[(#\=) |
| 334 |
(cond [(eqv? (peek-char) #\=) (read-char) '==] |
| 335 |
[else (warn "invalid assignment in C code") #f])] |
| 336 |
[(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9) |
| 337 |
(read-number c)] |
| 338 |
[(#\n) 'n] |
| 339 |
[(#\space #\newline) (next-token)] |
| 340 |
[else (warn "invalid character in C code: ~S" c) #f])))) |
| 341 |
(define (C-parse str) |
| 342 |
(define (precedence x) ;; lower value is higher precedence |
| 343 |
(case x |
| 344 |
[(**) 10] [(&) 70] |
| 345 |
[(! ~) 20] [(^ logand logior) 80] |
| 346 |
[(* / %) 30] [(and) 90] |
| 347 |
[(+ -) 40] [(or) 100] |
| 348 |
[(< > <= >=) 50] [(?) 110] |
| 349 |
[(== != <=>) 60] [else 120])) |
| 350 |
(define (parse1) |
| 351 |
(let1 x (next-token) |
| 352 |
(cond [(not x) (parse1)] |
| 353 |
[(eof-object? x) 'eof] |
| 354 |
[(eq? x 'open) (parse-until 'close)] |
| 355 |
[(memq x '(! ~)) `(,x ,(parse1))] |
| 356 |
[else x]))) |
| 357 |
(define (parse-until end) |
| 358 |
(define (group op left right) |
| 359 |
(cond [(or (eq? right end) (eq? right 'eof)) |
| 360 |
(warn "expected 2nd argument to: ~S" op) |
| 361 |
`(op ,left)] |
| 362 |
[(eq? op 'and) `(if (zero? ,left) 0 ,right)] |
| 363 |
[(eq? op 'or) `(if (zero? ,left) ,right 1)] |
| 364 |
[else `(,op ,left ,right)])) |
| 365 |
(define (join x stack) |
| 366 |
(if (null? stack) |
| 367 |
x |
| 368 |
(join (group (car stack) (cadr stack) x) (cddr stack)))) |
| 369 |
(let1 init (parse1) |
| 370 |
(if (equal? init end) |
| 371 |
'() |
| 372 |
(let loop ([left init] [op (parse1)] [stack '()]) |
| 373 |
(cond |
| 374 |
[(eq? op end) (join left stack)] |
| 375 |
[(eq? op 'eof) |
| 376 |
(warn "unexpected #<eof>") |
| 377 |
(join left stack)] |
| 378 |
[(eq? op '?) ;; trinary ? : (right-assoc) |
| 379 |
(let* ([pass (parse-until '|:|)] |
| 380 |
[fail (parse1)] |
| 381 |
[op2 (parse1)]) |
| 382 |
(cond [(or (eq? op2 end) (eq? op2 'eof)) |
| 383 |
`(if (zero? ,left) ,fail ,pass)] |
| 384 |
[(< (precedence op) (precedence op2)) |
| 385 |
(loop `(if (zero? ,left) ,fail ,pass) op2 stack)] |
| 386 |
[else |
| 387 |
(join `(if (zero? ,left) ,(loop fail op2 '()) ,pass) |
| 388 |
stack)]))] |
| 389 |
[else ;; assume a (left-assoc) binary operator |
| 390 |
(let* ([right (parse1)] |
| 391 |
[op2 (parse1)]) |
| 392 |
(cond |
| 393 |
[(or (eq? op2 end) (eq? op2 'eof)) |
| 394 |
(join (group op left right) stack)] |
| 395 |
[(<= (precedence op) (precedence op2)) |
| 396 |
;; op2 has less than or equal precedence, group |
| 397 |
(let loop2 ([x (group op left right)] (s stack)) |
| 398 |
(if (and (pair? s) |
| 399 |
(< (precedence (car s)) (precedence op2))) |
| 400 |
(loop2 (group (car s) (cadr s) x) (cddr s)) |
| 401 |
(loop x op2 s)))] |
| 402 |
[else |
| 403 |
;; op2 has higher precedence, push on the stack |
| 404 |
(loop right op2 (cons op (cons left stack)))]))]))))) |
| 405 |
(with-input-from-string str |
| 406 |
(^[] (parse-until 'eof)))) |
| 407 |
(define (map-C-names x) |
| 408 |
;; C routines avoid boolean results |
| 409 |
(define (C:! a) (if (zero? a) 1 0)) |
| 410 |
(define (C:== a b) (if (eqv? a b) 1 0)) |
| 411 |
(define (C:!= a b) (if (eqv? a b) 0 1)) |
| 412 |
(define (C:> a b) (if (> a b) 1 0)) |
| 413 |
(define (C:< a b) (if (< a b) 1 0)) |
| 414 |
(define (C:>= a b) (if (>= a b) 1 0)) |
| 415 |
(define (C:<= a b) (if (<= a b) 1 0)) |
| 416 |
(define (C:>> a b) (ash a (- b))) |
| 417 |
(cond [(symbol? x) (case x ;; map symbols directly to procs |
| 418 |
[(/) quotient] [(%) modulo] [(**) expt] [(!) C:!] |
| 419 |
[(^) logxor] [(<<) ash] [(>>) C:>>] |
| 420 |
[(==) C:==] [(!=) C:!=] [(>) C:>] |
| 421 |
[(<) C:<] [(>=) C:>=] [(<=) C:<=] |
| 422 |
[else x])] |
| 423 |
[(pair? x) (cons (map-C-names (car x)) (map-C-names (cdr x)))] |
| 424 |
[else x])) |
| 425 |
(let1 body (map-C-names (C-parse str)) |
| 426 |
;; could build from chained closures w/o using eval but this is |
| 427 |
;; faster at runtime |
| 428 |
;;(warn "code: ~S" (C-parse str)) |
| 429 |
(eval `(lambda (n) ,body) (scheme-report-environment 5)))) |
| 430 |
|
| 431 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 432 |
;; internal routines for building/caching files and lookups |
| 433 |
|
| 434 |
(define (split-langs lang) |
| 435 |
(let1 res (list lang) |
| 436 |
(for-each (^[sep] (and-let* ([i (string-index lang sep)]) |
| 437 |
(push! res (substring lang 0 i)))) |
| 438 |
'(#\. #\_)) |
| 439 |
(reverse res))) |
| 440 |
|
| 441 |
;; (make-gettext-interal domain locale dirs cdir cached?) |
| 442 |
(define (make-gettext-internal domain locale dirs cdir cached?) |
| 443 |
|
| 444 |
(define (make-cache) (make-hash-table 'string=?)) |
| 445 |
|
| 446 |
(define (make-file-list) |
| 447 |
(define suffixes '(".mo" ".po")) |
| 448 |
(reverse |
| 449 |
(filter |
| 450 |
(^f (file-is-readable? (filename-of f))) |
| 451 |
(map |
| 452 |
(^x (make-gettext-file |
| 453 |
(string-append (caddr x) "/" (car x) "/" cdir "/" (cadr x) (cadddr x)) |
| 454 |
(car x))) |
| 455 |
(cartesian-product (list (append-map split-langs locale) |
| 456 |
domain dirs suffixes)))))) |
| 457 |
|
| 458 |
(let ([files (make-file-list)] |
| 459 |
[cache (make-cache)]) |
| 460 |
|
| 461 |
(define (search msg :optional (msg2 #f) (n #f)) |
| 462 |
(if (and cached? (hash-table-exists? cache msg)) |
| 463 |
(hash-table-get cache msg #f) |
| 464 |
(let1 split? (number? n) |
| 465 |
(any (^f (unless (properties-of f) |
| 466 |
(gettext-file-update-properties! f)) |
| 467 |
(and-let* ([x0 (lookup-message f msg msg2)] |
| 468 |
[x (if (and split? (eq? (type-of f) 'mo)) |
| 469 |
(cons (or msg2 msg) |
| 470 |
(let1 l (string-split x0 #\null) |
| 471 |
(map cons (iota (length l)) l))) |
| 472 |
x0)] |
| 473 |
[res (cons x f)]) |
| 474 |
(if cached? (hash-table-put! cache msg res)) |
| 475 |
res)) |
| 476 |
files)))) |
| 477 |
|
| 478 |
(define (get msg) |
| 479 |
(if-let1 res (search msg) |
| 480 |
(if (pair? (car res)) (caar res) (car res)) |
| 481 |
msg)) |
| 482 |
|
| 483 |
(define (nget msg . opt) ;; [msg2] [n] |
| 484 |
(let ([msg2 #f] [n #f]) |
| 485 |
;; option parsing, both optional |
| 486 |
(when (pair? opt) |
| 487 |
(let1 x (car opt) (if (number? x) (set! n x) (set! msg2 x))) |
| 488 |
(when (pair? (cdr opt)) |
| 489 |
(let1 x (cadr opt) (if (number? x) (set! n x) (set! msg2 x))))) |
| 490 |
(let1 res (search msg msg2 n) |
| 491 |
(if (pair? res) |
| 492 |
(let1 plural-index (get-plural-index! (cdr res)) |
| 493 |
(or (assv-ref (cdar res) (plural-index (or n 1))) |
| 494 |
(if (eqv? n 1) msg (caar res)))) |
| 495 |
(if (or (eqv? n 1) (not msg2)) msg msg2))))) |
| 496 |
|
| 497 |
(define (set msg val) (hash-table-put! cache msg val)) |
| 498 |
|
| 499 |
(define (reset!) |
| 500 |
(set! files (make-file-list)) |
| 501 |
(set! cache (make-cache))) |
| 502 |
|
| 503 |
;; return the dispatcher |
| 504 |
(^[dispatch . args] |
| 505 |
(case dispatch |
| 506 |
[(searcher) search] |
| 507 |
[(getter) get] |
| 508 |
[(ngetter) nget] |
| 509 |
[(setter) set] |
| 510 |
[(search) (apply search args)] |
| 511 |
[(get) (apply get args)] |
| 512 |
[(nget) (apply nget args)] |
| 513 |
[(set!) (apply set args)] |
| 514 |
[(locale) locale] |
| 515 |
[(domain) domain] |
| 516 |
[(dirs) dirs] |
| 517 |
[(set-locale!) (set! locale (listify (car args))) (reset!)] |
| 518 |
[(set-domain!) (set! domain (listify (car args))) (reset!)] |
| 519 |
[(set-dirs!) (set! dirs (listify (car args))) (reset!)] |
| 520 |
[(use-cache) (set! cached? (car args))] |
| 521 |
[(clear) (set! cache (make-cache))] |
| 522 |
)))) |
| 523 |
|
| 524 |
;; cache the lookups and provide a more friendly interface. should this |
| 525 |
;; take keyword arguments? |
| 526 |
;; (make-gettext domain locale dirs cdir gettext-cached? lookup-cached?) |
| 527 |
(define make-gettext |
| 528 |
(let ((gettext-lookup-cache (make-hash-table 'equal?))) |
| 529 |
(lambda opt |
| 530 |
(let-optionals* opt |
| 531 |
((domain0 '("default")) |
| 532 |
(locale0 #f) |
| 533 |
(dirs0 #f) |
| 534 |
(cdir0 #f) |
| 535 |
(gettext-cached? #t) |
| 536 |
(lookup-cached? #t)) |
| 537 |
(let* ((domain (listify domain0)) |
| 538 |
(locale (listify (or locale0 (sys-getenv "LANG") |
| 539 |
(sys-getenv "LC_ALL") "C"))) |
| 540 |
(dirs1 (listify (or dirs0 (cond ((sys-getenv "GETTEXT_PATH") |
| 541 |
=> (cut string-split <> ":")) |
| 542 |
(else '()))))) |
| 543 |
;; prepend default dirs based on domain |
| 544 |
(dirs (append (hash-table-get domain-message-paths domain |
| 545 |
message-path) |
| 546 |
dirs1)) |
| 547 |
(cdir (or cdir0 (sys-getenv "LC_CATEGORY") "LC_MESSAGES"))) |
| 548 |
;; optionally lookup from cache |
| 549 |
(if lookup-cached? |
| 550 |
(let* ((key (list domain locale dirs cdir gettext-cached?)) |
| 551 |
(lookup (hash-table-get gettext-lookup-cache key #f))) |
| 552 |
(unless lookup |
| 553 |
(set! lookup (make-gettext-internal domain locale dirs |
| 554 |
cdir gettext-cached?)) |
| 555 |
(hash-table-put! gettext-lookup-cache key lookup)) |
| 556 |
lookup) |
| 557 |
(make-gettext-internal domain locale dirs cdir gettext-cached?))))))) |