Elisp: Chinese Char To Reference Link
We need a command to change the character under cursor into several HTML links.
For example
我
becomes:
<div class="chineseXL"><span lang="zh">我</span> <span class="en"><a href="https://translate.google.com/#zh-CN|en|我">Translate</a> • <a href="https://en.wiktionary.org/wiki/我">Wiktionary</a></span></div>
Appears in browser like this:
This is useful for writing blog on languages and linguistics.
Solution
(defun xah-words-chinese-linkify () "Make the Chinese character before cursor into Chinese dictionary reference links. URL `http://xahlee.info/emacs/emacs/elisp_chinese_char_linkify.html' Created: 2020-11-24 Version: 2024-10-17" (interactive) (let ( (xtemplate "<div class=\"chineseXL\"><span lang=\"zh\">▮</span> <span class=\"en\"><a href=\"https://translate.google.com/#zh-CN|en|▮\">Translate</a> • <a href=\"https://en.wiktionary.org/wiki/▮\">Wiktionary</a></span></div>" ) (xchar (buffer-substring-no-properties (- (point) 1) (point)))) (delete-char -1) (insert (string-replace "▮" xchar xtemplate))))
This is truely a time saver.
2024-10-17 i no longer use this. Because Google has become extremely corrupt. You don't want to link to it. Nor Wikipedia or associated sites. All existing links to google translate have been removed on my sites.
URL Encoding of Chinese
Note: technically, Chinese chars in a URL should be URL Encoded.
For example:
http://en.wiktionary.org/wiki/中
should be:
http://en.wiktionary.org/wiki/%E4%B8%AD
(A Chinese character should become bytes in hexadecimal from the char's UTF-8 encoding. The char 中's UTF-8 encoding is 3 bytes of the following hexadecimal: E4 B8 AD.)
However, i think the situation of percent encoding is a abomination. 〔see Problems of Symbol Congestion in Computer Languages; ASCII Jam vs Unicode〕 I decided to not botch my Chinese chars in URL. This does not cause practical problems.
If you want to do that, see: Elisp: URL Percent Decode, Encode