From 239d444b37a083296b2e40551dc0c37da3e9ee6c Mon Sep 17 00:00:00 2001 From: fukamachi Date: Wed, 12 Jan 2011 15:00:44 +0900 Subject: [PATCH] enabled to `define-dictionary' with file name. --- README.markdown | 2 +- src/locale.lisp | 13 +++++++++++-- test/locale.lisp | 6 +++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.markdown b/README.markdown index 66e37f8..be2fbd1 100644 --- a/README.markdown +++ b/README.markdown @@ -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) diff --git a/src/locale.lisp b/src/locale.lisp index de7d47b..af98047 100644 --- a/src/locale.lisp +++ b/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))) diff --git a/test/locale.lisp b/test/locale.lisp index 9a297ec..f772325 100644 --- a/test/locale.lisp +++ b/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=) @@ -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)