/[cl-l10n]/cl-l10n/locale.lisp
ViewVC logotype

Contents of /cl-l10n/locale.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations)
Thu Dec 30 11:56:38 2004 UTC (9 years, 3 months ago) by sross
Branch: MAIN
Changes since 1.5: +24 -17 lines
ChangeLog 2004-12-30
1 sross 1.1 ;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2     ;; See the file LICENCE for licence information.
3    
4     ;; TODO
5     ;; What to do with LC_CTYPE, LC_COLLATE
6     ;; Test on windows.
7 sross 1.6 ;; Parsers (money and time)
8 sross 1.5 ;; locale aliases
9 sross 1.6 ;; Optimizing print-time
10 sross 1.1
11     (in-package :cl-l10n )
12    
13     ;; Variables
14     (defvar *locale-path*
15     (let ((path *load-pathname*))
16     (make-pathname :host (pathname-host path)
17     :device (pathname-device path)
18     :directory
19     (append (pathname-directory path)
20     '("locales"))
21     :defaults #P"")))
22    
23     (defvar *locale* nil)
24    
25 sross 1.6 (defvar *locales* (make-hash-table :test #'equal)
26     "Hash table containing all loaded locales keyed on name (eg. \"af_ZA\")")
27 sross 1.1
28     ;; Conditions
29     (defun locale-report (obj stream)
30 sross 1.6 (cl:format stream "~A" (mesg obj)))
31 sross 1.1
32     (define-condition locale-error ()
33     ((mesg :accessor mesg :initarg :mesg :initform "Unknown."))
34     (:report locale-report))
35    
36     (defun locale-error (string &rest args)
37 sross 1.6 (error 'locale-error :mesg (apply #'cl:format nil string args)))
38 sross 1.1
39     ;; Classes
40     (defclass locale ()
41     ((locale-name :accessor locale-name :initarg :name
42     :initform (required-arg :name))
43     (title :accessor title :initarg :title :initform nil)
44 sross 1.6 (printers :accessor printers :initarg :printers :initform nil)
45 sross 1.1 (source :accessor source :initarg :source :initform nil)
46     (language :accessor language :initarg :language :initform nil)
47     (territory :accessor territory :initarg :territory :initform nil)
48     (revision :accessor revision :initarg :revision :initform nil)
49     (date :accessor date :initarg :date :initform nil)
50     (categories :accessor categories :initarg :categories
51     :initform (make-hash-table :test #'equal))))
52    
53     (defmethod print-object ((obj locale) stream)
54     (print-unreadable-object (obj stream :type t :identity t)
55     (princ (locale-name obj) stream)))
56    
57     (defclass category ()
58     ((category-name :accessor category-name :initform (required-arg :name)
59     :initarg :name)
60     (vals :accessor vals :initform (make-hash-table :test #'equal)
61     :initarg :vals)))
62    
63     (defmethod print-object ((obj category) stream)
64     (print-unreadable-object (obj stream :type t :identity t)
65     (princ (category-name obj) stream)))
66    
67    
68     ;; Macros
69     (defmacro get-locale (name)
70     `(gethash ,name *locales*))
71    
72     (defmacro get-category (name locale)
73     `(gethash ,name (categories ,locale)))
74    
75     (defmacro get-cat-val (value cat)
76     `(gethash ,value (vals ,cat)))
77    
78     (defun locale-value (locale cat key)
79     (awhen (get-category cat locale)
80     (get-cat-val key it)))
81    
82     (defun getenv (word)
83     #+sbcl (sb-ext:posix-getenv word)
84     #+lispworks (hcl:getenv word)
85     #+cmu (cdr (assoc (intern word :keyword) ext:*environment-list*))
86 sross 1.5 #+clisp (ext:getenv word)
87     #+ecl (si:getenv word))
88 sross 1.1
89 sross 1.6 ;; Getters
90     (let ((getter-cache (make-hash-table :test #'equal)))
91     (defun gett-value (locale cat key &optional (wrap #'identity))
92     (let ((lookup-key (list locale cat key)))
93     (multiple-value-bind (val win) (gethash lookup-key getter-cache)
94     (if (or val win)
95     val
96     (setf (gethash lookup-key getter-cache)
97     (funcall wrap (locale-value locale cat key)))))))
98     (defun clear-getter-cache ()
99     (setf getter-cache (make-hash-table :test #'equal))))
100 sross 1.1
101     (defmacro defgetter (key cat &key wrap)
102     (let ((name (symb "LOCALE-" (substitute #\- #\_ (string-upcase key)))))
103     `(progn
104 sross 1.6 (defun ,name (&optional (locale *locale*))
105     (let ((locale (locale-des->locale locale)))
106     (when locale
107     (gett-value locale ,cat ,key ,@(if wrap (list wrap) nil)))))
108     (export ',name))))
109 sross 1.1
110     (defun parse-car-or-val (x)
111     (values (parse-integer (if (consp x) (car x) x))))
112    
113     (defgetter "int_curr_symbol" "LC_MONETARY")
114     (defgetter "currency_symbol" "LC_MONETARY")
115     (defgetter "mon_decimal_point" "LC_MONETARY")
116     (defgetter "mon_thousands_sep" "LC_MONETARY")
117     (defgetter "mon_grouping" "LC_MONETARY" :wrap 'parse-car-or-val)
118     (defgetter "positive_sign" "LC_MONETARY")
119     (defgetter "negative_sign" "LC_MONETARY")
120     (defgetter "int_frac_digits" "LC_MONETARY" :wrap 'parse-car-or-val)
121     (defgetter "frac_digits" "LC_MONETARY" :wrap 'parse-car-or-val)
122     (defgetter "p_cs_precedes" "LC_MONETARY" :wrap 'parse-car-or-val)
123     (defgetter "p_sep_by_space" "LC_MONETARY" :wrap 'parse-car-or-val)
124     (defgetter "n_cs_precedes" "LC_MONETARY" :wrap 'parse-car-or-val)
125     (defgetter "n_sep_by_space" "LC_MONETARY" :wrap 'parse-car-or-val)
126     (defgetter "p_sign_posn" "LC_MONETARY" :wrap 'parse-car-or-val)
127     (defgetter "n_sign_posn" "LC_MONETARY" :wrap 'parse-car-or-val)
128     (defgetter "decimal_point" "LC_NUMERIC")
129     (defgetter "thousands_sep" "LC_NUMERIC")
130     (defgetter "grouping" "LC_NUMERIC" :wrap 'parse-car-or-val)
131     (defgetter "abday" "LC_TIME")
132     (defgetter "day" "LC_TIME")
133     (defgetter "abmon" "LC_TIME")
134     (defgetter "mon" "LC_TIME")
135     (defgetter "d_t_fmt" "LC_TIME")
136     (defgetter "d_fmt" "LC_TIME")
137     (defgetter "t_fmt" "LC_TIME")
138     (defgetter "am_pm" "LC_TIME")
139     (defgetter "t_fmt_ampm" "LC_TIME")
140     (defgetter "date_fmt" "LC_TIME")
141     (defgetter "yesexpr" "LC_MESSAGES")
142     (defgetter "noexpr" "LC_MESSAGES")
143     (defgetter "height" "LC_PAPER")
144     (defgetter "width" "LC_PAPER")
145     (defgetter "name_fmt" "LC_NAME")
146     (defgetter "name_gen" "LC_NAME")
147     (defgetter "name_mr" "LC_NAME")
148     (defgetter "name_mrs" "LC_NAME")
149     (defgetter "name_miss" "LC_NAME")
150     (defgetter "name_ms" "LC_NAME")
151     (defgetter "postal_fmt" "LC_ADDRESS")
152     (defgetter "tel_int_fmt" "LC_TELEPHONE")
153     (defgetter "measurement" "LC_MEASUREMENT")
154    
155    
156 sross 1.6 ;; EOF

  ViewVC Help
Powered by ViewVC 1.1.5