Debian Codegraph @

lib/srfi/29/bundle.scm

raw
1 ;;;
2 ;;; SRFI-29 - Localization
3 ;;;
4 ;;; Alex Shinn
5 ;;;
6
7 ;; This module provides basic support for SRFI-29 API.
8 ;;
9 ;; load-bundle! and store-bundle! don't actually load/store the bundle
10 ;; and they return #f (permitted in SRFI-29 spec).
11
12 (define-module srfi.29.bundle
13 (use srfi.13)
14 (export current-language current-country current-locale-details
15 load-bundle! store-bundle! declare-bundle! localized-template))
16 (select-module srfi.29.bundle)
17
18 ;; bundle specifiers are (package ... [country] lang)
19 (define *bundles* (make-hash-table 'equal?))
20
21 ;; implement as parameters (default to something other than en?)
22 (define current-language (make-parameter 'en))
23 (define current-country (make-parameter 'us))
24 (define current-locale-details (make-parameter '()))
25
26 ;; initialize locale from LANG env variable if defined
27 (let ((lang (sys-getenv "LANG")))
28 (cond ((and lang (#/^(\w+)(?:[-_](\w+))?(?:\.(.*))?$/ lang))
29 => (lambda (m)
30 (current-language (string->symbol (m 1)))
31 (cond ((m 2)
32 => (lambda (x)
33 (current-country
34 (string->symbol (string-downcase x))))))
35 (cond ((m 3)
36 => (lambda (x)
37 (current-locale-details
38 (list (string->symbol (string-downcase x)))))))))))
39
40 ;; possibly tie these in with text.gettext
41 (define (load-bundle! bundle-specifier) #f)
42 (define (store-bundle! bundle-specifier) #f)
43
44 ;; could also use read/write, but doesn't have tool support like gettext
45 ; (use file.util)
46 ; (define (bundle->file x)
47 ; (apply build-path (gauche-library-directory) "srfi-29/bundles" x))
48 ; (define (read-from-file file)
49 ; (and (file-exists? file)
50 ; (with-error-handler (lambda (err . opt) (warn err) #f)
51 ; (with-input-from-file file read))))
52 ; (define (write-to-file file obj)
53 ; (with-error-handler (lambda (err . opt) (warn err) #f)
54 ; (with-output-to-file file (cut write obj))))
55 ; (define (load-bundle! bundle-specifier)
56 ; (cond ((read-from-file (bundle->file bundle-specifier))
57 ; => (lambda (ls)
58 ; (hash-table-put! *bundles* bundle-specifier
59 ; (alist->hash-table ls))))))
60 ; (define (store-bundle! bundle-specifier)
61 ; (write-to-file (bundle->file bundle-specifier)
62 ; (hash-table-get *bundles* bundle-specifier)))
63
64 ;; declare a bundle of templates with a given bundle specifier
65 (define (declare-bundle! bundle-specifier bundle-assoc-list)
66 (hash-table-put! *bundles* bundle-specifier
67 (alist->hash-table bundle-assoc-list)))
68
69 ;; lookup a name in a given package
70 (define (localized-template package-name template-name)
71 (define (rdc ls)
72 (cond ((null? ls) '())
73 ((null? (cdr ls)) '())
74 (else (cons (car ls) (rdc (cdr ls))))))
75 (let loop ((name (list package-name (current-language) (current-country))))
76 (let ((bundle (hash-table-get *bundles* name #f)))
77 (or (and bundle (hash-table-get bundle template-name #f))
78 (let ((next (rdc name)))
79 (and (pair? next) (loop next)))))))