1 ;;; gettext.el --- GNU gettext interface
2 ;; Copyright (C) 1999 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
8 ;; This file is part of Liece.
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
31 (eval-when-compile (require 'cl))
38 (autoload 'mime-content-type-parameter "mime-parse")
39 (autoload 'mime-read-Content-Type "mime-parse"))
41 (static-if (fboundp 'string-to-list)
42 (defalias 'gettext-string-to-list 'string-to-list)
43 ;; Rely on `string-to-char-list' emulation is provided in poem.
44 (defalias 'gettext-string-to-list 'string-to-char-list))
46 (defvar gettext-gmo-endian 1234)
47 (defvar gettext-message-domain-to-catalog-alist nil)
48 (defvar gettext-default-message-domain "emacs")
49 (defvar gettext-default-mime-charset default-mime-charset)
51 (defconst gettext-msgid-regexp "msgid\\s-*\"")
52 (defconst gettext-msgstr-regexp "msgstr\\s-*\"")
54 (defmacro gettext-hex-char-to-integer (character)
55 `(if (and (>= ,character ?0) (<= ,character ?9))
57 (let ((ch (logior ,character 32)))
58 (if (and (>= ch ?a) (<= ch ?f))
60 (error "Invalid hex digit `%c'" ch)))))
62 (defun gettext-hex-string-to-integer (hex-string)
64 (while (not (equal hex-string ""))
65 (setq hex-num (+ (* hex-num 16)
66 (gettext-hex-char-to-integer
67 (string-to-char hex-string)))
68 hex-string (substring hex-string 1)))
71 (defun gettext-gmo-read-32bit-word ()
72 (let ((word (string-to-char-list
73 (buffer-substring (point) (+ (point) 4)))))
75 (apply #'format "%02x%02x%02x%02x"
76 (mapcar (lambda (ch) (logand 255 ch))
77 (if (= gettext-gmo-endian 1234)
81 (defmacro gettext-gmo-header-revision (header)
84 (defmacro gettext-gmo-header-nn (header)
87 (defmacro gettext-gmo-header-oo (header)
90 (defmacro gettext-gmo-header-tt (header)
93 (defmacro gettext-gmo-header-ss (header)
96 (defmacro gettext-gmo-header-hh (header)
99 (defmacro gettext-gmo-read-header ()
101 (make-list 6 '(gettext-hex-string-to-integer
102 (gettext-gmo-read-32bit-word)))))
104 (defun gettext-gmo-collect-strings (nn)
105 (let (strings pos len off)
107 (setq len (gettext-hex-string-to-integer
108 (gettext-gmo-read-32bit-word))
109 off (gettext-hex-string-to-integer
110 (gettext-gmo-read-32bit-word))
113 (push (buffer-substring (point) (+ (point) len))
118 (defun gettext-parse-Content-Type (&optional header)
119 "Return the MIME charset of PO file."
122 (if (require 'mime-parse nil 'noerror)
123 (mime-content-type-parameter (mime-read-Content-Type) "charset")
124 (goto-char (point-min))
125 (if (re-search-forward
126 "^\"Content-Type: *text/plain;[ \t]*charset=\\([^\\]+\\)"
128 (find-mime-charset-by-charsets
129 (list (buffer-substring (match-beginning 1) (match-end 1))))
130 gettext-default-mime-charset))))
132 (defun gettext-mapcar* (function &rest args)
133 "Apply FUNCTION to successive cars of all ARGS.
134 Return the list of results."
135 (unless (memq nil args)
136 (cons (apply function (mapcar #'car args))
137 (apply #'gettext-mapcar* function
138 (mapcar #'cdr args)))))
140 (defun gettext-load-message-catalogue (file)
142 (let (header strings charset gettext-obarray)
143 (as-binary-input-file
144 (insert-file-contents file)
145 (goto-char (point-min))
146 (when (looking-at "\x95\x04\x12\xde")
147 (setq gettext-gmo-endian 4321))
149 (setq header (gettext-gmo-read-header)
151 (gettext-mapcar* #'cons
153 (goto-char (1+ (gettext-gmo-header-oo header)))
154 (gettext-gmo-collect-strings
155 (gettext-gmo-header-nn header)))
157 (goto-char (1+ (gettext-gmo-header-tt header)))
158 (gettext-gmo-collect-strings
159 (gettext-gmo-header-nn header))))
160 charset (or (gettext-parse-Content-Type
161 (cdr (assoc "" strings)))
163 gettext-obarray (make-vector
164 (* 2 (gettext-gmo-header-nn header))
166 (dolist (oott strings)
167 (set (intern (car oott) gettext-obarray)
168 (decode-mime-charset-string
169 (cdr oott) charset)))
172 (defun gettext-load-portable-message-catalogue (file)
174 (let (strings charset msgstr msgid state gettext-obarray)
175 (as-binary-input-file
176 (insert-file-contents file)
177 (goto-char (point-min))
180 ((looking-at gettext-msgid-regexp)
181 (if (eq state 'msgstr)
182 (push (cons msgid msgstr)
184 (setq msgid (buffer-substring (match-end 0)
185 (progn (end-of-line) (point))))
186 (when (string-match "\"\\s-*$" msgid)
187 (setq msgid (substring msgid 0 (match-beginning 0))))
189 ((looking-at gettext-msgstr-regexp)
190 (setq msgstr (buffer-substring (match-end 0)
191 (progn (end-of-line) (point))))
192 (when (string-match "\"\\s-*$" msgstr)
193 (setq msgstr (substring msgstr 0 (match-beginning 0))))
194 (setq state 'msgstr))
195 ((looking-at "\\s-*\"")
196 (let ((line (buffer-substring (match-end 0)
197 (progn (end-of-line) (point)))))
198 (when (string-match "\"\\s-*$" line)
199 (setq line (substring line 0 (match-beginning 0))))
200 (set state (concat (symbol-value state) line)))))
201 (beginning-of-line 2))
202 (if (eq state 'msgstr)
203 (push (cons msgid msgstr)
207 (goto-char (point-min))
208 (insert "(setq strings '(\n")
209 (dolist (oott strings)
210 (insert (format "(\"%s\" . \"%s\")\n"
211 (car oott) (cdr oott)))
213 (ignore-errors (eval-buffer))
214 (setq charset (or (gettext-parse-Content-Type
215 (cdr (assoc "" strings)))
217 (dolist (oott strings)
218 (set (intern (car oott) gettext-obarray)
219 (decode-mime-charset-string
220 (cdr oott) charset)))
223 (unless (featurep 'i18n3)
225 (defun dgettext (domain string)
226 "Look up STRING in the default message domain and return its translation.
227 \[XEmacs I18N level 3 emulating function]"
228 (let ((oott (assoc domain gettext-message-domain-to-catalog-alist)))
229 (when (stringp (cdr oott))
230 (setcdr oott (gettext-load-message-catalogue
233 (intern-soft string (or (cdr oott) (make-vector 1 0))))
236 (defun gettext (string)
237 "Look up STRING in the default message domain and return its translation.
238 \[XEmacs I18N level 3 emulating function]"
239 (dgettext gettext-default-message-domain string))
241 (defun bind-text-domain (domain pathname)
242 "Associate a pathname with a message domain.
243 Here's how the path to message files is constructed under SunOS 5.0:
244 {pathname}/{LANG}/LC_MESSAGES/{domain}.mo
245 \[XEmacs I18N level 3 emulating function]"
246 (let* ((lang (getenv "LANG"))
247 (file (concat domain ".mo"))
248 (catalog (expand-file-name
249 file (concat pathname "/" lang "/LC_MESSAGES"))))
250 (when (file-exists-p catalog)
251 ;;(file-exists-p (setq catalog (expand-file-name file pathname)))
252 (push (cons domain catalog) gettext-message-domain-to-catalog-alist))))
254 (defun set-domain (domain)
255 "Specify the domain used for translating messages in this source file.
256 The domain declaration may only appear at top-level, and should precede
257 all function and variable definitions.
259 The presence of this declaration in a compiled file effectively sets the
260 domain of all functions and variables which are defined in that file.
261 \[XEmacs I18N level 3 emulating function]"
262 (setq gettext-default-message-domain domain)))
266 ;;; gettext.el ends here