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

Contents of /cl-l10n/locale.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5