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
)))
60 ;; can't make it work -
61 ;; (defmacro globalize:save-case-fold (&rest body)
62 ;; "save current `case-fold-search' value and evaluate expression with
63 ;; `case-fold-search' set to nil"
64 ;; `(let ((prev-case-fold case-fold-search)
66 ;; (setq case-fold-search nil)
67 ;; (setq result (progn ,body))
68 ;; (setq case-fold-search prev-case-fold)
73 (defun globalize:query-globalize
()
74 "Globalize current buffer. Add .t to candidate lines and dump them
75 to corresponding .globalines buffer for later use."
77 (ad-unadvise 'replace-match-maybe-edit
)
79 (defadvice replace-match-maybe-edit
80 (before globalize
:run-replaced-hook
(newtext fixedcase literal noedit match-data
))
81 "Insert a matched (and approved) string to the globalize buffer"
82 (run-hooks 'globalize
:replaced-hook
))
84 (ad-activate 'replace-match-maybe-edit
)
86 ; clean all hooks on definition
87 (set 'globalize
:replaced-hook nil
)
89 (add-hook 'globalize
:replaced-hook
'globalize
:append-matched-string
)
91 (let ((prev-case case-fold-search
))
92 (setq case-fold-search nil
)
94 globalize
:string-literal-regexp
95 globalize
:replacement-string
)
96 (setq case-fold-search prev-case
))
98 (ad-unadvise 'replace-match-maybe-edit
)
100 (pop-to-buffer (globalize:get-buffer
)))
102 (defun globalize:globalize-region
(beg end
)
103 "Globalize selected region: erbify, .tify and add to globalines"
105 (globalize:append-line
(buffer-substring-no-properties beg end
))
107 (let ((prev-case case-fold-search
))
108 (setq case-fold-search nil
)
109 (replace-regexp ".*" "<%= '\\&'.t %>" nil beg end
)
110 (setq case-fold-search prev-case
))
112 ;(pop-to-buffer (globalize:get-buffer))
115 (provide 'globalizer
)
117 ;;; globalizer.el ends here