Skip to content

Add localization support #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 44 additions & 27 deletions algorithmic.typ
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,20 @@
* (change-indent: int, body: ((ast | content)[] | content | ast)
*/

#let ast-to-content-list(indent, ast) = {
#import "locale.typ": locale

#let localize(kw, lang: "en") = {
if type(kw) == str {
let kws = locale.at(lang)
kws.at(kw, default: kw)
} else if type(kw) == dictionary {
kw.at(lang, default: kw.at("en", default: kw.values().at(0, default: none)))
} else {
kw
}
}

#let ast_to_content_list(indent, ast) = {
if type(ast) == array {
// array of (ast | content)
ast.map(d => ast-to-content-list(indent, d))
Expand Down Expand Up @@ -73,38 +86,42 @@
..table-bits
)
}

#let algorithm-figure(
title,
supplement: "Algorithm",
supplement: none,
inset: 0.2em,
indent: 0.5em,
vstroke: 0pt + luma(200),
lang: "en",
..bits,
) = {
return figure(
supplement: supplement,
supplement: if supplement == none {
localize("Algorithm", lang: lang)
} else {
supplement
},
kind: "algorithm",
caption: title,
placement: none,
algorithm(indent: indent, inset: inset, vstroke: vstroke, ..bits),
)
}

#let iflike-unterminated(kw1: "", kw2: "", cond, ..body) = (
(strong(kw1) + " " + cond + " " + strong(kw2)),
#let iflike-unterminated(kw1: "", kw2: "", cond, lang: "en", ..body) = (
(strong(localize(kw1, lang: lang)) + " " + cond + " " + strong(localize(kw2, lang: lang))),
(change-indent: 2, body: body.pos()),
)
#let iflike-terminated(kw1: "", kw2: "", kw3: "", cond, ..body) = (
(strong(kw1) + " " + cond + " " + strong(kw2)),
#let iflike-terminated(kw1: "", kw2: "", kw3: "", cond, lang: "en", ..body) = (
(strong(localize(kw1, lang: lang)) + " " + cond + " " + strong(localize(kw2, lang: lang))),
(change-indent: 2, body: body.pos()),
strong(kw3),
strong(localize(kw3, lang: lang)),
)
#let iflike(kw1: "", kw2: "", kw3: none, cond, ..body) = (
#let iflike(kw1: "", kw2: "", kw3: none, cond, lang: "en", ..body) = (
if kw3 == "" or kw3 == none {
iflike-unterminated(kw1: kw1, kw2: kw2, cond, ..body)
iflike-unterminated(kw1: kw1, kw2: kw2, cond, lang: lang, ..body)
} else {
iflike-terminated(kw1: kw1, kw2: kw2, kw3: kw3, cond, ..body)
iflike-terminated(kw1: kw1, kw2: kw2, kw3: kw3, cond, lang: lang, ..body)
}
)
#let arraify(v) = {
Expand All @@ -114,11 +131,11 @@
(v,)
}
}
#let call(name, kw: "function", inline: false, style: smallcaps, args, ..body) = (
#let call(name, kw: "function", inline: false, style: smallcaps, lang: "en", args, ..body) = (
if inline {
[#style(name)\(#arraify(args).join(", ")\)]
[#style(localize(name, lang: lang))\(#arraify(args).join(", ")\)]
} else {
iflike(kw1: kw, kw3: "end", (style(name) + $(#arraify(args).join(", "))$), ..body)
iflike(kw1: kw, kw3: "end", (style(name) + $(#arraify(args).join(", "))$), lang: lang, ..body)
}
)

Expand All @@ -131,13 +148,13 @@
#let LineBreak = State[]

/// Inline call
#let CallInline(name, args) = call(inline: true, name, args)
#let FnInline(f, args) = call(inline: true, style: strong, f, args)
#let CallInline(name, args, lang: none) = call(inline: true, name, args, lang: lang)
#let FnInline(f, args, lang: none) = call(inline: true, style: strong, f, args, lang: lang)
#let CommentInline(c) = sym.triangle.stroked.r + " " + c

// Block calls
#let Call(..args) = (CallInline(..args),)
#let Fn(..args) = (FnInline(..args),)
#let Call(lang: none, ..args) = (CallInline(..args, lang: lang),)
#let Fn(lang: none, ..args) = (FnInline(..args, lang: lang),)
#let Comment(c) = (CommentInline(c),)
#let LineComment(l, c) = ([#l.first()#h(1fr)#CommentInline(c)],)

Expand All @@ -147,7 +164,7 @@
#let For = iflike.with(kw1: "for", kw2: "do", kw3: "end")
#let Else = iflike.with(kw1: "else", kw2: "", kw3: "end", "")
#let ElseIf = iflike.with(kw1: "else if", kw2: "then", kw3: "end")
#let IfElseChain(..conditions-and-bodies) = {
#let IfElseChain(lang: "en", ..conditions-and-bodies) = {
let result = ()
let conditions-and-bodies = conditions-and-bodies.pos()
let len = conditions-and-bodies.len()
Expand All @@ -156,20 +173,20 @@
while i < len {
if i == len - 1 and calc.odd(len) {
// Last element is the "else" block
result.push(Else(..arraify(conditions-and-bodies.at(i))))
result.push(Else(..arraify(conditions-and-bodies.at(i)), lang: lang))
} else if calc.even(i) {
// Condition
let cond = conditions-and-bodies.at(i)
let body = arraify(conditions-and-bodies.at(i + 1))
if i == 0 {
// First condition is a regular "if"
result.push(If(cond, ..body, kw3: ""))
result.push(If(cond, ..body, kw3: "", lang: lang))
} else if i + 2 == len {
// Last condition before "else" is an "elseif" with "end"
result.push(ElseIf(cond, ..body, kw3: "end"))
result.push(ElseIf(cond, ..body, kw3: "end", lang: lang))
} else {
// Intermediate conditions are "elseif" without "end"
result.push(ElseIf(cond, ..body, kw3: ""))
result.push(ElseIf(cond, ..body, kw3: "", lang: lang))
}
} else {
// Skip body since it's already processed
Expand All @@ -181,6 +198,6 @@

// Instructions
#let Assign(var, val) = (var + " " + $<-$ + " " + val,)
#let Return(arg) = (strong("return") + " " + arg,)
#let Terminate = (smallcaps("terminate"),)
#let Break = (smallcaps("break"),)
#let Return(arg, lang: "en") = (strong(localize("return", lang: lang)) + " " + arg,)
#let Terminate(lang: "en") = (smallcaps(localize("terminate", lang: lang)),)
#let Break(lang: "en") = (smallcaps(localize("break", lang: lang)),)
Binary file modified docs/algorithmic-demo.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/algorithmic-demo.typ
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
While(
$l <= r$,
{
Assign([mid], FnInline[floor][$(l + r) / 2$])
Assign([mid], FnInline((en: "floor", hr: "pod"))[$(l + r) / 2$])
IfElseChain(
$A ["mid"] < v$,
{
Expand Down
114 changes: 114 additions & 0 deletions locale.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#let locale = (
en: (
"Algorithm": "Algorithm",
"function": "function",
"procedure": "procedure",
"if": "if",
"then": "then",
"else": "else",
"else if": "else if",
"for": "for",
"do": "do",
"while": "while",
"end": "end",
"return": "return",
"terminate": "terminate",
"break": "break",
),
de: (
"Algorithm": "Algorithmus",
"function": "Funktion",
"procedure": "Prozedur",
"if": "wenn",
"then": "dann",
"else": "sonst",
"else if": "else sonst wenn",
"for": "für",
"do": "ausführen",
"while": "während",
"end": "ende",
"return": "zurückgeben",
"terminate": "abbrechen",
// "break": "break", usually not translated
),
hr: (
"Algorithm": "Algoritam",
"function": "funkcija",
"procedure": "procedura",
"if": "ako",
"then": "onda",
"else": "inače",
"else if": "inače ako",
"for": "za",
"do": "provedi",
"while": "dok",
"end": "kraj",
"return": "vrati",
"terminate": "terminiraj",
"break": "prekid",
),
sr: (
"Algorithm": "Algoritam",
"function": "funkcija",
"procedure": "procedura",
"if": "ako",
"then": "onda",
"else": "inače",
"else if": "inače ako",
"for": "za",
"do": "provedi",
"while": "dok",
"end": "kraj",
"return": "vrati",
"terminate": "terminiraj",
"break": "prekid",
),
cnr: (
"Algorithm": "Algoritam",
"function": "funkcija",
"procedure": "procedura",
"if": "ako",
"then": "onda",
"else": "inače",
"else if": "inače ako",
"for": "za",
"do": "provedi",
"while": "dok",
"end": "kraj",
"return": "vrati",
"terminate": "terminiraj",
"break": "prekid",
),
bs: (
"Algorithm": "Algoritam",
"function": "funkcija",
"procedure": "procedura",
"if": "ako",
"then": "onda",
"else": "inače",
"else if": "inače ako",
"for": "za",
"do": "uradi",
"while": "dok",
"end": "kraj",
"return": "vrati",
"terminate": "terminiraj",
"break": "prekid",
),
sl: (
"Algorithm": "Algoritem",
"function": "funkcija",
"procedure": "procedura",
"if": "če",
"then": "potem",
"else": "drugače",
"else if": "drugače če",
"for": "za",
"do": "izvedi",
"while": "dokler",
"end": "konec",
"return": "vrni",
"terminate": "prekini",
"break": "izstopi",
)
)