/[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.8 - (hide annotations)
Wed Feb 10 01:52:28 2010 UTC (4 years, 2 months ago) by rtoy
Branch: intl-branch
Changes since 1.1.2.7: +4 -2 lines
Paul says we need to call NOTE-TRANSLATABLE in
READ-TRANSLATABLE-STRING for #\", just like for #\N.  This allows
string in code that has been conditionalized out to be put in the pot
file.
1 rtoy 1.1.2.1 ;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Package: INTL -*-
2    
3 rtoy 1.1.2.8 ;;; $Revision: 1.1.2.8 $
4 rtoy 1.1.2.1 ;;; 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 rtoy 1.1.2.8 (ext:file-comment "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/intl.lisp,v 1.1.2.8 2010/02/10 01:52:28 rtoy Exp $")
27 rtoy 1.1.2.1
28     (in-package "INTL")
29    
30     (eval-when (:compile-toplevel :execute)
31 rtoy 1.1.2.2 (defparameter intl::*default-domain* "cmucl")
32 rtoy 1.1.2.1 (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 rtoy 1.1.2.7 ;; 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 rtoy 1.1.2.1
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 rtoy 1.1.2.3 ;; 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 rtoy 1.1.2.1 (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 rtoy 1.1.2.6 (#\" (let* ((*read-suppress* nil)
588     (string (read stream t nil t)))
589 rtoy 1.1.2.8 #-runtime
590     (note-translatable *default-domain* string)
591 rtoy 1.1.2.1 `(gettext ,string)))
592     (#\N (read-char stream t nil t)
593 rtoy 1.1.2.6 (let* ((*read-suppress* nil)
594     (string (read stream t nil t)))
595 rtoy 1.1.2.1 #-runtime
596     (note-translatable *default-domain* string)
597     string))
598     (#\@ (error _"_@ is a reserved reader macro prefix."))
599     (otherwise
600     (let ((fn (get-macro-character #\_ nil)))
601     (if fn (funcall fn stream #\_) '_)))))
602    
603 rtoy 1.1.2.2 ;; Process comments as usual, but look for lines that begin with
604     ;; "TRANSLATORS: ". These lines are saved and written out as a
605     ;; translator comment for the next translatable string.
606 rtoy 1.1.2.1 #-runtime
607     (defun read-comment (stream char)
608     (declare (optimize (speed 0) (space 3) #-gcl (debug 0))
609     (ignore char))
610     (do ((state 0)
611     (index 0)
612     (text nil)
613     (char (read-char stream nil nil t) (read-char stream nil nil t)))
614     ((or (not char) (char= char #\Newline))
615     (when text (setq *translator-comment* (copy-seq text))))
616     (cond ((and (= state 0) (char= char #\Space)) (setq state 1))
617     ((and (= state 0) (char= char #\T)) (setq state 1 index 1))
618     ((and (= state 0) (char/= char #\;)) (setq state 2))
619     ((and (= state 1) (= index 0) (char= char #\Space)) #|ignore|#)
620     ((= state 1)
621     (if (char= char (char "TRANSLATORS: " index))
622     (when (= (incf index) 13)
623     (setq state 3))
624     (setq state 2)))
625     ((= state 3)
626     (when (null text)
627     (setq text (make-array 50 :element-type 'character
628     :adjustable t :fill-pointer 0)))
629     (vector-push-extend char text))))
630     (values))
631    
632     #-runtime
633     (defun read-nested-comment (stream subchar arg)
634     (declare (ignore subchar arg)
635     (optimize (speed 0) (space 3) #-gcl (debug 0)))
636     (do ((level 1)
637     (state 0)
638     (index 0)
639     (text nil)
640     (prev (read-char stream t nil t) char)
641     (char (read-char stream t nil t) (read-char stream t nil t)))
642     (())
643     (cond ((and (char= prev #\|) (char= char #\#))
644     (when (zerop (decf level))
645     (when text
646     (setq *translator-comment*
647     (string-right-trim '(#\Space #\Newline) text)))
648     (return)))
649     ((and (char= prev #\#) (char= char #\|))
650     (setq state 2)
651     (incf level))
652     ((and (= state 0) (char= prev #\Space)) (setq state 1))
653     ((and (= state 0) (char= prev #\T))
654     (setq state 1 index 1))
655     ((= state 0) (setq state 2))
656     ((and (= state 1) (= index 0) (char= prev #\Space)) #| ignore |#)
657     ((= state 1)
658     (if (char= prev (char "TRANSLATORS: " index))
659     (when (= (incf index) 13)
660     (setq state 3))
661     (setq state 2)))
662     ((= state 3)
663     (when (null text)
664     (setq text (make-array 50 :element-type 'character
665     :adjustable t :fill-pointer 0)))
666     (vector-push-extend prev text))))
667     (values))
668    
669     (defun install ()
670     (set-macro-character #\_ #'read-translatable-string t)
671     #-runtime
672     (set-macro-character #\; #'read-comment)
673     #-runtime
674     (set-dispatch-macro-character #\# #\| #'read-nested-comment)
675     t)
676    
677    
678 rtoy 1.1.2.2 ;; Dump the translatable strings. The output is written to a file in
679     ;; the directory OUTPUT-DIRECTORY and its name is the domain.
680 rtoy 1.1.2.1 #-runtime
681 rtoy 1.1.2.2 (defun dump-pot-files (&key copyright output-directory)
682 rtoy 1.1.2.1 (declare (optimize (speed 0) (space 3) #-gcl (debug 1)))
683     (labels ((b (key data)
684     (format t "~@[~{~&#. ~A~}~%~]" (delete nil (car data)))
685     (format t "~@[~&~<#: ~@;~@{~A~^ ~}~:@>~%~]"
686     (delete nil (cdr data)))
687     (cond ((consp key)
688     (format t "~&msgid ") (str (car key) 6 0)
689     (format t "~&msgid_plural ") (str (cdr key) 13 0)
690     (format t "~&msgstr[0] \"\"~2%"))
691     (t
692 rtoy 1.1.2.4 (cond
693     (key
694     (format t "~&msgid ") (str key 6 0)
695     (format t "~&msgstr \"\"~2%"))
696     (t
697     (format *error-output* "Skipping NIL key~%"))))))
698 rtoy 1.1.2.1 (str (string col start)
699     (when (and (plusp col) (> (length string) (- 76 col)))
700     (format t "\"\"~%"))
701     (let ((nl (position #\Newline string :start start)))
702     (cond ((and nl (< (- nl start) 76))
703     (write-char #\")
704     (wstr string start nl)
705     (format t "\\n\"~%")
706     (str string 0 (1+ nl)))
707     ((< (- (length string) start) 76)
708     (write-char #\")
709     (wstr string start (length string))
710     (write-char #\"))
711     (t
712     (let* ((a (+ start 1))
713     (b (+ start 76))
714     (b1 (position #\Space string :start a :end b
715     :from-end t))
716     (b2 (position-if (lambda (x)
717     (position x ";:,?!)]}"))
718     string :start a :end b
719     :from-end t))
720     (b3 (position-if (lambda (x)
721     (position x "\"'-"))
722     string :start a :end b
723     :from-end t))
724     (b4 (position-if #'digit-char-p
725     string :start a :end b
726     :from-end t))
727     (b5 (position-if #'alpha-char-p
728     string :start a :end b
729     :from-end t))
730     (g1 (if b1 (* (- b b1) (- b b1) .03) 10000))
731     (g2 (if b2 (* (- b b2) (- b b2) .20) 10000))
732     (g3 (if b3 (* (- b b3) (- b b3) .97) 10000))
733     (g4 (if b4 (* (- b b4) (- b b4) 1.3) 10000))
734     (g5 (if b5 (* (- b b5) (- b b5) 2.0) 10000))
735     (g (min g1 g2 g3 g4 g5))
736     (end (1+ (cond ((> g 750) b)
737     ((= g g1) b1)
738     ((= g g2) b2)
739     ((= g g3) b3)
740     ((= g g4) b4)
741     ((= g g5) b5)))))
742     #+(or)
743     (progn
744     (format t "~&Splitting ~S:~%"
745     (subseq string start b))
746     (format t "~{~& b~D=~D; goodness=~F~}~%"
747     (list 1 b1 g1 2 b2 g2 3 b3 g3 4 b4 g4 5 b5 g5
748     6 b 10000))
749     (format t "~& best=~F == ~D~%" g end)
750     (format t "~& Part1=~S~% Part2=~S~%"
751     (subseq string start end)
752     (subseq string end b)))
753     (write-char #\")
754     (wstr string start end)
755     (write-char #\") (terpri)
756     (str string 0 end))))))
757     (wstr (string start end)
758     (loop while (< start end) do
759     (let ((i (position-if (lambda (x)
760     (or (char= x #\") (char= x #\\)))
761     string :start start :end end)))
762     (write-string string nil :start start :end (or i end))
763     (when i (write-char #\\ nil) (write-char (char string i) nil))
764     (setq start (if i (1+ i) end)))))
765     (a (domain hash)
766 rtoy 1.1.2.2 (with-open-file (*standard-output*
767     (merge-pathnames (make-pathname :name domain
768     :type "pot")
769     output-directory)
770     :direction :output
771     :if-exists :new-version
772     :external-format :utf8)
773     (format t "~&#@ ~A~2%" domain)
774     (format t "~&# SOME DESCRIPTIVE TITLE~%")
775     (format t "~@[~&# Copyright (C) YEAR ~A~%~]" copyright)
776     (format t "~&# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR~%")
777     (format t "~&#~%#, fuzzy~%msgid \"\"~%msgstr \"\"~%")
778     (format t "~&\"Project-Id-Version: PACKAGE VERSION\\n\"~%")
779     (format t "~&\"Report-Msgid-Bugs-To: \\n\"~%")
780     (format t "~&\"PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\\n\"~%")
781     (format t "~&\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"~%")
782     (format t "~&\"Language-Team: LANGUAGE <LL@li.org>\\n\"~%")
783     (format t "~&\"MIME-Version: 1.0\\n\"~%")
784     (format t "~&\"Content-Type: text/plain; charset=UTF-8\\n\"~%")
785     (format t "~&\"Content-Transfer-Encoding: 8bit\\n\"~2%")
786     (maphash #'b hash))))
787 rtoy 1.1.2.1 (maphash #'a *translations*)
788     #+(or)
789     (clrhash *translations*))
790     nil)
791    
792    
793    
794     (eval-when (:compile-toplevel :execute)
795     (setq *default-domain* nil)
796     (unless (and (fboundp 'intl:read-translatable-string)
797     (eq (get-macro-character #\_)
798     (fdefinition 'intl:read-translatable-string)))
799     (set-syntax-from-char #\_ #\_)))
800    
801     (install)

  ViewVC Help
Powered by ViewVC 1.1.5