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 5 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
85 changes: 57 additions & 28 deletions algorithmic.typ
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@
* (change_indent: int, body: ((ast | content)[] | content | ast)
*/

#import "locale.typ": locale

#let localize(kw, lang: none) = context {
let lang = if lang != none {
lang
} else {
text.lang
}

if type(kw) == str {
let kws = locale.at(lang, default: (:))
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 {
ast.map(d => ast_to_content_list(indent, d))
Expand Down Expand Up @@ -53,34 +78,38 @@
..table_bits
)
}
#let algorithm-figure(title, supplement: "Algorithm", inset: 0.2em, ..bits) = {
#let algorithm-figure(title, supplement: none, inset: 0.2em, lang: none, ..bits) = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supplement should be "Algorithm" by default, not none.

Copy link
Author

@Caellian Caellian Jun 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

none means none is specified explicitly, so it'll use default localization. This way you can still specify something like "Derp" without the rest of the code trying to localize that.

In docs you'd state the default is "Algorithm" as you did previously.

EDIT: Right, with keeping "en" the default, doing this doesn't make the API nicer. I'll make a commit addressing this review, as it's isolated to these changes it should be straightforward enough to simply revert it for 2.0.0.

return figure(
supplement: supplement,
supplement: if supplement != none {
supplement
} else {
localize("Algorithm", lang: lang)
},
kind: "algorithm",
caption: title,
placement: none,
algorithm(inset: inset, ..bits),
)
}
#let iflike_block(kw1: "", kw2: "", kw3: "", cond, ..body) = (
(strong(kw1) + " " + cond + " " + strong(kw2)),
#let iflike_block(kw1: "", kw2: "", kw3: "", lang: none, cond, ..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_block_with_kw3(kw1: "", kw2: "", kw3: "", cond, ..body) = (
(strong(kw1) + " " + cond + " " + strong(kw2)),
#let iflike_block_with_kw3(kw1: "", kw2: "", kw3: "", lang: none, cond, ..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_block_without_kw3(kw1: "", kw2: "", cond, ..body) = (
(strong(kw1) + " " + cond + " " + strong(kw2)),
#let iflike_block_without_kw3(kw1: "", kw2: "", lang: none, cond, ..body) = (
(strong(localize(kw1, lang: lang)) + " " + cond + " " + strong(localize(kw2, lang: lang))),
(change_indent: 2, body: body.pos()),
)
#let iflike_block(kw1: "", kw2: "", kw3: none, cond, ..body) = (
#let iflike_block(kw1: "", kw2: "", kw3: none, lang: none, cond, ..body) = (
if kw3 == "" or kw3 == none {
iflike_block_without_kw3(kw1: kw1, kw2: kw2, cond, ..body)
iflike_block_without_kw3(kw1: kw1, kw2: kw2, lang: lang, cond, ..body)
} else {
iflike_block_with_kw3(kw1: kw1, kw2: kw2, kw3: kw3, cond, ..body)
iflike_block_with_kw3(kw1: kw1, kw2: kw2, kw3: kw3, lang: lang, cond, ..body)
}
)
#let arraify(v) = {
Expand All @@ -90,11 +119,11 @@
(v,)
}
}
#let call(name, kw: "function", inline: false, style: smallcaps, args, ..body) = (
#let call(name, kw: "function", inline: false, style: smallcaps, lang: none, args, ..body) = (
if inline {
[#style(name)\(#arraify(args).join(", ")\)]
[#style(localize(name, lang: lang))\(#arraify(args).join(", ")\)]
} else {
iflike_block(kw1: kw, kw3: "end", (style(name) + $(#arraify(args).join(", "))$), ..body)
iflike_block(kw1: kw, kw3: "end", (style(localize(name, lang: lang)) + $(#arraify(args).join(", "))$), lang: lang, ..body)
}
)

Expand All @@ -107,13 +136,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 @@ -123,7 +152,7 @@
#let For = iflike_block.with(kw1: "for", kw2: "do", kw3: "end")
#let Else = iflike_block.with(kw1: "else", kw2: "", kw3: "end", "")
#let ElseIf = iflike_block.with(kw1: "else if", kw2: "then", kw3: "end")
#let IfElseChain(..conditions_and_bodies) = {
#let IfElseChain(lang: none, ..conditions_and_bodies) = {
let result = ()
let conditions_and_bodies = conditions_and_bodies.pos()
let len = conditions_and_bodies.len()
Expand All @@ -132,20 +161,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 @@ -157,6 +186,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: none) = (strong(localize("return", lang: lang)) + " " + arg,)
#let Terminate(lang: none) = (smallcaps(localize("terminate", lang: lang)),)
#let Break(lang: none) = (smallcaps(localize("break", lang: lang)),)
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",
)
)