note 62586 added to function.gettext
| From: | vinaykuruvilaatgmaildotcom at osu1 dot php dot net | Date: | Fri, 03 Mar 2006 22:55:19 +0000 |
| Subject: | note 62586 added to function.gettext | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-104794@lists.php.net to get a copy of this message | ||
If like me, you are stuck with making a lot of code localizable, you have to go through all your php
files and wrap all srings in _("string"). Here's an elisp function which can help you
out.
This function enables you to highlight some text in an emacs buffer and make it a localizable string
using the keyboard shortcut C-l (Ctrl and l). If the first character highlighted is " or
', then it assumes the text is in php-context and changes it to: _(HIGHLIGHTED_TEXT). Otherwise
it assumes the text is in html-context and changes it to <?=_('HIGHLIGHTED_TEXT')?>
The shortcut C-k can be used for translating parts of php strings which contain html tabs. We dont
want to translate the entire string including the tabs, so we highlight just the substring that
needs to be translated and use C-k.
To use it, do either of:
Copy and paste the following code into your .emacs file. This would permanently associate the
keyboard shortcut C-l with this function.
Save the code in a new file ending with the .el extension. Evaluate it using M-x eval-buffer. This
makes the C-l keyboard shortcut only last for the current Emacs session.
Code
;author: Vinay Kuruvila March 01 2006
;updated to handle php strings containing html tabs
;makes the text starting at left and ending at right in the
;current buffer a localizable string, assuming that the
;string is within php context
(defun make-localizable-string-in-php-context(left right)
(goto-char left)
(insert "_(")
(goto-char (+ right 2))
(insert ")")
)
;makes the text starting at left and ending at right in the
;current buffer a localizable string, assuming that the
;string is within html context
(defun make-localizable-string-in-html-context(left right)
(goto-char left)
(insert "<?= _('")
(goto-char (+ right 7))
(insert "')?>")
)
;makes the highlighted text a localizable string
;uses php-context localization if the first char highlighted
;is " or '
;otherwise uses html-context localization
(defun make-localizable-string()
(interactive)
;find the positions of the left and right ends of
;the highlighted text
(if (> (point) (mark))
(progn
(setq right (point))
(setq left (mark))
)
(progn
(setq right (mark))
(setq left (point))
)
)
;determine php-context or html-context and dispatch
(if (or (char-equal (char-after left) ?\") (char-equal (char-after left) ?'))
(make-localizable-string-in-php-context left right)
(make-localizable-string-in-html-context left right)
)
(deactivate-mark)
)
;to handle php strings which contain html tabs
;we dont want to translate the html tabs
(defun make-localizable-string-within-php-string ()
(interactive)
;find the positions of the left and right ends of
;the highlighted text
(if (> (point) (mark))
(progn
(setq right (point))
(setq left (mark))
)
(progn
(setq right (mark))
(setq left (point))
)
)
(goto-char left)
(insert "\". _(\"")
(goto-char (+ right 6))
(insert "\").\"")
(deactivate-mark)
)
;assigns a keyboard shortcut
(global-set-key "\C-l" 'make-localizable-string)
(global-set-key "\C-k" 'make-localizable-string-within-php-string)
----
Server IP: 216.194.113.175
Probable Submitter: 128.32.157.241
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.gettext.php
Edit -- http://master.php.net/note/edit/62586
Del: integrated -- http://master.php.net/note/delete/62586/integrated
Del: useless -- http://master.php.net/note/delete/62586/useless
Del: bad code -- http://master.php.net/note/delete/62586/bad+code
Del: spam -- http://master.php.net/note/delete/62586/spam
Del: non-english -- http://master.php.net/note/delete/62586/non-english
Del: in docs -- http://master.php.net/note/delete/62586/in+docs
Del: other reasons-- http://master.php.net/note/delete/62586
Reject -- http://master.php.net/note/reject/62586
Search -- http://master.php.net/manage/user-notes.php