Do you ever look for the translation of a word by looking it up on Wikipedia, then clicking on "Languages" and hovering on the language you're interested in to see the translation?
This snippet of code automates it for you, inside Emacs.
It creates a user-defined set of functions like en-fr
, which translates from English to French, or de-pt
, which translates from German to Portuguese.
By default, a prefix is put before these functions, to comply with Emacs Lisp Coding Conventions, so they are accessible through wt-en-de
, wt-en-ru
and so on: but I recommend setting this prefix (wt-prefix
) to nil if you can, for a more direct access.
Calling the functions with C-u opens the Wikipedia page of the translation.
Autocompletion is available (I recommand helm).
- How do you say rabbit in French ?
M-x
en-fr
rabbit
gives you the answer. (wt-en-fr
if you did not setwt-prefix
to nil). M-x
en-eo
Russia
looks for the translation of Russia in Esperanto and displays the result (Rusio) in the minibuffer.C-u
M-x
pt-ja
Coimbra
looks for the japanese translation of the portuguese name Coimbra, displays it in the minibuffer, and opens the japanese page dedicated to it.
Reload wiki-translate.el (or Emacs) for modifications to take effect.
I suggest setting wt-prefix
to nil, to be able to access the functions directly through their two-letter codes (es-en
), without having to type in the prefix (wt-es-en
).
Default languages are English (en), Esperanto (eo), French (fr), German (de), Japanese (ja), Mandarin Chinese (zh), Portuguese (pt).
You can add or remove any language you want through the variable wt-languages
. It uses the ISO 639-1 code of the languages (generally two letters : en
for english, pt
for portuguese, etc.), provided that the Wikipedia in this language exists and is known under this name (which is generally the case, but have a check at the List Of Wikipedias) if in doubt).
For each language pair, the code automatically creates a function to translate from and to this language. For instance, if you specify English (en), German (de) and Russian (ru), it creates the functions en-de
, en-ru
, de-en
,de-ru
, ru-en
, and ru-de
.
Wiki-translate is written entirely in Elisp, and uses the following Wikipedia APIs:
-
Word selection and auto-completion in the input language: listing API.
Example: Complete the word "Foundatio" in english:
https://en.wikipedia.org/w/api.php?action=query&list=allpages&apprefix=Foundatio&formatversion=2&aplimit=15&format=json -
Translation: query API on the input language Wikipedia.
Example: Translate the french town "Vérone" in english:
https://fr.wikipedia.org/w/api.php?action=query&prop=langlinks&titles=Vérone&lllang=en&formatversion=2&redirects&format=json
- Find a way to unset
debug-on-error
insidegeneric-interactive-wiki-translate
(see code) - Evaluate if https://en.wikipedia.org/w/api.php?action=opensearch&search=Ivan%C2%A0Ill isn't a better API for the input language. That's the one used by helm-wikipedia.
- Should we offer to open the Wikipedia page in the browser after the translation?
- Code cleaning: should-we split get-ws-json in the same way it was done on this project?
- Should we rename all functions with a customizable string prefix, by default wiki-translate?