From a07d00e66d25147b45ceff5f9689ff98dd182390 Mon Sep 17 00:00:00 2001 From: Bauke Date: Tue, 16 Jan 2024 12:40:02 +0100 Subject: [PATCH] Add the localize utility function. --- utilities/localize.typ | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 utilities/localize.typ diff --git a/utilities/localize.typ b/utilities/localize.typ new file mode 100644 index 0000000..bee79b2 --- /dev/null +++ b/utilities/localize.typ @@ -0,0 +1,24 @@ +// Get the localized item of a given key inside a data dictionary, using the +// provided language. +#let localize(data, key, language) = { + // If the key is directly available in the data then it hasn't been localized. + if key in data { + return data.at(key) + } + + // Then check for the key with the wanted language appended to it. + let key_with_language = key + "_" + language + if key_with_language in data { + return data.at(key_with_language) + } + + // If a locale dictionary exists in the data, then run this function again + // using that dictionary for the same key and language. + if "locale" in data { + return localize(data.locale, key, language) + } + + // And if the key nor locale exists in any way then panic, as we won't have + // any text to return. + panic("Data does not contain key in any localized way: " + key, data) +}