unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Sebastian Tennant <sebyte@smolny.plus.com>
To: help-gnu-emacs@gnu.org
Subject: Re: French->English translation?
Date: Tue, 22 Jan 2008 23:35:52 +0200	[thread overview]
Message-ID: <87tzl5r1af.fsf@moley.moleskin.org> (raw)
In-Reply-To: fn0qdh$t3i$1@reader2.panix.com

Quoth kj <socyl@987jk.com.invalid>:
> Are there any language translation modules for Emacs?  

Not that I know of.

> I'm thinking of something analogous to ispell but instead of providing
> help with spelling, it provides possible translations in the selected
> target language.  I'm particularly interested in tools to help me when
> I write French.  (I need more than spelling help in French; oftentimes
> I don't know or can't remember the word in French for what I want to
> say.)

I know the feeling.  My solution is to use dict and my own little Emacs
interface.  It's nothing like ispell of course, but it does provide
pretty rapid and convenient dictionary access in Emacs (with keyword
highlighting, automatic activating of an input method and multiple
specified dictionary search functionality).  Not bad for a couple of
dozen lines, even if I do say so myself!

Assuming you're running an apt-based Linux distro, do:

 $ apt-get install dict dict-freedict-eng-fra dict-freedict-fra-eng

This provides the 'dict' command-line program and two dictionaries;
English -> French and French -> English.

Add the lines:

 server localhost
 pager -

to /etc/dictd/dict.conf.

You've installed the dictionaries locally (so you don't need network
access for it to work) and Emacs is going to be our pager.

Now add the following to your ~/.emacs:

----------------8<----------------8<----------------8<----------------8<
 ;;; dict.el
 (defun dict (host databases strategy word)
   (interactive "sHost: \nsDatabase(s): \nsStrategy: \nsWord: ")
   ;; set up buffer
   (set-buffer (get-buffer-create "*dict*"))
   (text-mode) (erase-buffer) (setq font-lock-keywords-case-fold-search t)
   ;; highlight command-line
   (font-lock-add-keywords nil '(("^\\$ .*$" 0 font-lock-keyword-face prepend)))
   ;; highlight word
   (let ((match-string word))
     (and (equal strategy "prefix") (setq match-string (concat word "[a-z]*")))
     (font-lock-add-keywords nil `((,match-string . font-lock-variable-name-face))))
   ;; force font-lock-mode
   (font-lock-mode 1)
   ;; construct separate queries for each database in databases
   (let ((database-list (split-string databases)))
     (mapc (lambda (d)
             (let ((commstr (concat " -h " host " -d " d " -s " strategy " " word)))
               (insert (format "$ dict%s\n" commstr))
               (apply 'call-process "dict" nil t nil (split-string commstr))
               (insert "\n")))
           database-list))
   ;; display
   (goto-char (point-min))
   (view-mode)
   (display-buffer "*dict*"))

 ;;; $ dict -h localhost -D   to see what dictionaries are installed locally

 ;;; search French dictionaries (English -> French and French -> English)
 (defun fw (word)
   (interactive
    (with-temp-buffer
      (activate-input-method "french-postfix")
      (list (read-from-minibuffer "Word: " nil nil nil nil nil t))))
   (let (strat)
     (if current-prefix-arg (setq strat "prefix") (setq strat "exact"))
     (dict "localhost" "fd-fra-eng fd-eng-fra" strat word)))

----------------8<----------------8<----------------8<----------------8<

We have defined two functions:

 dict - a 'generic' interface to dict
 fw   - a French specific 'shortcut' interface to dict, via our dict

When fw is called without a prefix argument, only exact matches are
returned.  When fw is called with a prefix argument, words that _begin_
to match are returned.

So, for example, without prefix argument:

 M-x fw aim <RET>
 ----------------------------------------------------
 $ dict -h localhost -d fd-fra-eng -s exact aim
 No matches found for "aim"

 $ dict -h localhost -d fd-eng-fra -s exact aim

 From English-French Freedict dictionary [fd-eng-fra]:

   aim [eim]
      avoir pour but; viser
      but; dessein
      se démener
      peiner
 ----------------------------------------------------

And with a prefix argument:

 C-u M-x fw aim <RET>
 ----------------------------------------------------
 $ dict -h localhost -d fd-fra-eng -s prefix aim

 From French-English Freedict dictionary [fd-fra-eng]:

   aimable [ɛmabl]
      affable; friendly; good‐natured; kind
      dainty; kind; nice; pretty


 From French-English Freedict dictionary [fd-fra-eng]:

   aimer [ɛme]
      love
      appreciate; like


 From French-English Freedict dictionary [fd-fra-eng]:

   aimer mieux [ɛmemjø]
      prefer


 $ dict -h localhost -d fd-eng-fra -s prefix aim

 From English-French Freedict dictionary [fd-eng-fra]:

   aim [eim]
      avoir pour but; viser
      but; dessein
      se démener
      peiner
 ----------------------------------------------------

There's a 'proper' emacs interface to dict called dictem.el, but it
struck me as unecessarily complicated.  Also, my interface queries each
of a list of specific dictionaries in turn which dictem.el doesn't
provide for.

IMHO it's a serious limitation of dict that it only allows you to
specify _one_ particular dictionary on the command line, not two or
three or four, and if no dictionaries are specified it searches _all_ of
the dictionaries on the server, which, in my experience, is rarely what
you want.

Anyway, hope it helps.

Sebastian

  reply	other threads:[~2008-01-22 21:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-21  0:53 French->English translation? kj
2008-01-22 21:35 ` Sebastian Tennant [this message]
     [not found] ` <mailman.6424.1201037768.18990.help-gnu-emacs@gnu.org>
2008-01-23 14:23   ` kj

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87tzl5r1af.fsf@moley.moleskin.org \
    --to=sebyte@smolny.plus.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).