(in-package #:cl-user) (defpackage #:rails-to-caveman.locale (:use #:cl) (:export #:define-dictionary #:with-i18n #:accept-language #:parse-accept-language #:find-locale #:find-acceptable-locale)) (in-package #:rails-to-caveman.locale) (cl-locale:define-dictionary rails-to-caveman ("ja" (merge-pathnames "locale/ja-jp.lisp" rails-to-caveman.config:*static-directory*)) ("ja-JP" (merge-pathnames "locale/ja-jp.lisp" rails-to-caveman.config:*static-directory*))) (defmacro define-dictionary (name &rest args) `(progn (cl-locale:define-dictionary ,name ,@args) (defun reload () (loop :for (locale dictionary) :in (list ,@(mapcar (lambda (arg) `(list ,@arg)) args)) :do (cl-locale.core::register-dictionary ',name dictionary :locale locale))))) (defmacro with-i18n ((dictionary locale) &body string-generate-form) `(let ((cl-locale.core::*dictionary* (cl-locale.core::ensure-dictionary ',dictionary)) (cl-locale.core::*locale* ,locale)) (cl-locale:i18n (progn ,@string-generate-form)))) (defun accept-language () (and ningle:*request* (gethash "accept-langauge" (getf (lack.request:request-env ningle:*request*) :headers)))) (defun parse-accept-language (paremeter) (mapcar #'car (sort (mapcar (lambda (param) (ppcre:split ";q=" param)) (ppcre:split #\, paremeter)) #'> :key (lambda (x) (read-from-string (or (cadr x) "1")))))) (defun find-locale (locale) (and cl-locale.core::*dictionary* (gethash locale cl-locale.core::*dictionary*))) (defun find-acceptable-locale (locales) (find-if #'find-locale locales))