-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
language.lisp
163 lines (144 loc) · 6.16 KB
/
language.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
(in-package #:org.shirakumo.fraf.trial)
(defvar *language-change-hooks* '())
(defvar *languages* (make-hash-table :test 'equalp))
(define-global +loaded-language+ NIL)
(define-global +language-data+ NIL)
(defun languages ()
(mapcar #'pathname-utils:directory-name
(directory (pathname-utils:merge-pathnames* #p"lang/*/" (data-root)))))
(defun language ()
(or +loaded-language+
(load-language :language (or (setting :language) :eng))))
(defun try-find-language (language)
(etypecase language
((member NIL :system)
(let ((languages (system-locale:languages)))
(if languages
(try-find-language languages)
(try-find-language "eng"))))
(symbol
(try-find-language (string-downcase language)))
(cons
(loop for candidate in language
thereis (try-find-language candidate)))
(string
(flet ((try (language)
(when (language-files language)
(return-from try-find-language (string-downcase language)))))
(try language)
(mapc #'try (language-codes:codes language))
(let ((name (first (language-codes:names language))))
(when name
(mapc #'try (language-codes:codes name))))
(try "eng")
(try (first (languages)))))))
(defun language-dir (&optional (language (language)))
(pathname-utils:merge-pathnames*
(make-pathname :directory `(:relative "lang" ,(string-downcase language)))
(data-root)))
(defun language-files (&optional (language (language)))
(directory (make-pathname :name :wild :type "sexp" :defaults (language-dir language))))
(defmacro define-language-change-hook (name args &body body)
(let ((name (mksym *package* '%language-change-hook name)))
`(progn
,(if args
`(defun ,name ,args
,@body)
`(defun ,name (language)
(declare (ignorable language))
,@body))
(pushnew ',name *language-change-hooks*))))
(defun load-language-file (file table &optional (package *package*))
(depot:with-open (tx file :input 'character)
(with-trial-io-syntax (package)
(loop with stream = (depot:to-stream tx)
for k = (read stream NIL)
for v = (read stream NIL)
while k
do (setf (gethash k table) v))
table)))
(defvar *recursive-load* NIL)
(defun load-language (&key (language (setting :language)) replace (package *package*))
(let ((table (if (null +language-data+)
(make-hash-table :test 'eq)
+language-data+))
(corrected-language (try-find-language language)))
(if corrected-language
(setf language corrected-language)
(error "Could not find any suitable language for ~a" language))
(when (and (or replace (null +loaded-language+) (not (equalp +loaded-language+ language)))
(not (equalp language *recursive-load*)))
(setf language (string-downcase language))
(v:info :trial.language "Loading language ~s from ~a" language (language-dir language))
(let ((files (language-files language)))
(cond (files
(dolist (file files)
(load-language-file file table package))
(setf (gethash language *languages*) table))
((null (gethash language *languages*))
(error "No language named ~s found." language))))
(setf +language-data+ (gethash language *languages*))
(setf +loaded-language+ language)
(let ((*recursive-load* language))
(dolist (hook *language-change-hooks* table)
(funcall hook language))))
language))
(defun save-language (file &key (language (language)) (package *package*))
(when +language-data+
(setf language (string-downcase language))
(v:info :trial.language "Saving language ~s to ~s" language (language-dir language))
(with-trial-io-syntax (package)
(let ((order-table (make-hash-table :test 'eq))
(i 0))
(depot:with-open (tx file :input 'character :if-does-not-exist NIL)
(when tx
(loop with stream = (depot:to-stream tx)
for k = (read stream NIL)
while k
do (read stream NIL)
(setf (gethash k order-table) (1- (incf i))))))
(loop for k being the hash-keys of +language-data+
do (unless (gethash k order-table)
(setf (gethash k order-table) (1- (incf i)))))
(depot:with-open (tx file :output 'character)
(loop with stream = (depot:to-stream tx)
for (k) in (sort (alexandria:hash-table-alist order-table) #'< :key #'cdr)
for v = (gethash k +language-data+)
do (format stream "~s ~s~%" k v)))))))
(defun language-string (identifier &optional (errorp T))
(unless +language-data+ (load-language))
(or (gethash identifier +language-data+)
(unless identifier "NIL")
(unless errorp (return-from language-string NIL))
;; Try loading again in case things changed.
(progn (load-language :language +loaded-language+ :replace T)
(gethash identifier +language-data+))
(restart-case
(error "No language string defined for ~s" identifier)
(retry ()
:report "Retry now."
(language-string identifier))
(use-value (value)
:report "Supply a string to use."
value))))
(defun (setf language-string) (string identifier)
(unless +language-data+ (load-language))
(check-type string string)
(check-type identifier symbol)
(setf (gethash identifier +language-data+) string))
(defun ensure-language-string (thing)
(etypecase thing
(string thing)
(symbol (language-string thing))))
(define-setting-observer load-language :language (value)
(load-language :language value))
(defun @format (destination identifier &rest args)
(format destination "~?" (language-string identifier) args))
(defun @formats (identifier &rest args)
(format NIL "~?" (language-string identifier) args))
(defmacro @ (identifier)
`(language-string ',identifier))
(set-dispatch-macro-character
#\# #\@ (lambda (s c a)
(declare (ignore c a))
(language-string (read s T NIL T))))