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

Contents of /cl-l10n/locale.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (show annotations)
Thu Jun 15 19:57:34 2006 UTC (7 years, 10 months ago) by alendvai
Branch: MAIN
CVS Tags: HEAD
Changes since 1.13: +3 -3 lines
Added arnesi and iterate dependency, lookup-first-matching-resource
1 ;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;; See the file LICENCE for licence information.
3
4 ;; TODO
5 ;; use LC_COLLATE to define locale-uppercase and friends
6 ;; Test on windows.
7 ;; Parsers (money)
8 ;; locale aliases?
9 ;; Optimizing print-time
10 ;; Handle _ and - in time directives (see date --help)
11 ;; Compile locales into fasl files.
12
13 (in-package :cl-l10n )
14
15 (defvar *locale-path*
16 (merge-pathnames (make-pathname :directory '(:relative "locales"))
17 (asdf:component-pathname (asdf:find-system :cl-l10n))))
18
19 (defvar *locale* nil
20 "Either a locale or a list of locales in which case resources will be looked for in each locale in order.")
21
22 (defun current-locale ()
23 (declare (inline current-locale))
24 (if (consp *locale*)
25 (car *locale*)
26 *locale*))
27
28 (defvar *locales* (make-hash-table :test #'equal)
29 "Hash table containing all loaded locales keyed on name (eg. \"af_ZA\")")
30
31 ;; Conditions
32 (define-condition locale-error (error)
33 ((mesg :accessor mesg :initarg :mesg :initform "Unknown."))
34 (:report (lambda (obj stream) (cl:format stream "~A" (mesg obj)))))
35
36 (defun locale-error (string &rest args)
37 (error 'locale-error :mesg (apply #'cl:format nil string args)))
38
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 (printers :accessor printers :initarg :printers :initform nil)
45 (parsers :accessor parsers :initarg :parsers :initform nil)
46 (source :accessor source :initarg :source :initform nil)
47 (language :accessor language :initarg :language :initform nil)
48 (territory :accessor territory :initarg :territory :initform nil)
49 (revision :accessor revision :initarg :revision :initform nil)
50 (date :accessor date :initarg :date :initform nil)
51 (categories :accessor categories :initarg :categories
52 :initform (make-hash-table :test #'equal))))
53
54 (defmethod print-object ((obj locale) stream)
55 (print-unreadable-object (obj stream :type t :identity t)
56 (princ (locale-name obj) stream)))
57
58 (defclass category ()
59 ((category-name :accessor category-name :initform (required-arg :name)
60 :initarg :name)
61 (vals :accessor vals :initform (make-hash-table :test #'equal)
62 :initarg :vals)))
63
64 (defmethod print-object ((obj category) stream)
65 (print-unreadable-object (obj stream :type t :identity t)
66 (princ (category-name obj) stream)))
67
68
69 (declaim (inline get-locale))
70 (defun get-locale (name)
71 (gethash name *locales*))
72
73 (defun (setf get-locale) (new-val name)
74 (setf (gethash name *locales*)
75 new-val))
76
77 (defgeneric get-category (locale name)
78 (:documentation "Find category called NAME in locale LOCALE.")
79 (:method ((locale locale) (name string))
80 (gethash name (categories locale))))
81
82 (defmethod (setf get-category) ((new-val category) (locale locale) (name string))
83 (setf (gethash name (categories locale))
84 new-val))
85
86 (defgeneric category-value (category key)
87 (:documentation "Lookup attribute named by string KEY in category CATEGORY.")
88 (:method ((category category) (key string))
89 (gethash key (vals category))))
90
91 (defmethod (setf category-value) ((new-val t) (category category) (key string))
92 (setf (gethash key (vals category))
93 new-val))
94
95 (defun locale-value (locale cat key)
96 (awhen (get-category locale cat)
97 (category-value it key)))
98
99 (defun getenv (word)
100 #+sbcl (sb-ext:posix-getenv word)
101 #+lispworks (lw:environment-variable word)
102 #+acl (sys:getenv word)
103 #+cmu (cdr (assoc (intern word :keyword) ext:*environment-list*))
104 #+clisp (ext:getenv word)
105 #+ecl (si:getenv word))
106
107 ;; Getters
108 (defmacro defgetter (key cat &key (wrap '#'identity))
109 (let ((name (intern-concat (list "LOCALE-" (substitute #\- #\_ (string-upcase key))))))
110 `(progn
111 (defun ,name (&optional (locale (current-locale)))
112 (let ((locale (locale locale)))
113 (when locale
114 (funcall ,wrap (locale-value locale ,cat ,key)))))
115 (export ',name))))
116
117 (defun parse-car-or-val (x)
118 (values (parse-integer (if (consp x) (car x) x))))
119
120 (defgetter "int_curr_symbol" "LC_MONETARY")
121 (defgetter "currency_symbol" "LC_MONETARY")
122 (defgetter "mon_decimal_point" "LC_MONETARY")
123 (defgetter "mon_thousands_sep" "LC_MONETARY")
124 (defgetter "mon_grouping" "LC_MONETARY" :wrap 'parse-car-or-val)
125 (defgetter "positive_sign" "LC_MONETARY")
126 (defgetter "negative_sign" "LC_MONETARY")
127 (defgetter "int_frac_digits" "LC_MONETARY" :wrap 'parse-car-or-val)
128 (defgetter "frac_digits" "LC_MONETARY" :wrap 'parse-car-or-val)
129 (defgetter "p_cs_precedes" "LC_MONETARY" :wrap 'parse-car-or-val)
130 (defgetter "p_sep_by_space" "LC_MONETARY" :wrap 'parse-car-or-val)
131 (defgetter "n_cs_precedes" "LC_MONETARY" :wrap 'parse-car-or-val)
132 (defgetter "n_sep_by_space" "LC_MONETARY" :wrap 'parse-car-or-val)
133 (defgetter "p_sign_posn" "LC_MONETARY" :wrap 'parse-car-or-val)
134 (defgetter "n_sign_posn" "LC_MONETARY" :wrap 'parse-car-or-val)
135 (defgetter "decimal_point" "LC_NUMERIC")
136 (defgetter "thousands_sep" "LC_NUMERIC")
137 (defgetter "grouping" "LC_NUMERIC" :wrap 'parse-car-or-val)
138 (defgetter "abday" "LC_TIME")
139 (defgetter "day" "LC_TIME")
140 (defgetter "abmon" "LC_TIME")
141 (defgetter "mon" "LC_TIME")
142 (defgetter "d_t_fmt" "LC_TIME")
143 (defgetter "d_fmt" "LC_TIME")
144 (defgetter "t_fmt" "LC_TIME")
145 (defgetter "am_pm" "LC_TIME")
146 (defgetter "t_fmt_ampm" "LC_TIME")
147 (defgetter "date_fmt" "LC_TIME")
148 (defgetter "yesexpr" "LC_MESSAGES")
149 (defgetter "noexpr" "LC_MESSAGES")
150 (defgetter "height" "LC_PAPER")
151 (defgetter "width" "LC_PAPER")
152 (defgetter "name_fmt" "LC_NAME")
153 (defgetter "name_gen" "LC_NAME")
154 (defgetter "name_mr" "LC_NAME")
155 (defgetter "name_mrs" "LC_NAME")
156 (defgetter "name_miss" "LC_NAME")
157 (defgetter "name_ms" "LC_NAME")
158 (defgetter "postal_fmt" "LC_ADDRESS")
159 (defgetter "tel_int_fmt" "LC_TELEPHONE")
160 (defgetter "measurement" "LC_MEASUREMENT")
161
162
163 ;; EOF

  ViewVC Help
Powered by ViewVC 1.1.5