/[cmucl]/src/code/intl.lisp
ViewVC logotype

Contents of /src/code/intl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (show annotations)
Sun Dec 19 03:01:47 2010 UTC (3 years, 3 months ago) by rtoy
Branch: MAIN
CVS Tags: GIT-CONVERSION, cross-sol-x86-merged, snapshot-2011-09, snapshot-2011-06, snapshot-2011-07, snapshot-2011-04, snapshot-2011-02, snapshot-2011-03, snapshot-2011-01, HEAD
Changes since 1.10: +37 -37 lines
Oops.  If the domain-entry-encoding is NIL, we just want to return
NIL.  (From Paul Foley.)
1 ;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Package: INTL -*-
2
3 ;;; $Revision: 1.11 $
4 ;;; Copyright 1999-2010 Paul Foley (mycroft@actrix.gen.nz)
5 ;;;
6 ;;; Permission is hereby granted, free of charge, to any person obtaining
7 ;;; a copy of this Software to deal in the Software without restriction,
8 ;;; including without limitation the rights to use, copy, modify, merge,
9 ;;; publish, distribute, sublicense, and/or sell copies of the Software,
10 ;;; and to permit persons to whom the Software is furnished to do so,
11 ;;; provided that the above copyright notice and this permission notice
12 ;;; are included in all copies or substantial portions of the Software.
13 ;;;
14 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
15 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
18 ;;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 ;;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20 ;;; OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
21 ;;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 ;;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
24 ;;; USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25 ;;; DAMAGE.
26 (ext:file-comment "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/intl.lisp,v 1.11 2010/12/19 03:01:47 rtoy Exp $")
27
28 (in-package "INTL")
29
30 (eval-when (:compile-toplevel :execute)
31 (defparameter intl::*default-domain* "cmucl")
32 (unless (and (fboundp 'intl:read-translatable-string)
33 (eq (get-macro-character #\_)
34 (fdefinition 'intl:read-translatable-string)))
35 (set-macro-character #\_ (lambda (stream char)
36 (declare (ignore char))
37 (case (peek-char nil stream nil nil t)
38 (#\" (values))
39 (#\N (read-char stream t nil t) (values))
40 (otherwise '_)))
41 t)))
42
43 (in-package "INTL")
44
45 (defvar *locale-directories*
46 '(#p"library:locale/" #p"/usr/share/locale/" #p"target:i18n/locale/"))
47 (defvar *locale* "C")
48
49 (defvar *default-domain* nil
50 "The message-lookup domain used by INTL:GETTEXT and INTL:NGETTEXT.
51 Use (INTL:TEXTDOMAIN \"whatever\") in each source file to set this.")
52 (defvar *loaded-domains* (make-hash-table :test 'equal))
53 (defvar *locale-aliases* (make-hash-table :test 'equal))
54
55 (defstruct domain-entry
56 (domain "" :type simple-base-string)
57 (locale "" :type simple-base-string)
58 (file #p"" :type pathname)
59 (plurals nil :type (or null function))
60 (hash (make-hash-table :test 'equal) :type hash-table)
61 (encoding nil)
62 (readfn #'identity :type function))
63
64 (declaim (ftype (function (stream) (unsigned-byte 32)) read-lelong))
65 (defun read-lelong (stream)
66 (declare #+(or)(optimize (speed 3) (space 2) (safety 0)
67 #+CMU (ext:inhibit-warnings 3))) ;quiet about boxing retn
68 (+ (the (unsigned-byte 8) (read-byte stream))
69 (ash (the (unsigned-byte 8) (read-byte stream)) 8)
70 (ash (the (unsigned-byte 8) (read-byte stream)) 16)
71 (ash (the (unsigned-byte 8) (read-byte stream)) 24)))
72
73 (declaim (ftype (function (stream) (unsigned-byte 32)) read-belong))
74 (defun read-belong (stream)
75 (declare #+(or)(optimize (speed 3) (space 2) (safety 0)
76 #+CMU (ext:inhibit-warnings 3))) ;quiet about boxing retn
77 (+ (ash (the (unsigned-byte 8) (read-byte stream)) 24)
78 (ash (the (unsigned-byte 8) (read-byte stream)) 16)
79 (ash (the (unsigned-byte 8) (read-byte stream)) 8)
80 (the (unsigned-byte 8) (read-byte stream))))
81
82 (defun locate-domain-file (domain locale locale-dir)
83 ;; The default locale-dir includes search lists. If we get called
84 ;; before the search lists are initialized, we lose. The search
85 ;; lists are initialized in environment-init, which sets
86 ;; *environment-list-initialized*. This way, we return NIL to
87 ;; indicate there's no domain file to use.
88 (when lisp::*environment-list-initialized*
89 (flet ((path (locale base)
90 (merge-pathnames (make-pathname :directory (list :relative locale
91 "LC_MESSAGES")
92 :name domain :type "mo")
93 base)))
94 (let ((locale (or (gethash locale *locale-aliases*) locale)))
95 (dolist (base (if (listp locale-dir) locale-dir (list locale-dir)))
96 (let ((probe
97 (or (probe-file (path locale base))
98 (let ((dot (position #\. locale)))
99 (and dot (probe-file (path (subseq locale 0 dot) base))))
100 (let ((at (position #\@ locale)))
101 (and at (probe-file (path (subseq locale 0 at) base))))
102 (let ((us (position #\_ locale)))
103 (and us (probe-file (path (subseq locale 0 us) base)))))))
104 (when probe (return probe))))))))
105
106 (defun find-encoding (domain)
107 (when (null (domain-entry-encoding domain))
108 (setf (domain-entry-encoding domain) :iso-8859-1)
109 ;; Domain lookup can call the compiler, so set the locale to "C"
110 ;; so things work.
111 (let* ((*locale* "C")
112 (header (domain-lookup "" domain))
113 (ctype (search "Content-Type: " header))
114 (eoln (and ctype (position #\Newline header :start ctype)))
115 (charset (and ctype (search "; charset=" header
116 :start2 ctype :end2 eoln))))
117 (when charset
118 (incf charset 10)
119 (loop for i upfrom charset below eoln as c = (char header i)
120 while (or (alphanumericp c) (eql c #\-))
121 finally (setf (domain-entry-encoding domain)
122 (intern (nstring-upcase (subseq header charset i))
123 "KEYWORD"))))))
124 domain)
125
126 (defun parse-plurals (domain)
127 (let* ((header (domain-lookup "" domain))
128 (plurals (search "Plural-Forms: " header))
129 (default (lambda (n) (if (= n 1) 0 1))))
130 (if (and plurals
131 (> (length header) (+ plurals 36))
132 (string= header "nplurals="
133 :start1 (+ plurals 14) :end1 (+ plurals 23)))
134 (let ((nplurals
135 (parse-integer header :start (+ plurals 23) :junk-allowed t))
136 (point (+ (position #\; header :start (+ plurals 23)) 2)))
137 (if (and (> (length header) (+ point 10))
138 (string= header "plural=" :start1 point :end1 (+ point 7)))
139 (values (parse-expr header (+ point 7)) nplurals)
140 (values default 2)))
141 (values default 2))))
142
143 (defun parse-expr (string pos)
144 (labels ((next ()
145 (loop while (member (char string pos) '(#\Space #\Tab #\Newline))
146 do (incf pos))
147 (case (char string (1- (incf pos)))
148 (#\n 'n)
149 (#\? 'IF)
150 (#\: 'THEN)
151 (#\( 'LPAR)
152 (#\) 'RPAR)
153 (#\^ 'LOGXOR)
154 (#\+ 'ADD)
155 (#\- 'SUB)
156 (#\* 'MUL)
157 (#\/ 'FLOOR)
158 (#\% 'MOD)
159 (#\~ 'LOGNOT32)
160 (#\; 'END)
161 (#\| (if (char= (char string pos) #\|)
162 (progn (incf pos) 'COR)
163 'LOGIOR))
164 (#\& (if (char= (char string pos) #\&)
165 (progn (incf pos) 'CAND)
166 'LOGAND))
167 (#\= (if (char= (char string pos) #\=)
168 (progn (incf pos) 'CMP=)
169 (error (intl:gettext "Encountered illegal token: ="))))
170 (#\! (if (char= (char string pos) #\=)
171 (progn (incf pos) 'CMP/=)
172 'NOT))
173 (#\< (case (char string pos)
174 (#\= (incf pos) 'CMP<=)
175 (#\< (incf pos) 'SHL)
176 (otherwise 'CMP<)))
177 (#\> (case (char string pos)
178 (#\= (incf pos) 'CMP>=)
179 (#\> (incf pos) 'SHR)
180 (otherwise 'CMP>)))
181 (otherwise (let ((n (digit-char-p (char string (1- pos)))))
182 (if n
183 (loop for nx = (digit-char-p (char string pos))
184 while nx
185 do (setq n (+ (* n 10) nx)) (incf pos)
186 finally (return n))
187 (error (intl:gettext "Encountered illegal token: ~C")
188 (char string (1- pos))))))))
189 (conditional (tok &aux tree)
190 (multiple-value-setq (tree tok) (logical-or tok))
191 (when (eql tok 'IF)
192 (multiple-value-bind (right next) (logical-or (next))
193 (unless (eql next 'THEN)
194 (error (intl:gettext "Expected : in ?: construct")))
195 (multiple-value-bind (else next) (conditional (next))
196 (setq tree (list tok (list 'zerop tree) else right)
197 tok next))))
198 (values tree tok))
199 (logical-or (tok &aux tree)
200 (multiple-value-setq (tree tok) (logical-and tok))
201 (loop while (eql tok 'COR) do
202 (multiple-value-bind (right next) (logical-and (next))
203 (setq tree (list tok tree right)
204 tok next)))
205 (values tree tok))
206 (logical-and (tok &aux tree)
207 (multiple-value-setq (tree tok) (inclusive-or tok))
208 (loop while (eql tok 'CAND) do
209 (multiple-value-bind (right next) (inclusive-or (next))
210 (setq tree (list tok tree right)
211 tok next)))
212 (values tree tok))
213 (inclusive-or (tok &aux tree)
214 (multiple-value-setq (tree tok) (exclusive-or tok))
215 (loop while (eql tok 'LOGIOR) do
216 (multiple-value-bind (right next) (exclusive-or (next))
217 (setq tree (list tok tree right)
218 tok next)))
219 (values tree tok))
220 (exclusive-or (tok &aux tree)
221 (multiple-value-setq (tree tok) (bitwise-and tok))
222 (loop while (eql tok 'LOGXOR) do
223 (multiple-value-bind (right next) (bitwise-and (next))
224 (setq tree (list tok tree right)
225 tok next)))
226 (values tree tok))
227 (bitwise-and (tok &aux tree)
228 (multiple-value-setq (tree tok) (equality tok))
229 (loop while (eql tok 'LOGAND) do
230 (multiple-value-bind (right next) (equality (next))
231 (setq tree (list tok tree right)
232 tok next)))
233 (values tree tok))
234 (equality (tok &aux tree)
235 (multiple-value-setq (tree tok) (relational tok))
236 (loop while (member tok '(CMP= CMP/=)) do
237 (multiple-value-bind (right next) (relational (next))
238 (setq tree (list tok tree right)
239 tok next)))
240 (values tree tok))
241 (relational (tok &aux tree)
242 (multiple-value-setq (tree tok) (shift tok))
243 (loop while (member tok '(CMP< CMP> CMP<= CMP>=)) do
244 (multiple-value-bind (right next) (shift (next))
245 (setq tree (list tok tree right)
246 tok next)))
247 (values tree tok))
248 (shift (tok &aux tree)
249 (multiple-value-setq (tree tok) (additive tok))
250 (loop while (member tok '(SHL SHR)) do
251 (multiple-value-bind (right next) (additive (next))
252 (setq tree (list tok tree right)
253 tok next)))
254 (values tree tok))
255 (additive (tok &aux tree)
256 (multiple-value-setq (tree tok) (multiplicative tok))
257 (loop while (member tok '(ADD SUB)) do
258 (multiple-value-bind (right next) (multiplicative (next))
259 (setq tree (list tok tree right)
260 tok next)))
261 (values tree tok))
262 (multiplicative (tok &aux tree)
263 (multiple-value-setq (tree tok) (unary tok))
264 (loop while (member tok '(MUL FLOOR MOD)) do
265 (multiple-value-bind (right next) (unary (next))
266 (setq tree (list tok tree right)
267 tok next)))
268 (values tree tok))
269 (unary (tok &aux tree)
270 (cond ((eq tok 'LPAR)
271 (multiple-value-setq (tree tok) (conditional (next)))
272 (unless (eq tok 'RPAR)
273 (error (intl:gettext "Expected close-paren.")))
274 (values tree (next)))
275 ((numberp tok)
276 (values tok (next)))
277 ((eql tok 'n)
278 (values tok (next)))
279 ((eql tok 'ADD)
280 (unary (next)))
281 ((eql tok 'SUB)
282 (multiple-value-setq (tree tok) (unary (next)))
283 (values (list '- tree) tok))
284 ((eql tok 'LOGNOT32)
285 (multiple-value-setq (tree tok) (unary (next)))
286 (values (list 'LOGNOT32 tree) tok))
287 ((eql tok 'NOT)
288 (multiple-value-setq (tree tok) (unary (next)))
289 (values (list 'CNOT tree) tok))
290 (t
291 (error (intl:gettext "Unexpected token: ~S.") tok)))))
292 (multiple-value-bind (tree end) (conditional (next))
293 (unless (eq end 'END)
294 (error (intl:gettext "Expecting end of expression. ~S.") end))
295 (let ((*compile-print* nil))
296 (compile nil
297 `(lambda (n)
298 (declare (type (unsigned-byte 32) n)
299 (optimize (space 3)))
300 (flet ((add (a b) (ldb (byte 32 0) (+ a b)))
301 (sub (a b) (ldb (byte 32 0) (- a b)))
302 (mul (a b) (ldb (byte 32 0) (* a b)))
303 (shl (a b) (ldb (byte 32 0) (ash a b)))
304 (shr (a b) (ash a (- b)))
305 (cmp= (a b) (if (= a b) 1 0))
306 (cmp/= (a b) (if (/= a b) 1 0))
307 (cmp< (a b) (if (< a b) 1 0))
308 (cmp<= (a b) (if (<= a b) 1 0))
309 (cmp> (a b) (if (> a b) 1 0))
310 (cmp>= (a b) (if (>= a b) 1 0))
311 (cand (a b) (if (or (zerop a) (zerop b)) 0 1))
312 (cor (a b) (if (and (zerop a) (zerop b)) 0 1))
313 (cnot (a) (if a 0 1))
314 (lognot32 (a) (ldb (byte 32 0) (lognot a))))
315 (declare (ignorable #'add #'sub #'mul #'shr #'shl
316 #'cmp= #'cmp/=
317 #'cmp< #'cmp<= #'cmp> #'cmp>=
318 #'cand #'cor #'cnot #'lognot32))
319 ,tree)))))))
320
321 (defun load-domain (domain locale &optional (locale-dir *locale-directories*))
322 (let ((file (locate-domain-file domain locale locale-dir))
323 (read #'read-lelong))
324 (unless file
325 (let ((entry (make-domain-entry :domain domain :locale locale
326 :hash (make-hash-table :size 0
327 :test 'equal))))
328 (setf (gethash domain *loaded-domains*) entry)
329 (return-from load-domain entry)))
330 (with-open-file (stream file :direction :input :if-does-not-exist nil
331 :element-type '(unsigned-byte 8))
332 (unless stream (return-from load-domain nil))
333 (let ((magic (read-lelong stream)))
334 (cond ((= magic #x950412de) (setq read #'read-lelong))
335 ((= magic #xde120495) (setq read #'read-belong))
336 (t
337 ;; DON'T translate this! If we can't load the domain,
338 ;; we can't print this message, Which causes an error
339 ;; that causes use to do a domain lookup again, which
340 ;; fails which cause an error message which ...
341 (warn "Bad magic number in \"~A.mo\"." domain)
342 (return-from load-domain nil))))
343 (let ((version (funcall read stream))
344 (messages (funcall read stream))
345 (master (funcall read stream))
346 (translation (funcall read stream))
347 (entry (make-domain-entry)))
348 (declare (ignore version))
349 (setf (domain-entry-readfn entry) read)
350 (setf (domain-entry-domain entry) domain)
351 (setf (domain-entry-locale entry) locale)
352 (setf (domain-entry-file entry) file)
353 (dotimes (msg messages)
354 (file-position stream (+ master (* 8 msg)))
355 (let ((length (funcall read stream))
356 (start (funcall read stream)))
357 (setf (gethash length (domain-entry-hash entry))
358 (acons start (+ translation (* 8 msg))
359 (gethash length (domain-entry-hash entry))))))
360 (setf (gethash domain *loaded-domains*) entry)
361 (find-encoding entry)))))
362
363 (defun find-domain (domain locale &optional (locale-dir *locale-directories*))
364 (let ((found (gethash domain *loaded-domains*)))
365 (if (and found (string= (domain-entry-locale found) locale))
366 found
367 (load-domain domain locale locale-dir))))
368
369 (declaim (inline string-to-octets))
370 (defun string-to-octets (string encoding)
371 (declare (ignorable encoding))
372 #+(and CMU Unicode)
373 (ext:string-to-octets string :external-format encoding)
374 #+Allegro
375 (excl:string-to-octets string :external-format encoding :null-terminate nil)
376 #+SBCL
377 (sb-ext:string-to-octets string :external-format encoding
378 :null-terminate nil)
379 #+CLISP ;;@@ Not sure if encoding keyword is OK here
380 (ext:convert-string-to-bytes string encoding)
381 ;;@@ add other implementations
382 #-(or (and CMU Unicode) Allegro SBCL CLISP #|others|#)
383 (map-into (make-array (length string) :element-type '(unsigned-byte 8))
384 #'char-code string))
385
386 (declaim (inline octets-to-string))
387 (defun octets-to-string (octets encoding)
388 (declare (ignorable encoding))
389 #+(and CMU Unicode)
390 (ext:octets-to-string octets :external-format encoding)
391 #+Allegro
392 (excl:octets-to-string octets :external-format encoding :end (length octets))
393 #+SBCL
394 (sb-ext:octets-to-string octets :external-format encoding)
395 #+CLISP ;;@@ Not sure if encoding keyword is OK here
396 (ext:convert-string-from-bytes octets encoding)
397 ;;@@ add other implementations
398 #-(or (and CMU Unicode) Allegro SBCL CLISP #|others|#)
399 (map-into (make-string (length octets)) #'code-char octets))
400
401 (defun octets= (a b &key (start1 0) (end1 (length a))
402 (start2 0) (end2 (length b)))
403 (declare (type (simple-array (unsigned-byte 8) (*)) a b)
404 (type (integer 0 #.array-dimension-limit) start1 end1 start2 end2)
405 #+(or)(optimize (speed 3) (space 2) (safety 0) #-gcl (debug 0)))
406 (when (and (< start1 end1)
407 (< start2 end2))
408 (loop
409 (unless (= (aref a start1) (aref b start2)) (return nil))
410 (when (or (= (incf start1) end1) (= (incf start2) end2)) (return t)))))
411
412 (defun search-domain (octets domain pos)
413 (declare (type (simple-array (unsigned-byte 8) (*)) octets)
414 (type domain-entry domain)
415 (type list pos)
416 #+(or)(optimize (speed 3) (space 2) (safety 0) #-gcl (debug 0)
417 #+CMU (ext:inhibit-warnings 3))) ; quiet about boxing
418 (when pos
419 (let ((temp (make-array 120 :element-type '(unsigned-byte 8)))
420 (length (length octets)))
421 (with-open-file (stream (domain-entry-file domain)
422 :direction :input
423 :element-type '(unsigned-byte 8))
424 (dolist (entry pos)
425 (file-position stream (car entry))
426 (let ((off 0)
427 (end (read-sequence temp stream
428 :end (min 120 length))))
429 (declare (type (integer 0 #.array-dimension-limit) off end))
430 (loop while (octets= octets temp
431 :start1 off
432 :end1 (min (+ off 120) length)
433 :end2 end)
434 do
435 (incf off end)
436 (when (< off length)
437 (setf end (read-sequence temp stream
438 :end (min 120 (- length off))))))
439 (when (= off length)
440 (file-position stream (cdr entry))
441 (let* ((len (funcall (domain-entry-readfn domain) stream))
442 (off (funcall (domain-entry-readfn domain) stream))
443 (tmp (make-array len :element-type '(unsigned-byte 8))))
444 (file-position stream off)
445 (read-sequence tmp stream)
446 (return (values tmp entry))))))))))
447
448 (defun domain-lookup (string domain)
449 (declare (type string string) (type domain-entry domain)
450 #+(or)(optimize (speed 3) (space 2) (safety 0)))
451 (or (if (null (domain-entry-encoding domain)) string)
452 (gethash string (domain-entry-hash domain))
453 (let* ((octets (string-to-octets string
454 (domain-entry-encoding domain)))
455 (length (length octets))
456 (pos (gethash length (domain-entry-hash domain))))
457 (declare (type (simple-array (unsigned-byte 8) (*)) octets))
458 (multiple-value-bind (tmp entry) (search-domain octets domain pos)
459 (declare (type (or null (simple-array (unsigned-byte 8) (*))) tmp))
460 (when tmp
461 (let ((temp (delete entry pos :test #'eq)))
462 (if temp
463 (setf (gethash length (domain-entry-hash domain)) temp)
464 (remhash length (domain-entry-hash domain))))
465 (setf (gethash (copy-seq string) (domain-entry-hash domain))
466 (octets-to-string tmp (domain-entry-encoding domain))))))))
467
468 (defun domain-lookup-plural (singular plural domain)
469 (declare (type string singular plural) (type domain-entry domain)
470 #+(or)(optimize (speed 3) (space 2) (safety 0)))
471 (when (domain-entry-encoding domain)
472 (or (gethash (cons singular plural) (domain-entry-hash domain))
473 (let* ((octets (let* ((a (string-to-octets singular
474 (domain-entry-encoding domain)))
475 (b (string-to-octets plural
476 (domain-entry-encoding domain)))
477 (c (make-array (+ (length a) (length b) 1)
478 :element-type '(unsigned-byte 8))))
479 (declare (type (simple-array (unsigned-byte 8) (*))
480 a b c))
481 (replace c a)
482 (setf (aref c (length a)) 0)
483 (replace c b :start1 (+ (length a) 1))
484 c))
485 (length (length octets))
486 (pos (gethash length (domain-entry-hash domain))))
487 (declare (type (simple-array (unsigned-byte 8) (*)) octets)
488 (type list pos))
489 (multiple-value-bind (tmp entry) (search-domain octets domain pos)
490 (declare (type (or null (simple-array (unsigned-byte 8) (*))) tmp))
491 (when tmp
492 (prog1
493 (setf (gethash (cons (copy-seq singular) (copy-seq plural))
494 (domain-entry-hash domain))
495 (loop for i = 0 then (1+ j)
496 as j = (position 0 tmp :start i)
497 collect (octets-to-string (subseq tmp i j)
498 (domain-entry-encoding domain))
499 while j))
500 (let ((temp (delete entry pos :test #'eq)))
501 (if temp
502 (setf (gethash length (domain-entry-hash domain)) temp)
503 (remhash length (domain-entry-hash domain))))
504 (when (null (domain-entry-plurals domain))
505 (setf (domain-entry-plurals domain)
506 (parse-plurals domain))))))))))
507
508 (declaim (inline getenv)
509 (ftype (function (string) (or null string)) getenv))
510 (defun getenv (var)
511 (let ((val #+(or CMU SCL) (cdr (assoc (intern var "KEYWORD")
512 ext:*environment-list*))
513 #+SBCL (sb-ext:posix-getenv var)
514 #+Allegro (system:getenv var)
515 #+LispWorks (hcl:getenv var)
516 #+clisp (ext:getenv var)
517 #+(or openmcl mcl) (ccl::getenv var)
518 #+(or gcl ecl) (si::getenv var)))
519 (if (equal val "") nil val)))
520
521 (defun setlocale (&optional locale)
522 (setf *locale* (or locale
523 (getenv "LANGUAGE")
524 (getenv "LC_ALL")
525 (getenv "LC_MESSAGES")
526 (getenv "LANG")
527 *locale*)))
528
529 (defmacro textdomain (domain)
530 `(eval-when (:compile-toplevel :execute)
531 (setf *default-domain* ,domain)))
532
533 ;; Set the textdomain to New-Domain for the body and then restore the
534 ;; domain to the original.
535 (defmacro with-textdomain ((old-domain new-domain) &body body)
536 `(progn
537 (intl:textdomain ,new-domain)
538 ,@body
539 (intl:textdomain ,old-domain)))
540
541 (defmacro gettext (string)
542 "Look up STRING in the current message domain and return its translation."
543 `(dgettext ,*default-domain* ,string))
544
545 (defmacro ngettext (singular plural n)
546 "Look up the singular or plural form of a message in the current domain."
547 `(dngettext ,*default-domain* ,singular ,plural ,n))
548
549 (declaim (inline dgettext))
550 (defun dgettext (domain string)
551 "Look up STRING in the specified message domain and return its translation."
552 #+(or)(declare (optimize (speed 3) (space 2) (safety 0)))
553 (let ((domain (and domain (find-domain domain *locale*))))
554 (or (and domain (domain-lookup string domain)) string)))
555
556 (defun dngettext (domain singular plural n)
557 "Look up the singular or plural form of a message in the specified domain."
558 (declare (type integer n)
559 #+(or)(optimize (speed 3) (space 2) (safety 0)))
560 (let* ((domain (and domain (find-domain domain *locale*)))
561 (list (and domain (domain-lookup-plural singular plural domain))))
562 (if list
563 (nth (the integer
564 (funcall (the function (domain-entry-plurals domain)) n))
565 list)
566 (if (= n 1) singular plural))))
567
568 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
569
570 #-runtime
571 (defvar *translator-comment* nil)
572
573 #-runtime
574 (defvar *translations* nil)
575
576 #-runtime
577 (defun translation-enable ()
578 (setq *translations* (or *translations* (make-hash-table :test 'equal)))
579 t)
580
581 #-runtime
582 (defun translation-disable ()
583 (setq *translations* nil))
584
585 #-runtime
586 (defun note-translatable (domain string &optional plural)
587 (when (and domain *translations*)
588 (let* ((hash (or (gethash domain *translations*)
589 (setf (gethash domain *translations*)
590 (make-hash-table :test 'equal))))
591 (key (if plural (cons string plural) string))
592 (val (or (gethash key hash) (cons nil nil))))
593 (pushnew *translator-comment* (car val) :test #'equal)
594 (pushnew (and *compile-file-truename* (enough-namestring *compile-file-truename*))
595 (cdr val) :test #'equal)
596 ;; FIXME: How does this happen? Need to figure this out and get
597 ;; rid of this!
598 (unless key
599 (warn "Translate error with null key. domain = ~S string = ~S~%"
600 domain string))
601 (setf (gethash key hash) val)))
602 (setq *translator-comment* nil))
603
604 (define-compiler-macro dgettext (&whole form domain string)
605 #-runtime
606 (when (and (stringp domain) (stringp string))
607 (note-translatable domain string))
608 form)
609
610 (define-compiler-macro dngettext (&whole form domain singular plural n)
611 (declare (ignore n))
612 #-runtime
613 (when (and (stringp domain) (stringp singular) (stringp plural))
614 (note-translatable domain singular plural))
615 form)
616
617 (defun read-translatable-string (stream char)
618 (declare (ignore char))
619 (case (peek-char nil stream nil nil t)
620 (#\" (let* ((*read-suppress* nil)
621 (string (read stream t nil t)))
622 (note-translatable *default-domain* string)
623 `(gettext ,string)))
624 (#\N (read-char stream t nil t)
625 (let* ((*read-suppress* nil)
626 (string (read stream t nil t)))
627 #-runtime
628 (note-translatable *default-domain* string)
629 string))
630 (#\@ (error _"_@ is a reserved reader macro prefix."))
631 (otherwise
632 (let ((fn (get-macro-character #\_ nil)))
633 (if fn (funcall fn stream #\_) '_)))))
634
635 ;; Process comments as usual, but look for lines that begin with
636 ;; "TRANSLATORS: ". These lines are saved and written out as a
637 ;; translator comment for the next translatable string.
638 #-runtime
639 (defun read-comment (stream char)
640 (declare (optimize (speed 0) (space 3) #-gcl (debug 0))
641 (ignore char))
642 (do ((state 0)
643 (index 0)
644 (text nil)
645 (char (read-char stream nil nil t) (read-char stream nil nil t)))
646 ((or (not char) (char= char #\Newline))
647 (when text (setq *translator-comment* (copy-seq text))))
648 (cond ((and (= state 0) (char= char #\Space)) (setq state 1))
649 ((and (= state 0) (char= char #\T)) (setq state 1 index 1))
650 ((and (= state 0) (char/= char #\;)) (setq state 2))
651 ((and (= state 1) (= index 0) (char= char #\Space)) #|ignore|#)
652 ((= state 1)
653 (if (char= char (char "TRANSLATORS: " index))
654 (when (= (incf index) 13)
655 (setq state 3))
656 (setq state 2)))
657 ((= state 3)
658 (when (null text)
659 (setq text (make-array 50 :element-type 'character
660 :adjustable t :fill-pointer 0)))
661 (vector-push-extend char text))))
662 (values))
663
664 #-runtime
665 (defun read-nested-comment (stream subchar arg)
666 (declare (ignore subchar arg)
667 (optimize (speed 0) (space 3) #-gcl (debug 0)))
668 (do ((level 1)
669 (state 0)
670 (index 0)
671 (text nil)
672 (prev (read-char stream t nil t) char)
673 (char (read-char stream t nil t) (read-char stream t nil t)))
674 (())
675 (cond ((and (char= prev #\|) (char= char #\#))
676 (when (zerop (decf level))
677 (when text
678 (setq *translator-comment*
679 (string-right-trim '(#\Space #\Newline) text)))
680 (return)))
681 ((and (char= prev #\#) (char= char #\|))
682 (setq state 2)
683 (incf level))
684 ((and (= state 0) (char= prev #\Space)) (setq state 1))
685 ((and (= state 0) (char= prev #\T))
686 (setq state 1 index 1))
687 ((= state 0) (setq state 2))
688 ((and (= state 1) (= index 0) (char= prev #\Space)) #| ignore |#)
689 ((= state 1)
690 (if (char= prev (char "TRANSLATORS: " index))
691 (when (= (incf index) 13)
692 (setq state 3))
693 (setq state 2)))
694 ((= state 3)
695 (when (null text)
696 (setq text (make-array 50 :element-type 'character
697 :adjustable t :fill-pointer 0)))
698 (vector-push-extend prev text))))
699 (values))
700
701 (defun install (&optional (rt *readtable*))
702 (set-macro-character #\_ #'read-translatable-string t rt)
703 #-runtime
704 (set-macro-character #\; #'read-comment nil rt)
705 #-runtime
706 (set-dispatch-macro-character #\# #\| #'read-nested-comment rt)
707 t)
708
709
710 ;; Dump the translatable strings. The output is written to a file in
711 ;; the directory OUTPUT-DIRECTORY and its name is the domain.
712 #-runtime
713 (defun dump-pot-files (&key copyright output-directory)
714 ;;(declare (optimize (speed 0) (space 3) #-gcl (debug 1)))
715 (labels ((b (key data)
716 (format t "~@[~{~&#. ~A~}~%~]" (delete nil (car data)))
717 (format t "~@[~&~<#: ~@;~@{~A~^ ~}~:@>~%~]"
718 (delete nil (cdr data)))
719 (cond ((consp key)
720 (format t "~&msgid ") (str (car key) 6 0)
721 (format t "~&msgid_plural ") (str (cdr key) 13 0)
722 (format t "~&msgstr[0] \"\"~2%"))
723 (t
724 (cond
725 (key
726 (format t "~&msgid ") (str key 6 0)
727 (format t "~&msgstr \"\"~2%"))
728 (t
729 (format *error-output* "Skipping NIL key~%"))))))
730 (str (string col start)
731 (when (and (plusp col) (> (length string) (- 76 col)))
732 (format t "\"\"~%"))
733 (let ((nl (position #\Newline string :start start)))
734 (cond ((and nl (< (- nl start) 76))
735 (write-char #\")
736 (wstr string start nl)
737 (format t "\\n\"~%")
738 (str string 0 (1+ nl)))
739 ((< (- (length string) start) 76)
740 (write-char #\")
741 (wstr string start (length string))
742 (write-char #\"))
743 (t
744 (let* ((a (+ start 1))
745 (b (+ start 76))
746 (b1 (position #\Space string :start a :end b
747 :from-end t))
748 (b2 (position-if (lambda (x)
749 (position x ";:,?!)]}"))
750 string :start a :end b
751 :from-end t))
752 (b3 (position-if (lambda (x)
753 (position x "\"'-"))
754 string :start a :end b
755 :from-end t))
756 (b4 (position-if #'digit-char-p
757 string :start a :end b
758 :from-end t))
759 (b5 (position-if #'alpha-char-p
760 string :start a :end b
761 :from-end t))
762 (g1 (if b1 (* (- b b1) (- b b1) .03) 10000))
763 (g2 (if b2 (* (- b b2) (- b b2) .20) 10000))
764 (g3 (if b3 (* (- b b3) (- b b3) .97) 10000))
765 (g4 (if b4 (* (- b b4) (- b b4) 1.3) 10000))
766 (g5 (if b5 (* (- b b5) (- b b5) 2.0) 10000))
767 (g (min g1 g2 g3 g4 g5))
768 (end (1+ (cond ((> g 750) b)
769 ((= g g1) b1)
770 ((= g g2) b2)
771 ((= g g3) b3)
772 ((= g g4) b4)
773 ((= g g5) b5)))))
774 #+(or)
775 (progn
776 (format t "~&Splitting ~S:~%"
777 (subseq string start b))
778 (format t "~{~& b~D=~D; goodness=~F~}~%"
779 (list 1 b1 g1 2 b2 g2 3 b3 g3 4 b4 g4 5 b5 g5
780 6 b 10000))
781 (format t "~& best=~F == ~D~%" g end)
782 (format t "~& Part1=~S~% Part2=~S~%"
783 (subseq string start end)
784 (subseq string end b)))
785 (write-char #\")
786 (wstr string start end)
787 (write-char #\") (terpri)
788 (str string 0 end))))))
789 (wstr (string start end)
790 (loop while (< start end) do
791 (let ((i (position-if (lambda (x)
792 (or (char= x #\") (char= x #\\)))
793 string :start start :end end)))
794 (write-string string nil :start start :end (or i end))
795 (when i (write-char #\\ nil) (write-char (char string i) nil))
796 (setq start (if i (1+ i) end)))))
797 (a (domain hash)
798 (format t _"~&Dumping ~D messages for domain ~S~%"
799 (hash-table-count hash) domain)
800 (with-open-file (*standard-output*
801 (merge-pathnames (make-pathname :name domain
802 :type "pot")
803 output-directory)
804 :direction :output
805 :if-exists :new-version
806 ;;:external-format :utf8
807 :external-format :iso8859-1
808 )
809 (format t "~&#@ ~A~2%" domain)
810 (format t "~&# SOME DESCRIPTIVE TITLE~%")
811 (format t "~@[~&# Copyright (C) YEAR ~A~%~]" copyright)
812 (format t "~&# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR~%")
813 (format t "~&#~%#, fuzzy~%msgid \"\"~%msgstr \"\"~%")
814 (format t "~&\"Project-Id-Version: PACKAGE VERSION\\n\"~%")
815 (format t "~&\"Report-Msgid-Bugs-To: \\n\"~%")
816 (format t "~&\"PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\\n\"~%")
817 (format t "~&\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"~%")
818 (format t "~&\"Language-Team: LANGUAGE <LL@li.org>\\n\"~%")
819 (format t "~&\"MIME-Version: 1.0\\n\"~%")
820 (format t "~&\"Content-Type: text/plain; charset=UTF-8\\n\"~%")
821 (format t "~&\"Content-Transfer-Encoding: 8bit\\n\"~2%")
822 (maphash #'b hash))))
823 (maphash #'a *translations*)
824 #+(or)
825 (clrhash *translations*))
826 nil)
827
828
829
830 (eval-when (:compile-toplevel :execute)
831 (setq *default-domain* nil)
832 (unless (and (fboundp 'intl:read-translatable-string)
833 (eq (get-macro-character #\_)
834 (fdefinition 'intl:read-translatable-string)))
835 (set-syntax-from-char #\_ #\_)))
836
837 ;; Don't install the reader macros by default.
838 #+(or)
839 (install)

  ViewVC Help
Powered by ViewVC 1.1.5