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

Contents of /src/code/intl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.2.7 - (show annotations)
Tue Feb 9 23:40:35 2010 UTC (4 years, 2 months ago) by rtoy
Branch: intl-branch
Changes since 1.1.2.6: +24 -18 lines
Make the search for the domain file if search lists aren't initialized
yet, since the default locale directories includes search lists.  This
prevents the search from failing if we need to print out an error
message early in the build/load process.

Could this be done in a better/different way?

code/save.lisp:
o Define *ENVIRONMENT-LIST-INITIALIZED*
o Set *ENVIRONMENT-LIST-INITIALIZED* to T after ENVIRONMENT-INIT has
  initialized everything.

code/intl.lisp:
o Make LOCATE-DOMAIN-FILE return NIL if the search lists aren't set up
  yet.  This allows translated strings to just use the non-translated
  version.

bootfiles/20a/boot-2010-02-1.lisp:
o Bootstrap *ENVIRONMENT-LIST-INITIALIZED*
1 ;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Package: INTL -*-
2
3 ;;; $Revision: 1.1.2.7 $
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.1.2.7 2010/02/09 23:40:35 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 (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 (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 (let* ((header (domain-lookup "" domain))
110 (ctype (search "Content-Type: " header))
111 (eoln (and ctype (position #\Newline header :start ctype)))
112 (charset (and ctype (search "; charset=" header
113 :start2 ctype :end2 eoln))))
114 (when charset
115 (incf charset 10)
116 (loop for i upfrom charset below eoln as c = (char header i)
117 while (or (alphanumericp c) (eql c #\-))
118 finally (setf (domain-entry-encoding domain)
119 (intern (nstring-upcase (subseq header charset i))
120 "KEYWORD"))))))
121 domain)
122
123 (defun parse-plurals (domain)
124 (let* ((header (domain-lookup "" domain))
125 (plurals (search "Plural-Forms: " header))
126 (default (lambda (n) (if (= n 1) 0 1))))
127 (if (and plurals
128 (> (length header) (+ plurals 36))
129 (string= header "nplurals="
130 :start1 (+ plurals 14) :end1 (+ plurals 23)))
131 (let ((nplurals
132 (parse-integer header :start (+ plurals 23) :junk-allowed t))
133 (point (+ (position #\; header :start (+ plurals 23)) 2)))
134 (if (and (> (length header) (+ point 10))
135 (string= header "plural=" :start1 point :end1 (+ point 7)))
136 (values (parse-expr header (+ point 7)) nplurals)
137 (values default 2)))
138 (values default 2))))
139
140 (defun parse-expr (string pos)
141 (labels ((next ()
142 (loop while (member (char string pos) '(#\Space #\Tab #\Newline))
143 do (incf pos))
144 (case (char string (1- (incf pos)))
145 (#\n 'n)
146 (#\? 'IF)
147 (#\: 'THEN)
148 (#\( 'LPAR)
149 (#\) 'RPAR)
150 (#\^ 'LOGXOR)
151 (#\+ 'ADD)
152 (#\- 'SUB)
153 (#\* 'MUL)
154 (#\/ 'FLOOR)
155 (#\% 'MOD)
156 (#\~ 'LOGNOT32)
157 (#\; 'END)
158 (#\| (if (char= (char string pos) #\|)
159 (progn (incf pos) 'COR)
160 'LOGIOR))
161 (#\& (if (char= (char string pos) #\&)
162 (progn (incf pos) 'CAND)
163 'LOGAND))
164 (#\= (if (char= (char string pos) #\=)
165 (progn (incf pos) 'CMP=)
166 (error _"Encountered illegal token: =")))
167 (#\! (if (char= (char string pos) #\=)
168 (progn (incf pos) 'CMP/=)
169 'NOT))
170 (#\< (case (char string pos)
171 (#\= (incf pos) 'CMP<=)
172 (#\< (incf pos) 'SHL)
173 (otherwise 'CMP<)))
174 (#\> (case (char string pos)
175 (#\= (incf pos) 'CMP>=)
176 (#\> (incf pos) 'SHR)
177 (otherwise 'CMP>)))
178 (otherwise (let ((n (digit-char-p (char string (1- pos)))))
179 (if n
180 (loop for nx = (digit-char-p (char string pos))
181 while nx
182 do (setq n (+ (* n 10) nx)) (incf pos)
183 finally (return n))
184 (error _"Encountered illegal token: ~C"
185 (char string (1- pos))))))))
186 (conditional (tok &aux tree)
187 (multiple-value-setq (tree tok) (logical-or tok))
188 (when (eql tok 'IF)
189 (multiple-value-bind (right next) (logical-or (next))
190 (unless (eql next 'THEN)
191 (error _"Expected : in ?: construct"))
192 (multiple-value-bind (else next) (conditional (next))
193 (setq tree (list tok (list 'zerop tree) else right)
194 tok next))))
195 (values tree tok))
196 (logical-or (tok &aux tree)
197 (multiple-value-setq (tree tok) (logical-and tok))
198 (loop while (eql tok 'COR) do
199 (multiple-value-bind (right next) (logical-and (next))
200 (setq tree (list tok tree right)
201 tok next)))
202 (values tree tok))
203 (logical-and (tok &aux tree)
204 (multiple-value-setq (tree tok) (inclusive-or tok))
205 (loop while (eql tok 'CAND) do
206 (multiple-value-bind (right next) (inclusive-or (next))
207 (setq tree (list tok tree right)
208 tok next)))
209 (values tree tok))
210 (inclusive-or (tok &aux tree)
211 (multiple-value-setq (tree tok) (exclusive-or tok))
212 (loop while (eql tok 'LOGIOR) do
213 (multiple-value-bind (right next) (exclusive-or (next))
214 (setq tree (list tok tree right)
215 tok next)))
216 (values tree tok))
217 (exclusive-or (tok &aux tree)
218 (multiple-value-setq (tree tok) (bitwise-and tok))
219 (loop while (eql tok 'LOGXOR) do
220 (multiple-value-bind (right next) (bitwise-and (next))
221 (setq tree (list tok tree right)
222 tok next)))
223 (values tree tok))
224 (bitwise-and (tok &aux tree)
225 (multiple-value-setq (tree tok) (equality tok))
226 (loop while (eql tok 'LOGAND) do
227 (multiple-value-bind (right next) (equality (next))
228 (setq tree (list tok tree right)
229 tok next)))
230 (values tree tok))
231 (equality (tok &aux tree)
232 (multiple-value-setq (tree tok) (relational tok))
233 (loop while (member tok '(CMP= CMP/=)) do
234 (multiple-value-bind (right next) (relational (next))
235 (setq tree (list tok tree right)
236 tok next)))
237 (values tree tok))
238 (relational (tok &aux tree)
239 (multiple-value-setq (tree tok) (shift tok))
240 (loop while (member tok '(CMP< CMP> CMP<= CMP>=)) do
241 (multiple-value-bind (right next) (shift (next))
242 (setq tree (list tok tree right)
243 tok next)))
244 (values tree tok))
245 (shift (tok &aux tree)
246 (multiple-value-setq (tree tok) (additive tok))
247 (loop while (member tok '(SHL SHR)) do
248 (multiple-value-bind (right next) (additive (next))
249 (setq tree (list tok tree right)
250 tok next)))
251 (values tree tok))
252 (additive (tok &aux tree)
253 (multiple-value-setq (tree tok) (multiplicative tok))
254 (loop while (member tok '(ADD SUB)) do
255 (multiple-value-bind (right next) (multiplicative (next))
256 (setq tree (list tok tree right)
257 tok next)))
258 (values tree tok))
259 (multiplicative (tok &aux tree)
260 (multiple-value-setq (tree tok) (unary tok))
261 (loop while (member tok '(MUL FLOOR MOD)) do
262 (multiple-value-bind (right next) (unary (next))
263 (setq tree (list tok tree right)
264 tok next)))
265 (values tree tok))
266 (unary (tok &aux tree)
267 (cond ((eq tok 'LPAR)
268 (multiple-value-setq (tree tok) (conditional (next)))
269 (unless (eq tok 'RPAR)
270 (error _"Expected close-paren."))
271 (values tree (next)))
272 ((numberp tok)
273 (values tok (next)))
274 ((eql tok 'n)
275 (values tok (next)))
276 ((eql tok 'ADD)
277 (unary (next)))
278 ((eql tok 'SUB)
279 (multiple-value-setq (tree tok) (unary (next)))
280 (values (list '- tree) tok))
281 ((eql tok 'LOGNOT32)
282 (multiple-value-setq (tree tok) (unary (next)))
283 (values (list 'LOGNOT32 tree) tok))
284 ((eql tok 'NOT)
285 (multiple-value-setq (tree tok) (unary (next)))
286 (values (list 'CNOT tree) tok))
287 (t
288 (error _"Unexpected token: ~S." tok)))))
289 (multiple-value-bind (tree end) (conditional (next))
290 (unless (eq end 'END)
291 (error _"Expecting end of expression. ~S." end))
292 (let ((*compile-print* nil))
293 (compile nil
294 `(lambda (n)
295 (declare (type (unsigned-byte 32) n)
296 (optimize (space 3)))
297 (flet ((add (a b) (ldb (byte 32 0) (+ a b)))
298 (sub (a b) (ldb (byte 32 0) (- a b)))
299 (mul (a b) (ldb (byte 32 0) (* a b)))
300 (shl (a b) (ldb (byte 32 0) (ash a b)))
301 (shr (a b) (ash a (- b)))
302 (cmp= (a b) (if (= a b) 1 0))
303 (cmp/= (a b) (if (/= a b) 1 0))
304 (cmp< (a b) (if (< a b) 1 0))
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 (cand (a b) (if (or (zerop a) (zerop b)) 0 1))
309 (cor (a b) (if (and (zerop a) (zerop b)) 0 1))
310 (cnot (a) (if a 0 1))
311 (lognot32 (a) (ldb (byte 32 0) (lognot a))))
312 (declare (ignorable #'add #'sub #'mul #'shr #'shl
313 #'cmp= #'cmp/=
314 #'cmp< #'cmp<= #'cmp> #'cmp>=
315 #'cand #'cor #'cnot #'lognot32))
316 ,tree)))))))
317
318 (defun load-domain (domain locale &optional (locale-dir *locale-directories*))
319 (let ((file (locate-domain-file domain locale locale-dir))
320 (read #'read-lelong))
321 (unless file (return-from load-domain nil))
322 (with-open-file (stream file :direction :input :if-does-not-exist nil
323 :element-type '(unsigned-byte 8))
324 (unless stream (return-from load-domain nil))
325 (let ((magic (read-lelong stream)))
326 (cond ((= magic #x950412de) (setq read #'read-lelong))
327 ((= magic #xde120495) (setq read #'read-belong))
328 (t
329 (error _"Bad magic number in \"~A.mo\"." domain))))
330 (let ((version (funcall read stream))
331 (messages (funcall read stream))
332 (master (funcall read stream))
333 (translation (funcall read stream))
334 (entry (make-domain-entry)))
335 (declare (ignore version))
336 (setf (domain-entry-readfn entry) read)
337 (setf (domain-entry-domain entry) domain)
338 (setf (domain-entry-locale entry) locale)
339 (setf (domain-entry-file entry) file)
340 (dotimes (msg messages)
341 (file-position stream (+ master (* 8 msg)))
342 (let ((length (funcall read stream))
343 (start (funcall read stream)))
344 (setf (gethash length (domain-entry-hash entry))
345 (acons start (+ translation (* 8 msg))
346 (gethash length (domain-entry-hash entry))))))
347 (setf (gethash domain *loaded-domains*) entry)
348 (find-encoding entry)))))
349
350 (defun find-domain (domain locale &optional (locale-dir *locale-directories*))
351 (let ((found (gethash domain *loaded-domains*)))
352 (if (and found (string= (domain-entry-locale found) locale))
353 found
354 (load-domain domain locale locale-dir))))
355
356 (declaim (inline string-to-octets))
357 (defun string-to-octets (string encoding)
358 (declare (ignorable encoding))
359 #+(and CMU Unicode)
360 (ext:string-to-octets string :external-format encoding)
361 #+Allegro
362 (excl:string-to-octets string :external-format encoding :null-terminate nil)
363 #+SBCL
364 (sb-ext:string-to-octets string :external-format encoding
365 :null-terminate nil)
366 #+CLISP ;;@@ Not sure if encoding keyword is OK here
367 (ext:convert-string-to-bytes string encoding)
368 ;;@@ add other implementations
369 #-(or (and CMU Unicode) Allegro SBCL CLISP #|others|#)
370 (map-into (make-array (length string) :element-type '(unsigned-byte 8))
371 #'char-code string))
372
373 (declaim (inline octets-to-string))
374 (defun octets-to-string (octets encoding)
375 (declare (ignorable encoding))
376 #+(and CMU Unicode)
377 (ext:octets-to-string octets :external-format encoding)
378 #+Allegro
379 (excl:octets-to-string octets :external-format encoding :end (length octets))
380 #+SBCL
381 (sb-ext:octets-to-string octets :external-format encoding)
382 #+CLISP ;;@@ Not sure if encoding keyword is OK here
383 (ext:convert-string-from-bytes octets encoding)
384 ;;@@ add other implementations
385 #-(or (and CMU Unicode) Allegro SBCL CLISP #|others|#)
386 (map-into (make-string (length octets)) #'code-char octets))
387
388 (defun octets= (a b &key (start1 0) (end1 (length a))
389 (start2 0) (end2 (length b)))
390 (declare (type (simple-array (unsigned-byte 8) (*)) a b)
391 (type (integer 0 #.array-dimension-limit) start1 end1 start2 end2)
392 (optimize (speed 3) (space 2) (safety 0) #-gcl (debug 0)))
393 (loop
394 (unless (= (aref a start1) (aref b start2)) (return nil))
395 (when (or (= (incf start1) end1) (= (incf start2) end2)) (return t))))
396
397 (defun search-domain (octets domain pos)
398 (declare (type (simple-array (unsigned-byte 8) (*)) octets)
399 (type domain-entry domain)
400 (type list pos)
401 (optimize (speed 3) (space 2) (safety 0) #-gcl (debug 0)
402 #+CMU (ext:inhibit-warnings 3))) ; quiet about boxing
403 (when pos
404 (let ((temp (make-array 120 :element-type '(unsigned-byte 8)))
405 (length (length octets)))
406 (with-open-file (stream (domain-entry-file domain)
407 :direction :input
408 :element-type '(unsigned-byte 8))
409 (dolist (entry pos)
410 (file-position stream (car entry))
411 (let ((off 0)
412 (end (read-sequence temp stream
413 :end (min 120 length))))
414 (declare (type (integer 0 #.array-dimension-limit) off end))
415 (loop while (octets= octets temp
416 :start1 off
417 :end1 (min (+ off 120) length)
418 :end2 end)
419 do
420 (incf off end)
421 (when (< off length)
422 (setf end (read-sequence temp stream
423 :end (min 120 (- length off))))))
424 (when (= off length)
425 (file-position stream (cdr entry))
426 (let* ((len (funcall (domain-entry-readfn domain) stream))
427 (off (funcall (domain-entry-readfn domain) stream))
428 (tmp (make-array len :element-type '(unsigned-byte 8))))
429 (file-position stream off)
430 (read-sequence tmp stream)
431 (return (values tmp entry))))))))))
432
433 (defun domain-lookup (string domain)
434 (declare (type string string) (type domain-entry domain)
435 (optimize (speed 3) (space 2) (safety 0)))
436 (or (if (null (domain-entry-encoding domain)) string)
437 (gethash string (domain-entry-hash domain))
438 (let* ((octets (string-to-octets string
439 (domain-entry-encoding domain)))
440 (length (length octets))
441 (pos (gethash length (domain-entry-hash domain))))
442 (declare (type (simple-array (unsigned-byte 8) (*)) octets))
443 (multiple-value-bind (tmp entry) (search-domain octets domain pos)
444 (declare (type (or null (simple-array (unsigned-byte 8) (*))) tmp))
445 (when tmp
446 (let ((temp (delete entry pos :test #'eq)))
447 (if temp
448 (setf (gethash length (domain-entry-hash domain)) temp)
449 (remhash length (domain-entry-hash domain))))
450 (setf (gethash (copy-seq string) (domain-entry-hash domain))
451 (octets-to-string tmp (domain-entry-encoding domain))))))))
452
453 (defun domain-lookup-plural (singular plural domain)
454 (declare (type string singular plural) (type domain-entry domain)
455 (optimize (speed 3) (space 2) (safety 0)))
456 (or (if (null (domain-entry-encoding domain)) nil)
457 (gethash (cons singular plural) (domain-entry-hash domain))
458 (let* ((octets (let* ((a (string-to-octets singular
459 (domain-entry-encoding domain)))
460 (b (string-to-octets plural
461 (domain-entry-encoding domain)))
462 (c (make-array (+ (length a) (length b) 1)
463 :element-type '(unsigned-byte 8))))
464 (declare (type (simple-array (unsigned-byte 8) (*))
465 a b c))
466 (replace c a)
467 (setf (aref c (length a)) 0)
468 (replace c b :start1 (+ (length a) 1))
469 c))
470 (length (length octets))
471 (pos (gethash length (domain-entry-hash domain))))
472 (declare (type (simple-array (unsigned-byte 8) (*)) octets)
473 (type list pos))
474 (multiple-value-bind (tmp entry) (search-domain octets domain pos)
475 (declare (type (or null (simple-array (unsigned-byte 8) (*))) tmp))
476 (when tmp
477 (prog1
478 (setf (gethash (cons (copy-seq singular) (copy-seq plural))
479 (domain-entry-hash domain))
480 (loop for i = 0 then (1+ j)
481 as j = (position 0 tmp :start i)
482 collect (octets-to-string (subseq tmp i j)
483 (domain-entry-encoding domain))
484 while j))
485 (let ((temp (delete entry pos :test #'eq)))
486 (if temp
487 (setf (gethash length (domain-entry-hash domain)) temp)
488 (remhash length (domain-entry-hash domain))))
489 (when (null (domain-entry-plurals domain))
490 (setf (domain-entry-plurals domain)
491 (parse-plurals domain)))))))))
492
493 (declaim (inline getenv)
494 (ftype (function (string) (or null string)) getenv))
495 (defun getenv (var)
496 (let ((val #+(or CMU SCL) (cdr (assoc (intern var "KEYWORD")
497 ext:*environment-list*))
498 #+SBCL (sb-ext:posix-getenv var)
499 #+Allegro (system:getenv var)
500 #+LispWorks (hcl:getenv var)
501 #+clisp (ext:getenv var)
502 #+(or openmcl mcl) (ccl::getenv var)
503 #+(or gcl ecl) (si::getenv var)))
504 (if (equal val "") nil val)))
505
506 (defun setlocale (&optional locale)
507 (setf *locale* (or locale
508 (getenv "LANGUAGE")
509 (getenv "LC_ALL")
510 (getenv "LC_MESSAGES")
511 (getenv "LANG")
512 *locale*)))
513
514 (defmacro textdomain (domain)
515 `(eval-when (:compile-toplevel :execute)
516 (setf *default-domain* ,domain)))
517
518 (defmacro gettext (string)
519 _N"Look up STRING in the current message domain and return its translation."
520 `(dgettext ,*default-domain* ,string))
521
522 (defmacro ngettext (singular plural n)
523 _N"Look up the singular or plural form of a message in the current domain."
524 `(dngettext ,*default-domain* ,singular ,plural ,n))
525
526 (declaim (inline dgettext))
527 (defun dgettext (domain string)
528 _N"Look up STRING in the specified message domain and return its translation."
529 (declare (optimize (speed 3) (space 2) (safety 0)))
530 (let ((domain (and domain (find-domain domain *locale*))))
531 (or (and domain (domain-lookup string domain)) string)))
532
533 (defun dngettext (domain singular plural n)
534 _N"Look up the singular or plural form of a message in the specified domain."
535 (declare (type integer n)
536 (optimize (speed 3) (space 2) (safety 0)))
537 (let* ((domain (and domain (find-domain domain *locale*)))
538 (list (and domain (domain-lookup-plural singular plural domain))))
539 (if list
540 (nth (the integer
541 (funcall (the function (domain-entry-plurals domain)) n))
542 list)
543 (if (= n 1) singular plural))))
544
545 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
546
547 #-runtime
548 (defvar *translator-comment* nil)
549
550 #-runtime
551 (defvar *translations* (make-hash-table :test 'equal))
552
553 #-runtime
554 (defun note-translatable (domain string &optional plural)
555 (when domain
556 (let* ((hash (or (gethash domain *translations*)
557 (setf (gethash domain *translations*)
558 (make-hash-table :test 'equal))))
559 (key (if plural (cons string plural) string))
560 (val (or (gethash key hash) (cons nil nil))))
561 (pushnew *translator-comment* (car val) :test #'equal)
562 (pushnew *compile-file-pathname* (cdr val) :test #'equal)
563 ;; FIXME: How does this happen? Need to figure this out and get
564 ;; rid of this!
565 (unless key
566 (warn "Translate error with null key. domain = ~S string = ~S~%"
567 domain string))
568 (setf (gethash key hash) val)))
569 (setq *translator-comment* nil))
570
571 (define-compiler-macro dgettext (&whole form domain string)
572 #-runtime
573 (when (and (stringp domain) (stringp string))
574 (note-translatable domain string))
575 form)
576
577 (define-compiler-macro dngettext (&whole form domain singular plural n)
578 (declare (ignore n))
579 #-runtime
580 (when (and (stringp domain) (stringp singular) (stringp plural))
581 (note-translatable domain singular plural))
582 form)
583
584 (defun read-translatable-string (stream char)
585 (declare (ignore char))
586 (case (peek-char nil stream nil nil t)
587 (#\" (let* ((*read-suppress* nil)
588 (string (read stream t nil t)))
589 `(gettext ,string)))
590 (#\N (read-char stream t nil t)
591 (let* ((*read-suppress* nil)
592 (string (read stream t nil t)))
593 #-runtime
594 (note-translatable *default-domain* string)
595 string))
596 (#\@ (error _"_@ is a reserved reader macro prefix."))
597 (otherwise
598 (let ((fn (get-macro-character #\_ nil)))
599 (if fn (funcall fn stream #\_) '_)))))
600
601 ;; Process comments as usual, but look for lines that begin with
602 ;; "TRANSLATORS: ". These lines are saved and written out as a
603 ;; translator comment for the next translatable string.
604 #-runtime
605 (defun read-comment (stream char)
606 (declare (optimize (speed 0) (space 3) #-gcl (debug 0))
607 (ignore char))
608 (do ((state 0)
609 (index 0)
610 (text nil)
611 (char (read-char stream nil nil t) (read-char stream nil nil t)))
612 ((or (not char) (char= char #\Newline))
613 (when text (setq *translator-comment* (copy-seq text))))
614 (cond ((and (= state 0) (char= char #\Space)) (setq state 1))
615 ((and (= state 0) (char= char #\T)) (setq state 1 index 1))
616 ((and (= state 0) (char/= char #\;)) (setq state 2))
617 ((and (= state 1) (= index 0) (char= char #\Space)) #|ignore|#)
618 ((= state 1)
619 (if (char= char (char "TRANSLATORS: " index))
620 (when (= (incf index) 13)
621 (setq state 3))
622 (setq state 2)))
623 ((= state 3)
624 (when (null text)
625 (setq text (make-array 50 :element-type 'character
626 :adjustable t :fill-pointer 0)))
627 (vector-push-extend char text))))
628 (values))
629
630 #-runtime
631 (defun read-nested-comment (stream subchar arg)
632 (declare (ignore subchar arg)
633 (optimize (speed 0) (space 3) #-gcl (debug 0)))
634 (do ((level 1)
635 (state 0)
636 (index 0)
637 (text nil)
638 (prev (read-char stream t nil t) char)
639 (char (read-char stream t nil t) (read-char stream t nil t)))
640 (())
641 (cond ((and (char= prev #\|) (char= char #\#))
642 (when (zerop (decf level))
643 (when text
644 (setq *translator-comment*
645 (string-right-trim '(#\Space #\Newline) text)))
646 (return)))
647 ((and (char= prev #\#) (char= char #\|))
648 (setq state 2)
649 (incf level))
650 ((and (= state 0) (char= prev #\Space)) (setq state 1))
651 ((and (= state 0) (char= prev #\T))
652 (setq state 1 index 1))
653 ((= state 0) (setq state 2))
654 ((and (= state 1) (= index 0) (char= prev #\Space)) #| ignore |#)
655 ((= state 1)
656 (if (char= prev (char "TRANSLATORS: " index))
657 (when (= (incf index) 13)
658 (setq state 3))
659 (setq state 2)))
660 ((= state 3)
661 (when (null text)
662 (setq text (make-array 50 :element-type 'character
663 :adjustable t :fill-pointer 0)))
664 (vector-push-extend prev text))))
665 (values))
666
667 (defun install ()
668 (set-macro-character #\_ #'read-translatable-string t)
669 #-runtime
670 (set-macro-character #\; #'read-comment)
671 #-runtime
672 (set-dispatch-macro-character #\# #\| #'read-nested-comment)
673 t)
674
675
676 ;; Dump the translatable strings. The output is written to a file in
677 ;; the directory OUTPUT-DIRECTORY and its name is the domain.
678 #-runtime
679 (defun dump-pot-files (&key copyright output-directory)
680 (declare (optimize (speed 0) (space 3) #-gcl (debug 1)))
681 (labels ((b (key data)
682 (format t "~@[~{~&#. ~A~}~%~]" (delete nil (car data)))
683 (format t "~@[~&~<#: ~@;~@{~A~^ ~}~:@>~%~]"
684 (delete nil (cdr data)))
685 (cond ((consp key)
686 (format t "~&msgid ") (str (car key) 6 0)
687 (format t "~&msgid_plural ") (str (cdr key) 13 0)
688 (format t "~&msgstr[0] \"\"~2%"))
689 (t
690 (cond
691 (key
692 (format t "~&msgid ") (str key 6 0)
693 (format t "~&msgstr \"\"~2%"))
694 (t
695 (format *error-output* "Skipping NIL key~%"))))))
696 (str (string col start)
697 (when (and (plusp col) (> (length string) (- 76 col)))
698 (format t "\"\"~%"))
699 (let ((nl (position #\Newline string :start start)))
700 (cond ((and nl (< (- nl start) 76))
701 (write-char #\")
702 (wstr string start nl)
703 (format t "\\n\"~%")
704 (str string 0 (1+ nl)))
705 ((< (- (length string) start) 76)
706 (write-char #\")
707 (wstr string start (length string))
708 (write-char #\"))
709 (t
710 (let* ((a (+ start 1))
711 (b (+ start 76))
712 (b1 (position #\Space string :start a :end b
713 :from-end t))
714 (b2 (position-if (lambda (x)
715 (position x ";:,?!)]}"))
716 string :start a :end b
717 :from-end t))
718 (b3 (position-if (lambda (x)
719 (position x "\"'-"))
720 string :start a :end b
721 :from-end t))
722 (b4 (position-if #'digit-char-p
723 string :start a :end b
724 :from-end t))
725 (b5 (position-if #'alpha-char-p
726 string :start a :end b
727 :from-end t))
728 (g1 (if b1 (* (- b b1) (- b b1) .03) 10000))
729 (g2 (if b2 (* (- b b2) (- b b2) .20) 10000))
730 (g3 (if b3 (* (- b b3) (- b b3) .97) 10000))
731 (g4 (if b4 (* (- b b4) (- b b4) 1.3) 10000))
732 (g5 (if b5 (* (- b b5) (- b b5) 2.0) 10000))
733 (g (min g1 g2 g3 g4 g5))
734 (end (1+ (cond ((> g 750) b)
735 ((= g g1) b1)
736 ((= g g2) b2)
737 ((= g g3) b3)
738 ((= g g4) b4)
739 ((= g g5) b5)))))
740 #+(or)
741 (progn
742 (format t "~&Splitting ~S:~%"
743 (subseq string start b))
744 (format t "~{~& b~D=~D; goodness=~F~}~%"
745 (list 1 b1 g1 2 b2 g2 3 b3 g3 4 b4 g4 5 b5 g5
746 6 b 10000))
747 (format t "~& best=~F == ~D~%" g end)
748 (format t "~& Part1=~S~% Part2=~S~%"
749 (subseq string start end)
750 (subseq string end b)))
751 (write-char #\")
752 (wstr string start end)
753 (write-char #\") (terpri)
754 (str string 0 end))))))
755 (wstr (string start end)
756 (loop while (< start end) do
757 (let ((i (position-if (lambda (x)
758 (or (char= x #\") (char= x #\\)))
759 string :start start :end end)))
760 (write-string string nil :start start :end (or i end))
761 (when i (write-char #\\ nil) (write-char (char string i) nil))
762 (setq start (if i (1+ i) end)))))
763 (a (domain hash)
764 (with-open-file (*standard-output*
765 (merge-pathnames (make-pathname :name domain
766 :type "pot")
767 output-directory)
768 :direction :output
769 :if-exists :new-version
770 :external-format :utf8)
771 (format t "~&#@ ~A~2%" domain)
772 (format t "~&# SOME DESCRIPTIVE TITLE~%")
773 (format t "~@[~&# Copyright (C) YEAR ~A~%~]" copyright)
774 (format t "~&# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR~%")
775 (format t "~&#~%#, fuzzy~%msgid \"\"~%msgstr \"\"~%")
776 (format t "~&\"Project-Id-Version: PACKAGE VERSION\\n\"~%")
777 (format t "~&\"Report-Msgid-Bugs-To: \\n\"~%")
778 (format t "~&\"PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\\n\"~%")
779 (format t "~&\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"~%")
780 (format t "~&\"Language-Team: LANGUAGE <LL@li.org>\\n\"~%")
781 (format t "~&\"MIME-Version: 1.0\\n\"~%")
782 (format t "~&\"Content-Type: text/plain; charset=UTF-8\\n\"~%")
783 (format t "~&\"Content-Transfer-Encoding: 8bit\\n\"~2%")
784 (maphash #'b hash))))
785 (maphash #'a *translations*)
786 #+(or)
787 (clrhash *translations*))
788 nil)
789
790
791
792 (eval-when (:compile-toplevel :execute)
793 (setq *default-domain* nil)
794 (unless (and (fboundp 'intl:read-translatable-string)
795 (eq (get-macro-character #\_)
796 (fdefinition 'intl:read-translatable-string)))
797 (set-syntax-from-char #\_ #\_)))
798
799 (install)

  ViewVC Help
Powered by ViewVC 1.1.5