Clickable words that show a tooltip with their definition, fetched live from the Wiktionary API – no external library required.
Include it in your own course:
<!-- pinned (stable) -->
import: https://raw.githubusercontent.com/LiaTemplates/Wictionary/0.0.2/README.md
<!-- latest (may change) -->
import: https://raw.githubusercontent.com/LiaTemplates/Wictionary/master/README.md
Write @wik(word) anywhere in the text, then click the word. Without an
explicit language, the word is looked up in the document language
(<html lang="…">, which LiaScript derives from the course's language:
meta). This template is language: en, so:
A @wik(house) usually has a @wik(roof), several @wik(window) and a @wik(door). Next to it a @wik(tree) might grow.
Definitions are fetched live from
en.wiktionary.org.Translation aware: when the page is translated, LiaScript overwrites
<html lang>and the visible word changes too. For the plain@wik(word)form the lookup uses the currentinnerTextand document language, so the tooltip always matches the translated term. (@wik(word, xx)and@wikdkeep their fixed term/language.)
Pass a language code as the second argument to force a specific Wiktionary, regardless of the document language. This lets you mix several languages on the same page:
German: ein @wik(Haus, de) mit @wik(Dach, de), @wik(Fenster, de) und @wik(Tür, de).
French: une @wik(maison, fr) avec une @wik(porte, fr).
English: a @wik(house, en) with a @wik(roof, en).
For English the REST endpoint even returns structured definitions (grouped by part of speech). Every other language is read from the
Bedeutungen:/Meanings:section of the Action API.
Sometimes the displayed text should differ from the lookup term (e.g. an
inflected form). Use @wikd(term, language, display text) – leave the
language empty to keep the document language:
These old @wikd(house, en, houses) need repair – die @wikd(Haus, de, Häuser) in dieser @wikd(Straße, de, Straße) sind alt.
The default language is resolved per word, in this order:
- an explicit language on the macro –
@wik(word, de),@wikd(word, de, …) - an author-pinned default – set it once in your course:
window.liaWikDefaultLang = "fr"
- the document language –
<html lang="…">, from yourlanguage:meta enas a last resort
So if your course header says language: de, a plain @wik(Haus) already
queries de.wiktionary.org – without any extra configuration.
All behavior lives in the header comment of this file:
-
@onload– runs once (also in importing courses) and- injects the CSS (an
@styleblock would not be carried over on import, so it is written into<head>via JS), - creates a reusable tooltip box,
- registers a delegated click listener on
documentthat handles every.lia-wikword (survives slide changes), - fetches the data via
fetch(origin=*⇒ CORS ok) and caches it.
- injects the CSS (an
-
Macros only emit a
<span class="lia-wik" data-term=… data-lang=…>.
@wik: <span class="lia-wik" data-term="@0" data-lang="@1" data-auto tabindex="0">@0</span>
@wikd: <span class="lia-wik" data-term="@0" data-lang="@1" tabindex="0">@2</span>
@wik(Haus) leaves @1 empty, @wik(Haus, de) fills it. In JS the value
is only accepted when it matches a real language code (/^[a-z]{2,3}$/),
so an empty or unexpanded parameter safely falls back to the document
language.
The data-auto marker flags the language-less @wik form. Only then – and
only while no explicit language is given – the lookup follows the live
innerText and <html lang>, which is what makes it translation aware.
@wikd (display text ≠ lookup term) never carries the marker.
| Language | API | Result |
|---|---|---|
en |
…/api/rest_v1/page/definition/<word> |
structured (by part of speech) |
| every other | …/w/api.php?action=query&prop=extracts&explaintext |
the senses section |