/ src /
/src/i18n.lisp
1 (in-package :cl-smoke.qt.core)
2
3 (defun qt:tr (message &optional context)
4 "Returns the translated MESSAGE for CONTEXT or
5 a string STRING-EQUAL to MESSAGE when no translation was found.
6
7 Translations can be loaded with WITH-TRANSLATOR."
8 (qt:core-application.translate (or context "") message))
9
10 (defmacro with-installed-translator (translator &body body)
11 `(unwind-protect
12 (progn
13 (cxx:install-translator (qt:app) ,translator)
14 ,@body)
15 (cxx:remove-translator (qt:app) ,translator)))
16
17 (defmacro qt:with-translator ((base-name &rest paths) &body body)
18 "Loads the translations in the BASE-NAME_LANGCODE.qm file;
19 searching PATHS.
20
21 Must be in a WITH-APP."
22 (let ((translator (gensym)))
23 `(let ((,translator (make-instance 'qt:translator)))
24 (unless
25 (find-if #'(lambda (path)
26 (cxx:load ,translator
27 (format nil "~A_~A" ,base-name
28 (cxx:name (qt:locale.system)))
29 (namestring path)))
30 (list ,@paths))
31 (cerror "Ignore" "Loading the translations ~A for ~A failed."
32 ,base-name (cxx:name (qt:locale.system))))
33 (with-installed-translator ,translator
34 ,@body))))
35
36 (defmacro qt:with-libqt-translator (&body body)
37 "Loads the translations for the Qt library.
38
39 Must be in a WITH-APP."
40 (let ((translator (gensym)))
41 `(let ((,translator (make-instance 'qt:translator)))
42 (unless (cxx:load ,translator (format nil "qt_~A"
43 (cxx:name (qt:locale.system)))
44 (qt:library-info.location qt:library-info.+translations-path+))
45 (cerror "Ignore" "Loading the Qt library translations failed."))
46 (with-installed-translator ,translator
47 ,@body))))
48
49 (defun qt:search-file (name &rest paths)
50 "Searches the file NAME in PATHS and returns its path."
51 (let ((file-path (find-if #'(lambda (path)
52 (probe-file (merge-pathnames name path)))
53 paths)))
54 (unless file-path
55 (error "The file ~S not found in the paths ~S" name paths))
56 (merge-pathnames name file-path)))
57