Skip to content

Commit

Permalink
enabled to `define-dictionary' with file name.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukamachi committed Jan 12, 2011
1 parent c5af822 commit 239d444
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Expand Up @@ -2,7 +2,7 @@

## Usage

(load-dictionary #p"i18n/messages.lisp")
(define-dictionary "sche" #p"i18n/messages.lisp")
(i18n "Schedule")
;;=> "Schedule"
(i18n "Schedule" :locale :ja-JP)
Expand Down
13 changes: 11 additions & 2 deletions src/locale.lisp
@@ -1,12 +1,21 @@
(in-package :cl-locale)

(defun define-dictionary (name dict-list)
(defmethod define-dictionary (name (dict cons))
(loop with hash = (make-hash-table :test 'equal)
for (word . plist) in dict-list
for (word . plist) in dict
do (setf (gethash word hash) plist)
(loop for locale in plist by #'cddr do (pushnew locale *locales*))
finally (setf (gethash name *dictionaries*) hash)))

(defun slurp-stream (stream)
(let ((seq (make-string (file-length stream))))
(read-sequence seq stream)
seq))

(defmethod define-dictionary (name (dict pathname))
(with-open-file (stream dict)
(define-dictionary name (read-from-string (slurp-stream stream)))))

(defmacro aif (test then &optional else)
`(let ((it ,test))
(if it ,then ,else)))
Expand Down
6 changes: 5 additions & 1 deletion test/locale.lisp
@@ -1,6 +1,6 @@
(in-package :cl-locale-test)

(plan 4)
(plan 5)

(deftest i18n
(setf cl-test-more::*default-test-function* #'string=)
Expand All @@ -13,6 +13,10 @@
(is (i18n "Schedule") "予定" "ja-JP (default locale)")
(is (i18n "Schedule" :locale :ja-JP) "予定" "ja-JP")
(is (i18n "Schedule" :locale :fr-FR) "Calendrier" "fr-FR")

(define-dictionary "lisp" (merge-pathnames #p"dictionary.lisp" *load-pathname*))
(setf *dictionary-name* "lisp")
(is (i18n "Lisping" :locale :ja-JP) "舌足らず" "load from file")
)

(run-test-all)

0 comments on commit 239d444

Please sign in to comment.