Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching #125

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .bump-version.el
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
"google-translate-default-ui.el"
"google-translate-pkg.el"
"google-translate-smooth-ui.el"
"google-translate-cache.el"
"google-translate-cache-ui.el"
"Makefile"))
(:current-version "0.12.0"))
(:current-version "0.12.1"))
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ displayed if available. If you want to see the phonetics, set this
variable to t.

The variable `google-translate-listen-program` determines the program
to use to listen translations. By default the program looks for
to use to listen to translations. By default the program looks for
`mplayer` in the PATH, if `mplayer` is found then listening function
will be available and you'll see `Listen` button in the buffer with
the translation. You can use any other suitable program. If you use
Expand Down Expand Up @@ -351,6 +351,34 @@ Additionally, these variables would be useful for troubleshooting:
buffer `*google-translate-backend-debug*`
(defaults to nil)

## Cache

Translation results may be cached by setting `google-translate-use-cache`
to `t` (default). Only the text is cached, not the audio from `[Listen]`.

Some customization variables:

- `google-translate-cache-files-per-language` specifies how many files
may be used for storing the cache on the disk per language pair
(for incremental loading and saving),

- `google-translate-cache-word-limit` specifies maximum words a request
(w/out the translation) may have in order to be cached. `nil` for no
limit.

- `google-translate-cache-downcase-requests` indicates whether the text should
be downcased before translation (when it doesn't exceed
`google-translate-cache-word-limit`, that is) (defaults to `t`),

- `google-translate-cache-directory` is the directory where the cache is to
be saved (defaults to `~/.emacs.d/var/translate-cache`).

For batch word caching (such as vocabulary), see
`google-translate-cache-words-in-region` and
`google-translate-cache-words-in-buffer`.

`google-translate-cache-save` is added to `kill-emacs-hook`.

## Contributors

- Tassilo Horn
Expand All @@ -361,3 +389,4 @@ Additionally, these variables would be useful for troubleshooting:
- [Michihito Shigemura](https://github.com/shigemk2)
- [Tomotaka SUWA](https://github.com/t-suwa)
- [stardiviner](https://github.com/stardiviner)
- Dmitrii Korobeinikov
92 changes: 92 additions & 0 deletions google-translate-cache-ui.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
;;; google-translate-cache-ui.el --- Caching translation results UI

;; Copyright (C) 2012 Oleksandr Manzyuk <[email protected]>

;; Author: Oleksandr Manzyuk <[email protected]>
;; Maintainer: Andrey Tykhonov <[email protected]>
;; URL: https://github.com/atykhonov/google-translate
;; Package-Requires: ((emacs "24.3") (popup "0.5.8"))
;; Version: 0.12.1
;; Keywords: convenience

;; Contributors:
;; Tassilo Horn <[email protected]>
;; Bernard Hurley <[email protected]>
;; Chris Bilson <[email protected]>
;; Takumi Kinjo <[email protected]>
;; momomo5717 <[email protected]>
;; stardiviner <[email protected]>
;; Dmitrii Korobeinikov <[email protected]>

;; This file is NOT part of GNU Emacs.

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

;; This file 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
;; General Public License for more details.

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

;;; Commentary:

;; UI for caching.
;;
;; `google-translate-cache-words-in-buffer' - retreives and caches
;; translations for every word in the buffer.
;;
;; `google-translate-cache-words-in-region' - retreives and caches
;; translations for every word in the active region.
;;

;;; Code:

(eval-when-compile (require 'cl-lib))
(require 'google-translate-default-ui)

(defun %google-translate-cache-words-in-region (beg end &optional override-p)
"Translate and cache words between BEG and END.

For the meaning of OVERRIDE-P, see `google-translate-query-translate'."
(let ((langs (google-translate-read-args override-p nil))
(word 1)
(total (count-words beg end)))
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(while (forward-word-strictly 1)
(message "Processing word %d/%d." word total)
(save-excursion
(google-translate-translate
(car langs)
(cadr langs)
(let ((b (bounds-of-thing-at-point 'word)))
(buffer-substring-no-properties (car b) (cdr b)))))
(cl-incf word))))))


;;;###autoload
(defun google-translate-cache-words-in-region (beg end &optional override-p)
"Cache translations for all words in active region (from BEG to END).

For the meaning of OVERRIDE-P, see `google-translate-query-translate'."
(interactive "rP")
(%google-translate-cache-words-in-region beg end override-p))

;;;###autoload
(defun google-translate-cache-words-in-buffer (&optional override-p)
"Cache translations for all words in current buffer.

For the meaning of OVERRIDE-P, see `google-translate-query-translate'."
(interactive "P")
(%google-translate-cache-words-in-region (buffer-end -1) (buffer-end 1) override-p))

(provide 'google-translate-cache-ui)

;;; google-translate-cache-ui.el ends here
Loading