1 ;;; globalizer.el -- Add your lovely globalize .t with style
3 ;; Author: Ed Sinjiashvili <mediogre@gmail.com>
4 ;; Keywords: rails i18n
7 ;; Scan through a buffer to find strings eligible for translation.
8 ;; Allow user to observe changes and make an educated choice: approve, veto,
9 ;; quit or make changes automatically. Basically it does everything
10 ;; query-replace does, but calls user-defined hook when replace
13 ;; In other words if you choose the string to be translatable, it will
14 ;; add .t to it (make it globalized) and also add the string to
15 ;; another buffer which you can later give to a native translator to
16 ;; create actual translations.
19 (defconst globalize
:buffer-extension
".globalines"
20 "extension to append to buffer being globalized")
23 ;; - find a way to skip literals with .t (kinda hard without lookahead support)
24 ;; - maybe match more complex ruby strings
26 (defconst globalize
:string-literal-regexp
27 "\\(['\"]\\)\\(.*?\\)\\1"
28 "a simple ruby literal string regexp")
30 (defconst globalize
:replacement-string
32 "just add .t method call")
34 (defun globalize:get-buffer-name
()
35 "get the name for a file used for storing globalized strings"
36 (concat (buffer-file-name) globalize
:buffer-extension
))
38 (defun globalize:get-buffer
()
39 "get buffer with globalized strings"
41 (let ((buffer (find-buffer-visiting (globalize:get-buffer-name
))))
43 (setq buffer
(find-file-noselect (globalize:get-buffer-name
)))
46 (defun globalize:append-line
(str)
47 "append `str' to globalized buffer"
50 (set-buffer (globalize:get-buffer
))
51 (goto-char (point-max))
55 (defun globalize:append-matched-string
(&optional pos
)
56 "Append a matched (or some part of) string to globalize buffer"
58 (globalize:append-line
(match-string-no-properties pos
)))
61 (defun globalize:query-globalize
()
62 "Globalize current buffer. Add .t to candidate lines and dump them
63 to corresponding .globalines buffer for later use."
65 (ad-unadvise 'replace-match-maybe-edit
)
67 (defadvice replace-match-maybe-edit
68 (before globalize
:run-replaced-hook
(newtext fixedcase literal noedit match-data
))
69 "Insert a matched (and approved) string to the globalize buffer"
70 (run-hooks 'globalize
:replaced-hook
))
72 (ad-activate 'replace-match-maybe-edit
)
74 ; clean all hooks on definition
75 (set 'globalize
:replaced-hook nil
)
77 (add-hook 'globalize
:replaced-hook
'globalize
:append-matched-string
)
79 (let ((prev-case-fold case-fold-search
))
80 (setq case-fold-search nil
)
83 globalize
:string-literal-regexp
84 globalize
:replacement-string
)
86 (setq case-fold-search prev-case-fold
))
87 (pop-to-buffer (globalize:get-buffer
)))
91 ;;; globalizer.el ends here