lorniu / go-translate

Translator on Emacs. Supports multiple engines such as Google, Bing, deepL, ChatGPT, StarDict, Youdao and so on.
GNU General Public License v3.0
274 stars 37 forks source link

Translate direction option? #32

Closed brogowski closed 2 years ago

brogowski commented 2 years ago

Hi,

I'm trying to write small function to get translation show up in minibuffer.

(use-package go-translate
  :custom
  (gts-translate-list '(("pl" "en"))))

(defun my/go-translate-at-point ()
  (interactive)
  (gts-translate (gts-translator
                  :picker (gts-noprompt-picker)
                  :engines (gts-google-engine)
                  :render (gts-message-render))))

(defclass gts-message-render (gts-render) ())

(cl-defmethod gts-out ((_ gts-message-render) task)
  (deactivate-mark)
  (with-slots (result ecode) task
    (if ecode
        (user-error "%s" result)
      (message (nth 0 (split-string result "\n"))))))

However it looks like language pairs are ordered alphabetically... I'm getting translation from "en" to "pl" instead of "pl" to "en".

Is there any way I can specify translate direction?

brogowski commented 2 years ago

Hmm, on the other hand function with gts-prompt-picker works only in reverse - asking always for "pl" to "en" translation.

(defun my/go-translate ()
  (interactive)
  (gts-translate (gts-translator
                  :picker (gts-prompt-picker)
                  :engines (gts-google-engine)
                  :render (gts-buffer-render))))
lorniu commented 2 years ago

The current picker mainly relies on gts-picker-lang-match-alist for language judgment.

I'm not familiar with pl, so it's not builtin. But you can add your rule by adding:

(add-to-list 'gts-picker-lang-match-alist
  (cons "pl" "[regexp to judge whether current input is pl]"))

Of course, this mechanism of automatic judgment is not perfect, because some languages are really difficult to distinguish.

Then, you can:

  1. use x in the result buffer to switch language

  2. customize your own pick path logic:

    (defmethod gts-path ((o gts-prompt-picker) text)
      ;; return a proper path for your own
      )

Or, you can try to provide PR to make the auto judgment more perfect.

brogowski commented 2 years ago

Thanks @lorniu - I've added simple regex for pl (it's latin based).

(add-to-list 'gts-picker-lang-match-alist
             (cons "pl" "^[a-zA-Z\u0105-\u017b,.;_ ]+$"))

and now it starts with translation from pl to en.

However, I don't think that's going to be useful for you since it is overriding en regex.

walseb commented 5 months ago

Thanks so much for this useful package!

I ran into this issue just now.

How would I make the gts-translator function use my custom gts-path function? Maybe I can submit it to:path, or is that only for cons paths?

All I want is for the path order to remain as it is when I submit it. That is, (cons "en" "sv") should go from EN -> SV, and (cons "sv" "en") should go from SV -> EN. I only use this package programmatically, not through the UI, so I can't hit x to switch.

Maybe there should be a global function for this.

Thoughts?

walseb commented 5 months ago

For context, here is the relevant code:

(gts-translate (gts-translator
                  :picker (gts-noprompt-picker :texter (my/gts-string-texter))
                  :engines (gts-bing-engine)
                  :render (my/gts-insert-render)
                  :path lang ;; (cons "en" "sv") or (cons "sv" "en"), both result in the same result currently: EN -> SV
                  ))