書籍の翻訳のためのユーティリティを自前で実装している

翻訳作業を行うため、翻訳用のユーティリティを自前で実装している。

#+TITLE: trans.el - Translation utility.
#+CATEGORY: trans.el

trans.el is translation utility using by [[https://github.com/soimort/translate-shell][translation-shell]].
[[https://github.com/soimort/translate-shell/blob/develop/google-translate-mode.el][translate-shell has emacs extention]].  But this package do not use
google-translate-mode.el. Becouse it is created as a emacs lisp
plactice.

* How to use.

** Start process
=M-x trans-start RET=

** Translate

1. Mark region
2. =M-x trans-region RET=

If the translation is successful, the text will be displayed in
=*Trans*= buffer.


** Stop process
=M-x trans-stop RET=

* Install

1. Download trans.el
2. M-x package-install-file RET /PATH/TO/DOWNLOADED/trans.el RET

This package is not yet melpa packaging.

* Testing

This package has no test yet.

* Bug report and Contributing

We haven't prepared those flows yet. For the time being, please write
the steps to reproduce the bug in this gist comment, or attach the
patch file in diff format.  Welcome them.

* License

GPL v3

* Releases

** Version 3 (2023-04-09)

- support to change lang-mode function

** Version 2 (2023-01-26)

- Support Emacs 29.1

** Version 1 (2021-04-03)

- First implements.
- Supported translation.
  - en to ja

trans.el

;;; trans.el --- Translation functions -*- lexical-binding: t; -*-

;; Copyright (C) 2023 TakesxiSximada
;;
;; Author: TakesxiSximada <[email protected]>
;; Maintainer: TakesxiSximada <[email protected]>
;; Keywords: Translation
;; Homepage: https://github.com/TakesxiSximada/emacs.d/tree/main/lisp/trans
;; Version: 3

;; This file is not part of GNU Emacs.

;;; License:

;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Affero General Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with this program.  If not, see
;; <https://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; trans.el is translation utility using by translation-shell.
;;
;; Install by package.el
;; ---------------------
;; (This package is not yet melpa packaging.)
;;
;; 1. Download trans.el
;; 2. M-x package-install-file RET /PATH/TO/DOWNLOADED/trans.el RET

;; Testing
;; -------
;; This package has no test yet.

;; Bug report and Contributing
;; ---------------------------
;; We haven't prepared those flows yet. For the time being, please write the steps
;; to reproduce the bug in this gist comment, or attach the patch file in diff format.
;; Welcome them.

;;; Code:

(defvar trans-output-buffer-name "*Trans*")
(defvar trans-process-name "*Trans Process")
(defvar trans-process-buffer-name " *Transprocess*")
(defvar trans-process nil)

(defcustom trans-current-lang-mode "ja:en" "")
(defcustom trans-started-lang-mode "" "")

(defun trans-output (process output)
  (with-output-to-temp-buffer trans-output-buffer-name
    (princ output)))

(defun trans-get-sentence ()
  (if (region-active-p)
      `(buffer-substring-no-properties
	(region-beginning)
	(region-end))
    `(buffer-substring-no-properties
      (point-min)
      (point-max))))

(defun trans-cleanup-sentence (sentence)
  (with-temp-buffer
    (insert sentence)
    (delete-trailing-whitespace)
    (replace-regexp "\n" " " nil (point-min) (point-max))
    (buffer-substring-no-properties (point-min) (point-max))))

(defun trans-start ()
  (interactive)
  (unless trans-process
    (setq trans-process
	  (make-process :name trans-process-name
			:buffer (get-buffer-create trans-process-buffer-name)
			:command `("trans"
				   ,trans-current-lang-mode
				   "-show-original" "n"
				   "-show-prompt-message" "n"
				   "-brief"
				   "-no-pager"
				   "-no-ansi"
				   )
			:filter 'trans-output
			)
	  trans-started-lang-mode trans-current-lang-mode)))

;;;###autoload
(defun trans-switch-lang (lang-mode)
  (interactive (list (completing-read "sLang mode(BASE:TRNAS): " '("ja:en" "en:ja"))))
  (setq trans-current-lang-mode lang-mode))

;;;###autoload
(defun trans-restart ()
  (interactive)
  (trans-stop)
  (trans-start))

;;;###autoload
(defun trans-stop ()
  (interactive)
  (when trans-process
    (signal-process (get-process trans-process) 15)
    (setq trans-process nil
	  trans-started-lang-mode ""
	  )))

;;;###autoload
(defun trans-region ()
  (interactive)
  (unless (string-equal trans-current-lang-mode trans-started-lang-mode)
    (trans-stop))
  (trans-start)
  (if trans-process
      (process-send-string trans-process
			   (concat
			    (trans-cleanup-sentence
			     (eval (trans-get-sentence)))
			    "\n"))
    (error "No translation process")))

(provide 'trans)
;;; trans.el ends here

trans-autoloads.el

;;; trans-autoloads.el --- automatically extracted autoloads (do not edit)   -*- lexical-binding: t -*-
;; Generated by the `loaddefs-generate' function.

;; This file is part of GNU Emacs.

;;; Code:


;;;### (autoloads nil "trans" "trans.el" (0 0 0 0))
;;; Generated autoloads from trans.el

(autoload 'trans-switch-lang "trans" "\


\(fn LANG-MODE)" t)

(autoload 'trans-restart "trans" nil t)

(autoload 'trans-stop "trans" nil t)

(autoload 'trans-region "trans" nil t)

(register-definition-prefixes "trans" '("trans-"))

;;;***

;;; End of scraped data

(provide 'trans-autoloads)

;; Local Variables:
;; version-control: never
;; no-byte-compile: t
;; no-update-autoloads: t
;; no-native-compile: t
;; coding: utf-8-emacs-unix
;; End:

;;; trans-autoloads.el ends here