181 lines
5.6 KiB
Lua
181 lines
5.6 KiB
Lua
-- i18n.lua ------------------------------------------------------------
|
|
--
|
|
-- Single-source multilingual notes. One .qmd per chapter holds every
|
|
-- language; the maths is written once, only the prose is split. The active
|
|
-- language comes from `lang:` in _quarto.yml (or a profile that overrides it),
|
|
-- so `quarto render --profile fr` produces the French book.
|
|
--
|
|
-- Two mechanisms, deliberately kept apart:
|
|
--
|
|
-- 1. PROSE -- a div or span carrying a language class is kept when it
|
|
-- matches the active language and dropped otherwise. The wrapper itself
|
|
-- disappears, so it adds no markup to the output.
|
|
--
|
|
-- ::: {.de}
|
|
-- Wir rechnen
|
|
-- :::
|
|
-- ::: {.fr}
|
|
-- Nous calculons
|
|
-- :::
|
|
--
|
|
-- ## [Die natürlichen Zahlen]{.de}[Les entiers naturels]{.fr} $\mathbb{N}$
|
|
--
|
|
-- 2. SHORT STRINGS -- an `@tr{key}` token is replaced by the translation of
|
|
-- `key`, looked up in the `i18n:` map of i18n.yml. Works inside maths,
|
|
-- raw HTML, attribute values, image paths and ordinary text, i.e. all the
|
|
-- places a div cannot reach:
|
|
--
|
|
-- $$ \underset{\text{@tr{prime}}}{2} $$
|
|
-- :::{.gls-def key=N term="@tr{term-N}"}
|
|
-- 
|
|
--
|
|
-- Frontmatter titles follow the same suffix rule as any metadata key: a
|
|
-- `title-de:` / `title-fr:` / `title-en:` set collapses to `title:`.
|
|
--
|
|
-- MUST run before glossary.lua and exo.lua, which read the `term=` and
|
|
-- `title=` attributes this filter fills in. See _extension.yml.
|
|
|
|
local LANGS = { de = true, fr = true, en = true }
|
|
|
|
local active = nil -- e.g. "de"
|
|
local dict = {} -- key -> translated string, for `active` only
|
|
local missing = {} -- keys reported once each, to keep the log readable
|
|
|
|
------------------------------------------------------------------ helpers
|
|
|
|
local function base_lang(s)
|
|
if s == nil then return nil end
|
|
-- "de-CH" and "fr_CA" both reduce to the bare language subtag
|
|
return (tostring(s):lower():gsub("[_-].*$", ""))
|
|
end
|
|
|
|
-- @tr{key} -> translation, anywhere in a plain string
|
|
local function subst(s)
|
|
if s == nil or not s:find("@tr{", 1, true) then return s end
|
|
return (s:gsub("@tr%{([%w%._%-]+)%}", function(key)
|
|
local v = dict[key]
|
|
if v ~= nil then return v end
|
|
if not missing[key] then
|
|
missing[key] = true
|
|
io.stderr:write(
|
|
"i18n: no '" .. active .. "' translation for key '" .. key ..
|
|
"' -- add it to i18n.yml\n")
|
|
end
|
|
return "@tr{" .. key .. "}" -- leave the token visible rather than blank
|
|
end))
|
|
end
|
|
|
|
local function subst_attrs(attrs)
|
|
if attrs == nil then return attrs end
|
|
for k, v in pairs(attrs) do
|
|
if type(v) == "string" then attrs[k] = subst(v) end
|
|
end
|
|
return attrs
|
|
end
|
|
|
|
-- the language class on an element, if any
|
|
local function lang_class(el)
|
|
if el.classes == nil then return nil end
|
|
for _, c in ipairs(el.classes) do
|
|
if LANGS[c] then return c end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
------------------------------------------------------------------ metadata
|
|
|
|
local function read_meta(meta)
|
|
active = base_lang(meta.lang and pandoc.utils.stringify(meta.lang)) or "en"
|
|
|
|
if not LANGS[active] then
|
|
io.stderr:write(
|
|
"i18n: lang '" .. active .. "' is not one of de/fr/en; " ..
|
|
"language divs will all be dropped\n")
|
|
end
|
|
|
|
-- build the lookup table for the active language only
|
|
if meta.i18n ~= nil then
|
|
for key, per_lang in pairs(meta.i18n) do
|
|
if per_lang ~= nil and per_lang[active] ~= nil then
|
|
dict[key] = pandoc.utils.stringify(per_lang[active])
|
|
end
|
|
end
|
|
end
|
|
|
|
-- `title-de:` / `title-fr:` / ... collapse onto `title:`; same for any key
|
|
local drop = {}
|
|
for k, v in pairs(meta) do
|
|
local base, suffix = tostring(k):match("^(.-)%-(%a%a)$")
|
|
if base and base ~= "" and LANGS[suffix] then
|
|
if suffix == active then meta[base] = v end
|
|
drop[#drop + 1] = k
|
|
end
|
|
end
|
|
for _, k in ipairs(drop) do meta[k] = nil end
|
|
|
|
return meta
|
|
end
|
|
|
|
------------------------------------------------------------------ content
|
|
|
|
local function Div(el)
|
|
local l = lang_class(el)
|
|
if l then
|
|
if l == active then return el.content end
|
|
return {}
|
|
end
|
|
el.attributes = subst_attrs(el.attributes)
|
|
return el
|
|
end
|
|
|
|
local function Span(el)
|
|
local l = lang_class(el)
|
|
if l then
|
|
if l == active then return el.content end
|
|
return {}
|
|
end
|
|
el.attributes = subst_attrs(el.attributes)
|
|
return el
|
|
end
|
|
|
|
local function Header(el)
|
|
el.attributes = subst_attrs(el.attributes)
|
|
return el
|
|
end
|
|
|
|
local function Image(el)
|
|
-- the markdown reader percent-encodes the braces of a link destination,
|
|
-- so figures/x-@tr{lang} arrives as figures/x-@tr%7Blang%7D
|
|
local src = el.src:gsub("@tr%%7[Bb]([%w%._%-]+)%%7[Dd]", "@tr{%1}")
|
|
el.src = subst(src)
|
|
el.attributes = subst_attrs(el.attributes)
|
|
return el
|
|
end
|
|
|
|
-- an html comment is documentation, not content: `@tr{key}` written there as
|
|
-- an example must stay as typed
|
|
local function is_comment(s) return s:match("^%s*<!%-%-") ~= nil end
|
|
|
|
local function Math(el) el.text = subst(el.text); return el end
|
|
local function Str(el) el.text = subst(el.text); return el end
|
|
local function RawBlock(el)
|
|
if not is_comment(el.text) then el.text = subst(el.text) end
|
|
return el
|
|
end
|
|
local function RawInline(el)
|
|
if not is_comment(el.text) then el.text = subst(el.text) end
|
|
return el
|
|
end
|
|
|
|
local function Code(el) el.attributes = subst_attrs(el.attributes); return el end
|
|
local function CodeBlock(el)el.attributes = subst_attrs(el.attributes); return el end
|
|
|
|
return {
|
|
{ Meta = read_meta },
|
|
{
|
|
Div = Div, Span = Span, Header = Header, Image = Image,
|
|
Math = Math, Str = Str, RawBlock = RawBlock, RawInline = RawInline,
|
|
Code = Code, CodeBlock = CodeBlock,
|
|
},
|
|
}
|