Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Wictionary – LiaScript Template

Clickable words that show a tooltip with their definition, fetched live from the Wiktionary API – no external library required.

LiaScript

Try it out: https://liascript.github.io/course/?https://raw.githubusercontent.com/LiaTemplates/Wictionary/master/README.md

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

@wik(word) – document language

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 current innerText and document language, so the tooltip always matches the translated term. (@wik(word, xx) and @wikd keep their fixed term/language.)

@wik(word, language) – pin a language per word

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.

@wikd – custom display text

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.

Choosing the default language

The default language is resolved per word, in this order:

  1. an explicit language on the macro – @wik(word, de), @wikd(word, de, …)
  2. an author-pinned default – set it once in your course:
    window.liaWikDefaultLang = "fr"
  3. the document language – <html lang="…">, from your language: meta
  4. en as a last resort

So if your course header says language: de, a plain @wik(Haus) already queries de.wiktionary.org – without any extra configuration.

Implementation

All behavior lives in the header comment of this file:

  1. @onload – runs once (also in importing courses) and

    • injects the CSS (an @style block 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 document that handles every .lia-wik word (survives slide changes),
    • fetches the data via fetch (origin=* ⇒ CORS ok) and caches it.
  2. 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