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) +}