al-google-translate.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ;;; al-google-translate.el --- Additional functionality for google-translate
  2. ;; Copyright © 2013-2016 Alex Kost
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;; This program is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ;;; Code:
  14. (require 'google-translate-default-ui)
  15. (require 'google-translate-smooth-ui)
  16. (require 'al-read)
  17. ;;; Default UI
  18. (defun al/%google-translate (override-p reverse-p)
  19. "Translate region or prompting text.
  20. Alternative to `%google-translate-query-translate' and
  21. `%google-translate-at-point'."
  22. (let* ((langs (google-translate-read-args override-p reverse-p))
  23. (source-language (car langs))
  24. (target-language (cadr langs)))
  25. (google-translate-translate
  26. source-language target-language
  27. (if (use-region-p)
  28. (buffer-substring-no-properties (region-beginning) (region-end))
  29. (al/read-string
  30. (format "Translate from %s to %s: "
  31. (google-translate-language-display-name source-language)
  32. (google-translate-language-display-name target-language))
  33. nil nil (current-word t t))))))
  34. ;;;###autoload
  35. (defun al/google-translate (&optional override-p)
  36. "Translate region or prompting text from auto detected language.
  37. For the meaning of OVERRIDE-P, see `google-translate-query-translate'."
  38. (interactive "P")
  39. (let ((google-translate-default-source-language "auto"))
  40. (al/%google-translate override-p nil)))
  41. ;;;###autoload
  42. (defun al/google-translate-direct ()
  43. "Translate region or prompting text from source to target language.
  44. For details look at `google-translate-query-translate'."
  45. (interactive)
  46. (al/%google-translate nil nil))
  47. ;;;###autoload
  48. (defun al/google-translate-reverse ()
  49. "Translate region or prompting text from target to source language.
  50. For details look at `google-translate-query-translate'."
  51. (interactive)
  52. (al/%google-translate nil t))
  53. ;;; Smooth UI
  54. ;;;###autoload
  55. (defun al/google-translate-smooth-translate ()
  56. "Translate a text using translation directions.
  57. Similar to `google-translate-smooth-translate', but prompt for
  58. languages (if needed) before text."
  59. (interactive)
  60. (setq google-translate-translation-direction-query
  61. (when (use-region-p)
  62. (google-translate--strip-string
  63. (buffer-substring-no-properties
  64. (region-beginning) (region-end)))))
  65. (let ((google-translate-translation-directions-alist
  66. google-translate-translation-directions-alist)
  67. (google-translate-current-translation-direction
  68. google-translate-current-translation-direction))
  69. (unless google-translate-translation-directions-alist
  70. (let ((source (google-translate-read-source-language))
  71. (target (google-translate-read-target-language)))
  72. (setq google-translate-current-translation-direction 0
  73. google-translate-translation-directions-alist
  74. (list (cons source target)
  75. (cons target source)))))
  76. (let ((text (google-translate-query-translate-using-directions)))
  77. (google-translate-translate
  78. (google-translate--current-direction-source-language)
  79. (google-translate--current-direction-target-language)
  80. text))))
  81. ;;;###autoload
  82. (defun al/google-translate-using-languages (source target)
  83. "Translate a text using SOURCE and TARGET languages."
  84. (let ((google-translate-translation-directions-alist
  85. (list (cons source target)
  86. (cons target source))))
  87. (al/google-translate-smooth-translate)))
  88. (provide 'al-google-translate)
  89. ;;; al-google-translate.el ends here