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

Contents of /src/code/intl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Wed Apr 14 16:39:52 2010 UTC (4 years ago) by rtoy
Branch: MAIN
Changes since 1.2: +10 -9 lines
code/intl.lisp:
o When saving the file name, use *compile-file-truename* instead of
  *compile-file-pathname*.  Also use enough-namestring to make a
  shorter name.  The file names that were placed in the pot file
  included search-lists which aren't understood by anything besides
  CMUCL.
o Don't install the reader macros by default anymore.
o Change INSTALL to accept an optional readtable that is modified with
  the reader macros and such.

code/exports.lisp:
o Export INTL::INSTALL.

tools/build-world.sh:
tools/build-utils.sh:
o Need to install the reader macros when building so we can get
  generate the pot files.
o Set *default-pathname-defaults* to be the (full) current build
  directory so that the pathnames in the pot file are relative to the
  build directory.

These changes fix the main part of Ticket #39.
1 ;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Package: INTL -*-
2
3 ;;; $Revision: 1.3 $
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.3 2010/04/14 16:39:52 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 _N"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 _"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 _"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 _"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 _"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 _"Unexpected token: ~S." tok)))))
292 (multiple-value-bind (tree end) (conditional (next))
293 (unless (eq end 'END)
294 (error _"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 (return-from load-domain nil))
325 (with-open-file (stream file :direction :input :if-does-not-exist nil
326 :element-type '(unsigned-byte 8))
327 (unless stream (return-from load-domain nil))
328 (let ((magic (read-lelong stream)))
329 (cond ((= magic #x950412de) (setq read #'read-lelong))
330 ((= magic #xde120495) (setq read #'read-belong))
331 (t
332 ;; DON'T translate this! If we can't load the domain,
333 ;; we can't print this message, Which causes an error
334 ;; that causes use to do a domain lookup again, which
335 ;; fails which cause an error message which ...
336 (warn "Bad magic number in \"~A.mo\"." domain)
337 (return-from load-domain nil))))
338 (let ((version (funcall read stream))
339 (messages (funcall read stream))
340 (master (funcall read stream))
341 (translation (funcall read stream))
342 (entry (make-domain-entry)))
343 (declare (ignore version))
344 (setf (domain-entry-readfn entry) read)
345 (setf (domain-entry-domain entry) domain)
346 (setf (domain-entry-locale entry) locale)
347 (setf (domain-entry-file entry) file)
348 (dotimes (msg messages)
349 (file-position stream (+ master (* 8 msg)))
350 (let ((length (funcall read stream))
351 (start (funcall read stream)))
352 (setf (gethash length (domain-entry-hash entry))
353 (acons start (+ translation (* 8 msg))
354 (gethash length (domain-entry-hash entry))))))
355 (setf (gethash domain *loaded-domains*) entry)
356 (find-encoding entry)))))
357
358 (defun find-domain (domain locale &optional (locale-dir *locale-directories*))
359 (let ((found (gethash domain *loaded-domains*)))
360 (if (and found (string= (domain-entry-locale found) locale))
361 found
362 (load-domain domain locale locale-dir))))
363
364 (declaim (inline string-to-octets))
365 (defun string-to-octets (string encoding)
366 (declare (ignorable encoding))
367 #+(and CMU Unicode)
368 (ext:string-to-octets string :external-format encoding)
369 #+Allegro
370 (excl:string-to-octets string :external-format encoding :null-terminate nil)
371 #+SBCL
372 (sb-ext:string-to-octets string :external-format encoding
373 :null-terminate nil)
374 #+CLISP ;;@@ Not sure if encoding keyword is OK here
375 (ext:convert-string-to-bytes string encoding)
376 ;;@@ add other implementations
377 #-(or (and CMU Unicode) Allegro SBCL CLISP #|others|#)
378 (map-into (make-array (length string) :element-type '(unsigned-byte 8))
379 #'char-code string))
380
381 (declaim (inline octets-to-string))
382 (defun octets-to-string (octets encoding)
383 (declare (ignorable encoding))
384 #+(and CMU Unicode)
385 (ext:octets-to-string octets :external-format encoding)
386 #+Allegro
387 (excl:octets-to-string octets :external-format encoding :end (length octets))
388 #+SBCL
389 (sb-ext:octets-to-string octets :external-format encoding)
390 #+CLISP ;;@@ Not sure if encoding keyword is OK here
391 (ext:convert-string-from-bytes octets encoding)
392 ;;@@ add other implementations
393 #-(or (and CMU Unicode) Allegro SBCL CLISP #|others|#)
394 (map-into (make-string (length octets)) #'code-char octets))
395
396 (defun octets= (a b &key (start1 0) (end1 (length a))
397 (start2 0) (end2 (length b)))
398 (declare (type (simple-array (unsigned-byte 8) (*)) a b)
399 (type (integer 0 #.array-dimension-limit) start1 end1 start2 end2)
400 #+(or)(optimize (speed 3) (space 2) (safety 0) #-gcl (debug 0)))
401 (when (and (< start1 end1)
402 (< start2 end2))
403 (loop
404 (unless (= (aref a start1) (aref b start2)) (return nil))
405 (when (or (= (incf start1) end1) (= (incf start2) end2)) (return t)))))
406
407 (defun search-domain (octets domain pos)
408 (declare (type (simple-array (unsigned-byte 8) (*)) octets)
409 (type domain-entry domain)
410 (type list pos)
411 #+(or)(optimize (speed 3) (space 2) (safety 0) #-gcl (debug 0)
412 #+CMU (ext:inhibit-warnings 3))) ; quiet about boxing
413 (when pos
414 (let ((temp (make-array 120 :element-type '(unsigned-byte 8)))
415 (length (length octets)))
416 (with-open-file (stream (domain-entry-file domain)
417 :direction :input
418 :element-type '(unsigned-byte 8))
419 (dolist (entry pos)
420 (file-position stream (car entry))
421 (let ((off 0)
422 (end (read-sequence temp stream
423 :end (min 120 length))))
424 (declare (type (integer 0 #.array-dimension-limit) off end))
425 (loop while (octets= octets temp
426 :start1 off
427 :end1 (min (+ off 120) length)
428 :end2 end)
429 do
430 (incf off end)
431 (when (< off length)
432 (setf end (read-sequence temp stream
433 :end (min 120 (- length off))))))
434 (when (= off length)
435 (file-position stream (cdr entry))
436 (let* ((len (funcall (domain-entry-readfn domain) stream))
437 (off (funcall (domain-entry-readfn domain) stream))
438 (tmp (make-array len :element-type '(unsigned-byte 8))))
439 (file-position stream off)
440 (read-sequence tmp stream)
441 (return (values tmp entry))))))))))
442
443 (defun domain-lookup (string domain)
444 (declare (type string string) (type domain-entry domain)
445 #+(or)(optimize (speed 3) (space 2) (safety 0)))
446 (or (if (null (domain-entry-encoding domain)) string)
447 (gethash string (domain-entry-hash domain))
448 (let* ((octets (string-to-octets string
449 (domain-entry-encoding domain)))
450 (length (length octets))
451 (pos (gethash length (domain-entry-hash domain))))
452 (declare (type (simple-array (unsigned-byte 8) (*)) octets))
453 (multiple-value-bind (tmp entry) (search-domain octets domain pos)
454 (declare (type (or null (simple-array (unsigned-byte 8) (*))) tmp))
455 (when tmp
456 (let ((temp (delete entry pos :test #'eq)))
457 (if temp
458 (setf (gethash length (domain-entry-hash domain)) temp)
459 (remhash length (domain-entry-hash domain))))
460 (setf (gethash (copy-seq string) (domain-entry-hash domain))
461 (octets-to-string tmp (domain-entry-encoding domain))))))))
462
463 (defun domain-lookup-plural (singular plural domain)
464 (declare (type string singular plural) (type domain-entry domain)
465 #+(or)(optimize (speed 3) (space 2) (safety 0)))
466 (or (if (null (domain-entry-encoding domain)) nil)
467 (gethash (cons singular plural) (domain-entry-hash domain))
468 (let* ((octets (let* ((a (string-to-octets singular
469 (domain-entry-encoding domain)))
470 (b (string-to-octets plural
471 (domain-entry-encoding domain)))
472 (c (make-array (+ (length a) (length b) 1)
473 :element-type '(unsigned-byte 8))))
474 (declare (type (simple-array (unsigned-byte 8) (*))
475 a b c))
476 (replace c a)
477 (setf (aref c (length a)) 0)
478 (replace c b :start1 (+ (length a) 1))
479 c))
480 (length (length octets))
481 (pos (gethash length (domain-entry-hash domain))))
482 (declare (type (simple-array (unsigned-byte 8) (*)) octets)
483 (type list pos))
484 (multiple-value-bind (tmp entry) (search-domain octets domain pos)
485 (declare (type (or null (simple-array (unsigned-byte 8) (*))) tmp))
486 (when tmp
487 (prog1
488 (setf (gethash (cons (copy-seq singular) (copy-seq plural))
489 (domain-entry-hash domain))
490 (loop for i = 0 then (1+ j)
491 as j = (position 0 tmp :start i)
492 collect (octets-to-string (subseq tmp i j)
493 (domain-entry-encoding domain))
494 while j))
495 (let ((temp (delete entry pos :test #'eq)))
496 (if temp
497 (setf (gethash length (domain-entry-hash domain)) temp)
498 (remhash length (domain-entry-hash domain))))
499 (when (null (domain-entry-plurals domain))
500 (setf (domain-entry-plurals domain)
501 (parse-plurals domain)))))))))
502
503 (declaim (inline getenv)
504 (ftype (function (string) (or null string)) getenv))
505 (defun getenv (var)
506 (let ((val #+(or CMU SCL) (cdr (assoc (intern var "KEYWORD")
507 ext:*environment-list*))
508 #+SBCL (sb-ext:posix-getenv var)
509 #+Allegro (system:getenv var)
510 #+LispWorks (hcl:getenv var)
511 #+clisp (ext:getenv var)
512 #+(or openmcl mcl) (ccl::getenv var)
513 #+(or gcl ecl) (si::getenv var)))
514 (if (equal val "") nil val)))
515
516 (defun setlocale (&optional locale)
517 (setf *locale* (or locale
518 (getenv "LANGUAGE")
519 (getenv "LC_ALL")
520 (getenv "LC_MESSAGES")
521 (getenv "LANG")
522 *locale*)))
523
524 (defmacro textdomain (domain)
525 `(eval-when (:compile-toplevel :execute)
526 (setf *default-domain* ,domain)))
527
528 (defmacro gettext (string)
529 _N"Look up STRING in the current message domain and return its translation."
530 `(dgettext ,*default-domain* ,string))
531
532 (defmacro ngettext (singular plural n)
533 _N"Look up the singular or plural form of a message in the current domain."
534 `(dngettext ,*default-domain* ,singular ,plural ,n))
535
536 (declaim (inline dgettext))
537 (defun dgettext (domain string)
538 _N"Look up STRING in the specified message domain and return its translation."
539 #+(or)(declare (optimize (speed 3) (space 2) (safety 0)))
540 (let ((domain (and domain (find-domain domain *locale*))))
541 (or (and domain (domain-lookup string domain)) string)))
542
543 (defun dngettext (domain singular plural n)
544 _N"Look up the singular or plural form of a message in the specified domain."
545 (declare (type integer n)
546 #+(or)(optimize (speed 3) (space 2) (safety 0)))
547 (let* ((domain (and domain (find-domain domain *locale*)))
548 (list (and domain (domain-lookup-plural singular plural domain))))
549 (if list
550 (nth (the integer
551 (funcall (the function (domain-entry-plurals domain)) n))
552 list)
553 (if (= n 1) singular plural))))
554
555 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
556
557 #-runtime
558 (defvar *translator-comment* nil)
559
560 #-runtime
561 (defvar *translations* (make-hash-table :test 'equal))
562
563 #-runtime
564 (defun note-translatable (domain string &optional plural)
565 (when domain
566 (let* ((hash (or (gethash domain *translations*)
567 (setf (gethash domain *translations*)
568 (make-hash-table :test 'equal))))
569 (key (if plural (cons string plural) string))
570 (val (or (gethash key hash) (cons nil nil))))
571 (pushnew *translator-comment* (car val) :test #'equal)
572 (pushnew (enough-namestring *compile-file-truename*) (cdr val) :test #'equal)
573 ;; FIXME: How does this happen? Need to figure this out and get
574 ;; rid of this!
575 (unless key
576 (warn "Translate error with null key. domain = ~S string = ~S~%"
577 domain string))
578 (setf (gethash key hash) val)))
579 (setq *translator-comment* nil))
580
581 (define-compiler-macro dgettext (&whole form domain string)
582 #-runtime
583 (when (and (stringp domain) (stringp string))
584 (note-translatable domain string))
585 form)
586
587 (define-compiler-macro dngettext (&whole form domain singular plural n)
588 (declare (ignore n))
589 #-runtime
590 (when (and (stringp domain) (stringp singular) (stringp plural))
591 (note-translatable domain singular plural))
592 form)
593
594 (defun read-translatable-string (stream char)
595 (declare (ignore char))
596 (case (peek-char nil stream nil nil t)
597 (#\" (let* ((*read-suppress* nil)
598 (string (read stream t nil t)))
599 (note-translatable *default-domain* string)
600 `(gettext ,string)))
601 (#\N (read-char stream t nil t)
602 (let* ((*read-suppress* nil)
603 (string (read stream t nil t)))
604 #-runtime
605 (note-translatable *default-domain* string)
606 string))
607 (#\@ (error _"_@ is a reserved reader macro prefix."))
608 (otherwise
609 (let ((fn (get-macro-character #\_ nil)))
610 (if fn (funcall fn stream #\_) '_)))))
611
612 ;; Process comments as usual, but look for lines that begin with
613 ;; "TRANSLATORS: ". These lines are saved and written out as a
614 ;; translator comment for the next translatable string.
615 #-runtime
616 (defun read-comment (stream char)
617 (declare (optimize (speed 0) (space 3) #-gcl (debug 0))
618 (ignore char))
619 (do ((state 0)
620 (index 0)
621 (text nil)
622 (char (read-char stream nil nil t) (read-char stream nil nil t)))
623 ((or (not char) (char= char #\Newline))
624 (when text (setq *translator-comment* (copy-seq text))))
625 (cond ((and (= state 0) (char= char #\Space)) (setq state 1))
626 ((and (= state 0) (char= char #\T)) (setq state 1 index 1))
627 ((and (= state 0) (char/= char #\;)) (setq state 2))
628 ((and (= state 1) (= index 0) (char= char #\Space)) #|ignore|#)
629 ((= state 1)
630 (if (char= char (char "TRANSLATORS: " index))
631 (when (= (incf index) 13)
632 (setq state 3))
633 (setq state 2)))
634 ((= state 3)
635 (when (null text)
636 (setq text (make-array 50 :element-type 'character
637 :adjustable t :fill-pointer 0)))
638 (vector-push-extend char text))))
639 (values))
640
641 #-runtime
642 (defun read-nested-comment (stream subchar arg)
643 (declare (ignore subchar arg)
644 (optimize (speed 0) (space 3) #-gcl (debug 0)))
645 (do ((level 1)
646 (state 0)
647 (index 0)
648 (text nil)
649 (prev (read-char stream t nil t) char)
650 (char (read-char stream t nil t) (read-char stream t nil t)))
651 (())
652 (cond ((and (char= prev #\|) (char= char #\#))
653 (when (zerop (decf level))
654 (when text
655 (setq *translator-comment*
656 (string-right-trim '(#\Space #\Newline) text)))
657 (return)))
658 ((and (char= prev #\#) (char= char #\|))
659 (setq state 2)
660 (incf level))
661 ((and (= state 0) (char= prev #\Space)) (setq state 1))
662 ((and (= state 0) (char= prev #\T))
663 (setq state 1 index 1))
664 ((= state 0) (setq state 2))
665 ((and (= state 1) (= index 0) (char= prev #\Space)) #| ignore |#)
666 ((= state 1)
667 (if (char= prev (char "TRANSLATORS: " index))
668 (when (= (incf index) 13)
669 (setq state 3))
670 (setq state 2)))
671 ((= state 3)
672 (when (null text)
673 (setq text (make-array 50 :element-type 'character
674 :adjustable t :fill-pointer 0)))
675 (vector-push-extend prev text))))
676 (values))
677
678 (defun install (&optional (rt *readtable*))
679 (set-macro-character #\_ #'read-translatable-string t rt)
680 #-runtime
681 (set-macro-character #\; #'read-comment nil rt)
682 #-runtime
683 (set-dispatch-macro-character #\# #\| #'read-nested-comment rt)
684 t)
685
686
687 ;; Dump the translatable strings. The output is written to a file in
688 ;; the directory OUTPUT-DIRECTORY and its name is the domain.
689 #-runtime
690 (defun dump-pot-files (&key copyright output-directory)
691 ;;(declare (optimize (speed 0) (space 3) #-gcl (debug 1)))
692 (labels ((b (key data)
693 (format t "~@[~{~&#. ~A~}~%~]" (delete nil (car data)))
694 (format t "~@[~&~<#: ~@;~@{~A~^ ~}~:@>~%~]"
695 (delete nil (cdr data)))
696 (cond ((consp key)
697 (format t "~&msgid ") (str (car key) 6 0)
698 (format t "~&msgid_plural ") (str (cdr key) 13 0)
699 (format t "~&msgstr[0] \"\"~2%"))
700 (t
701 (cond
702 (key
703 (format t "~&msgid ") (str key 6 0)
704 (format t "~&msgstr \"\"~2%"))
705 (t
706 (format *error-output* "Skipping NIL key~%"))))))
707 (str (string col start)
708 (when (and (plusp col) (> (length string) (- 76 col)))
709 (format t "\"\"~%"))
710 (let ((nl (position #\Newline string :start start)))
711 (cond ((and nl (< (- nl start) 76))
712 (write-char #\")
713 (wstr string start nl)
714 (format t "\\n\"~%")
715 (str string 0 (1+ nl)))
716 ((< (- (length string) start) 76)
717 (write-char #\")
718 (wstr string start (length string))
719 (write-char #\"))
720 (t
721 (let* ((a (+ start 1))
722 (b (+ start 76))
723 (b1 (position #\Space string :start a :end b
724 :from-end t))
725 (b2 (position-if (lambda (x)
726 (position x ";:,?!)]}"))
727 string :start a :end b
728 :from-end t))
729 (b3 (position-if (lambda (x)
730 (position x "\"'-"))
731 string :start a :end b
732 :from-end t))
733 (b4 (position-if #'digit-char-p
734 string :start a :end b
735 :from-end t))
736 (b5 (position-if #'alpha-char-p
737 string :start a :end b
738 :from-end t))
739 (g1 (if b1 (* (- b b1) (- b b1) .03) 10000))
740 (g2 (if b2 (* (- b b2) (- b b2) .20) 10000))
741 (g3 (if b3 (* (- b b3) (- b b3) .97) 10000))
742 (g4 (if b4 (* (- b b4) (- b b4) 1.3) 10000))
743 (g5 (if b5 (* (- b b5) (- b b5) 2.0) 10000))
744 (g (min g1 g2 g3 g4 g5))
745 (end (1+ (cond ((> g 750) b)
746 ((= g g1) b1)
747 ((= g g2) b2)
748 ((= g g3) b3)
749 ((= g g4) b4)
750 ((= g g5) b5)))))
751 #+(or)
752 (progn
753 (format t "~&Splitting ~S:~%"
754 (subseq string start b))
755 (format t "~{~& b~D=~D; goodness=~F~}~%"
756 (list 1 b1 g1 2 b2 g2 3 b3 g3 4 b4 g4 5 b5 g5
757 6 b 10000))
758 (format t "~& best=~F == ~D~%" g end)
759 (format t "~& Part1=~S~% Part2=~S~%"
760 (subseq string start end)
761 (subseq string end b)))
762 (write-char #\")
763 (wstr string start end)
764 (write-char #\") (terpri)
765 (str string 0 end))))))
766 (wstr (string start end)
767 (loop while (< start end) do
768 (let ((i (position-if (lambda (x)
769 (or (char= x #\") (char= x #\\)))
770 string :start start :end end)))
771 (write-string string nil :start start :end (or i end))
772 (when i (write-char #\\ nil) (write-char (char string i) nil))
773 (setq start (if i (1+ i) end)))))
774 (a (domain hash)
775 (format t _"~&Dumping ~D messages for domain ~S~%"
776 (hash-table-count hash) domain)
777 (with-open-file (*standard-output*
778 (merge-pathnames (make-pathname :name domain
779 :type "pot")
780 output-directory)
781 :direction :output
782 :if-exists :new-version
783 ;;:external-format :utf8
784 :external-format :iso8859-1
785 )
786 (format t "~&#@ ~A~2%" domain)
787 (format t "~&# SOME DESCRIPTIVE TITLE~%")
788 (format t "~@[~&# Copyright (C) YEAR ~A~%~]" copyright)
789 (format t "~&# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR~%")
790 (format t "~&#~%#, fuzzy~%msgid \"\"~%msgstr \"\"~%")
791 (format t "~&\"Project-Id-Version: PACKAGE VERSION\\n\"~%")
792 (format t "~&\"Report-Msgid-Bugs-To: \\n\"~%")
793 (format t "~&\"PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\\n\"~%")
794 (format t "~&\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"~%")
795 (format t "~&\"Language-Team: LANGUAGE <LL@li.org>\\n\"~%")
796 (format t "~&\"MIME-Version: 1.0\\n\"~%")
797 (format t "~&\"Content-Type: text/plain; charset=UTF-8\\n\"~%")
798 (format t "~&\"Content-Transfer-Encoding: 8bit\\n\"~2%")
799 (maphash #'b hash))))
800 (maphash #'a *translations*)
801 #+(or)
802 (clrhash *translations*))
803 nil)
804
805
806
807 (eval-when (:compile-toplevel :execute)
808 (setq *default-domain* nil)
809 (unless (and (fboundp 'intl:read-translatable-string)
810 (eq (get-macro-character #\_)
811 (fdefinition 'intl:read-translatable-string)))
812 (set-syntax-from-char #\_ #\_)))
813
814 ;; Don't install the reader macros by default.
815 #+(or)
816 (install)

  ViewVC Help
Powered by ViewVC 1.1.5