atykhonov / google-translate

Emacs interface to Google Translate
368 stars 48 forks source link

Display romaji #147

Open anonimitoraf opened 2 years ago

anonimitoraf commented 2 years ago

This is awesome!

Is there a way to display Romaji for Japanese translations?

stardiviner commented 2 years ago

No plan to add that yet, but PR with testing is welcome.

anonimitoraf commented 2 years ago

kana-to-romaji

Here's my initial shot at it. Although, I'll need to clean up the code since it's ATM coupled with posframe and doom

(defvar ++google-translate-kana->romaji-buffer "*Google Translate kana->romaji*")

(defun ++google-translate-kana->romaji (kana)
  (replace-regexp-in-string "\r?\n" ""
    (shell-command-to-string
     (format "echo \"%s\" | iconv -f utf8 -t eucjp | kakasi -i euc -w | kakasi -i euc -Ha -Ka -Ja -Ea -ka" kana))))

(defun ++google-translate-curr-region-kana->romaji ()
  (when (equal (buffer-name) "*Google Translate*")
    (if (region-active-p)
      (posframe-show ++google-translate-kana->romaji-buffer
        :string (++google-translate-kana->romaji (buffer-substring-no-properties
                                                   (region-beginning)
                                                   (region-end)))
        :position (point)
        :border-color (doom-color 'yellow)
        :border-width 1
        :foreground-color (doom-color 'yellow))
      (posframe-hide ++google-translate-kana->romaji-buffer))))

(defun ++google-translate-setup-hook ()
  (add-hook 'post-command-hook #'++google-translate-curr-region-kana->romaji))

(++google-translate-setup-hook)
stardiviner commented 2 years ago

Interesting idea, looking at your code, it seems requires external command-line tools. Are they easily retrievable and installed for users? Also, you need to make posframe requirement optional. And provide this feature in a defcustom option for users to toggle it. (Disabled by default, because it's hardcoed requirements, I don't know whether it's suitable, @atykhonov What do you think?).

anonimitoraf commented 2 years ago

Yep agreed with those points. I'll make everything optional