From d9737568a40269776cf5a76c9d272df577d3a614 Mon Sep 17 00:00:00 2001 From: Pavel Eliseev Date: Tue, 8 Apr 2025 23:00:28 +0300 Subject: [PATCH 1/5] Add document locale support --- src/locales.typ | 202 +++++++++++++++++++++++++++++++++++++++++ src/show-module.typ | 5 +- src/styles/default.typ | 16 +++- src/styles/minimal.typ | 9 +- src/utilities.typ | 16 ++++ 5 files changed, 238 insertions(+), 10 deletions(-) create mode 100644 src/locales.typ diff --git a/src/locales.typ b/src/locales.typ new file mode 100644 index 0000000..412ae96 --- /dev/null +++ b/src/locales.typ @@ -0,0 +1,202 @@ +#let local-names = ( + "sq": ( // Albanian + parameters: [Parametrat], + default: [Standard], + variables: [Variablat] + ), + "ar": ( // Arabic + parameters: [المعلمات], + default: [الافتراضي], + variables: [المتغيرات] + ), + "eu": ( // Basque + parameters: [Parametroak], + default: [Lehenetsia], + variables: [Aldagaiak] + ), + "nb": ( // Bokmål + parameters: [Parametere], + default: [Standard], + variables: [Variabler] + ), + "bg": ( // Bulgarian + parameters: [Параметри], + default: [По подразбиране], + variables: [Променливи] + ), + "ca": ( // Catalan + parameters: [Paràmetres], + default: [Per defecte], + variables: [Variables] + ), + "zh": ( // Chinese (Simplified) + parameters: [参数], + default: [默认值], + variables: [变量] + ), + "hr": ( // Croatian + parameters: [Parametri], + default: [Zadano], + variables: [Varijable] + ), + "cs": ( // Czech + parameters: [Parametry], + default: [Výchozí], + variables: [Proměnné] + ), + "da": ( // Danish + parameters: [Parametre], + default: [Standard], + variables: [Variabler] + ), + "nl": ( // Dutch + parameters: [Parameters], + default: [Standaard], + variables: [Variabelen] + ), + "en": ( // English + parameters: [Parameters], + default: [Default], + variables: [Variables] + ), + "et": ( // Estonian + parameters: [Parameetrid], + default: [Vaikimisi], + variables: [Muutujad] + ), + "tl": ( // Filipino + parameters: [Mga Parameter], + default: [Karaniwan], + variables: [Mga Variable] + ), + "fi": ( // Finnish + parameters: [Parametrit], + default: [Oletus], + variables: [Muuttujat] + ), + "fr": ( // French + parameters: [Paramètres], + default: [Par défaut], + variables: [Variables] + ), + "gl": ( // Galician + parameters: [Parámetros], + default: [Por defecto], + variables: [Variables] + ), + "de": ( // German + parameters: [Parameter], + default: [Standard], + variables: [Variablen] + ), + "el": ( // Greek + parameters: [Παράμετροι], + default: [Προεπιλογή], + variables: [Μεταβλητές] + ), + "he": ( // Hebrew + parameters: [פרמטרים], + default: [ברירת מחדל], + variables: [משתנים] + ), + "hu": ( // Hungarian + parameters: [Paraméterek], + default: [Alapértelmezett], + variables: [Változók] + ), + "is": ( // Icelandic + parameters: [Færibreytur], + default: [Sjálfgefið], + variables: [Breytur] + ), + "id": ( // Indonesian + parameters: [Parameter], + default: [Baku], + variables: [Variabel] + ), + "it": ( // Italian + parameters: [Parametri], + default: [Predefinito], + variables: [Variabili] + ), + "ja": ( // Japanese + parameters: [パラメーター], + default: [デフォルト], + variables: [変数] + ), + "la": ( // Latin + parameters: [Parametri], + default: [Definitum], + variables: [Variabilia] + ), + "dsb": ( // Lower Sorbian + parameters: [Parametry], + default: [Standard], + variables: [Wariable] + ), + "nn": ( // Nynorsk + parameters: [Parametrar], + default: [Standard], + variables: [Variablar] + ), + "pl": ( // Polish + parameters: [Parametry], + default: [Domyślne], + variables: [Zmienne] + ), + "pt": ( // Portuguese + parameters: [Parâmetros], + default: [Padrão], + variables: [Variáveis] + ), + "ro": ( // Romanian + parameters: [Parametri], + default: [Implicit], + variables: [Variabile] + ), + "ru": ( // Russian + parameters: [Параметры], + default: [По умолчанию], + variables: [Переменные] + ), + "sr": ( // Serbian (Cyrillic) + parameters: [Параметри], + default: [Подразумевано], + variables: [Променљиве] + ), + "sk": ( // Slovak + parameters: [Parametre], + default: [Predvolené], + variables: [Premenné] + ), + "sl": ( // Slovenian + parameters: [Parametri], + default: [Privzeto], + variables: [Spremenljivke] + ), + "es": ( // Spanish + parameters: [Parámetros], + default: [Por defecto], + variables: [Variables] + ), + "sv": ( // Swedish + parameters: [Parametrar], + default: [Standard], + variables: [Variabler] + ), + "tr": ( // Turkish + parameters: [Parametreler], + default: [Varsayılan], + variables: [Değişkenler] + ), + "uk": ( // Ukrainian + parameters: [Параметри], + default: [За замовчуванням], + variables: [Змінні] + ), + "vi": ( // Vietnamese + parameters: [Tham số], + default: [Mặc định], + variables: [Biến số] + ), +) \ No newline at end of file diff --git a/src/show-module.typ b/src/show-module.typ index 177752a..73b9333 100644 --- a/src/show-module.typ +++ b/src/show-module.typ @@ -92,8 +92,9 @@ module-doc.variables = module-doc.variables.filter(filter) } - let style-functions = utilities.get-style-functions(style) + + let local-names-overrided = local-names != (parameters: [Parameters], default: [Default], variables: [Variables]) let style-args = ( style: style-functions, @@ -105,9 +106,9 @@ colors: colors, enable-cross-references: enable-cross-references, local-names: local-names, + local-names-overrided: local-names-overrided ) - let eval-scope = ( // Predefined functions that may be called by the user in doc-comment code example: style-functions.show-example.with( diff --git a/src/styles/default.typ b/src/styles/default.typ index 34f165a..10fa0b2 100644 --- a/src/styles/default.typ +++ b/src/styles/default.typ @@ -75,8 +75,8 @@ list(..module-doc.functions.map(fn => gen-entry(fn.name + "()"))) } - if module-doc.variables.len() > 0 { - text(style-args.local-names.at("variables"), weight: "bold") + context if module-doc.variables.len() > 0 { + text(get-local-name("variables", style-args: style-args, language: text.lang), weight: "bold") list(..module-doc.variables.map(var => gen-entry(var.name))) } } @@ -139,7 +139,10 @@ #types.map(x => (style-args.style.show-type)(x, style-args: style-args)).join([ #text("or",size:.6em) ]) #content - #if show-default [ #parbreak() #style-args.local-names.default: #raw(lang: "typc", default) ] + #context if show-default [ + #parbreak() + #get-local-name("default", style-args: style-args, language: text.lang): #raw(lang: "typc", default) + ] ] ) @@ -159,8 +162,11 @@ eval-docstring(fn.description, style-args) - block(breakable: style-args.break-param-descriptions, { - heading(style-args.local-names.parameters, level: style-args.first-heading-level + 2) + block(breakable: style-args.break-param-descriptions, context { + heading( + get-local-name("parameters", style-args: style-args, language: text.lang), + level: style-args.first-heading-level + 2 + ) (style-args.style.show-parameter-list)(fn, style-args: style-args) }) diff --git a/src/styles/minimal.typ b/src/styles/minimal.typ index 9022a77..9e43dc5 100644 --- a/src/styles/minimal.typ +++ b/src/styles/minimal.typ @@ -21,7 +21,10 @@ } if module-doc.variables.len() > 0 { - text(style-args.local-names.at("variables"), weight: "bold") + text( + get-local-name("variables", style-args: style-args, language: text.lang), + weight: "bold" + ) list(..module-doc.variables.map(var => gen-entry(var.name))) } } @@ -122,8 +125,8 @@ ) } - if parameter-block != none { - [*#style-args.local-names.parameters:*] + context if parameter-block != none { + [*#get-local-name("parameters", style-args: style-args, language: text.lang):*] parameter-block } v(4em, weak: true) diff --git a/src/utilities.typ b/src/utilities.typ index 807996f..1189b48 100644 --- a/src/utilities.typ +++ b/src/utilities.typ @@ -1,3 +1,4 @@ +#import "locales.typ" // Matches doc-comment references of the form `@@otherfunc` or `@@otherfunc()`. #let reference-matcher = regex(`@@([\w\d\-_\)\(]+)`.text) @@ -69,4 +70,19 @@ ) } return style-functions +} + +/// Get the local name for string with the given language. +#let get-local-name( + /// String to get the local name for -> str + target, + /// Style-args provided from styles -> dict + style-args: (:), + /// Target language known by context -> str + language: "en" +) = context { + if style-args.local-names-overrided { + return style-args.local-names.at(target) + } + return locales.local-names.at(text.lang).at(target) } \ No newline at end of file From cf598148cc27d59125386165ff2ce94641523970 Mon Sep 17 00:00:00 2001 From: Pavel Eliseev Date: Wed, 9 Apr 2025 20:15:06 +0300 Subject: [PATCH 2/5] Add language existence check --- src/utilities.typ | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/utilities.typ b/src/utilities.typ index 1189b48..fe08493 100644 --- a/src/utilities.typ +++ b/src/utilities.typ @@ -84,5 +84,8 @@ if style-args.local-names-overrided { return style-args.local-names.at(target) } + if language not in locales.local-names.keys() { + panic("Unknown language '" + language + "', you can use custom translations with #show-module(local-names: ...)") + } return locales.local-names.at(text.lang).at(target) } \ No newline at end of file From 2495869bf5a4e3c987ded55695908cc47bc1589a Mon Sep 17 00:00:00 2001 From: Mc-Zen <52877387+Mc-Zen@users.noreply.github.com> Date: Thu, 17 Apr 2025 23:21:05 +0200 Subject: [PATCH 3/5] [add] allow partial translation customization --- src/show-module.typ | 8 +++++--- src/utilities.typ | 10 ++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/show-module.typ b/src/show-module.typ index 73b9333..45f2463 100644 --- a/src/show-module.typ +++ b/src/show-module.typ @@ -77,7 +77,8 @@ /// are `parameters` and `default`. You can for example use: /// `local-names: (parameters: [Parameter], default: [Standard], variables: [Variablen])`. /// -> dictionary - local-names: (parameters: [Parameters], default: [Default], variables: [Variables]) + local-names: auto + ) = block({ let label-prefix = module-doc.label-prefix if sort-functions == auto { @@ -94,7 +95,9 @@ let style-functions = utilities.get-style-functions(style) - let local-names-overrided = local-names != (parameters: [Parameters], default: [Default], variables: [Variables]) + if local-names == auto { + local-names = (:) + } let style-args = ( style: style-functions, @@ -106,7 +109,6 @@ colors: colors, enable-cross-references: enable-cross-references, local-names: local-names, - local-names-overrided: local-names-overrided ) let eval-scope = ( diff --git a/src/utilities.typ b/src/utilities.typ index fe08493..fc81e96 100644 --- a/src/utilities.typ +++ b/src/utilities.typ @@ -72,7 +72,8 @@ return style-functions } -/// Get the local name for string with the given language. + +/// Get the local name for a string with the given language. #let get-local-name( /// String to get the local name for -> str target, @@ -81,11 +82,12 @@ /// Target language known by context -> str language: "en" ) = context { - if style-args.local-names-overrided { + + if target in style-args.local-names { return style-args.local-names.at(target) } if language not in locales.local-names.keys() { - panic("Unknown language '" + language + "', you can use custom translations with #show-module(local-names: ...)") + panic("Unknown language '" + language + "', you can use custom translations with `#show-module(local-names: ...)`") } return locales.local-names.at(text.lang).at(target) -} \ No newline at end of file +} From 0f230c668f1c0ca04d255a7889a1762b5f144315 Mon Sep 17 00:00:00 2001 From: Mc-Zen <52877387+Mc-Zen@users.noreply.github.com> Date: Thu, 17 Apr 2025 23:25:00 +0200 Subject: [PATCH 4/5] [add] type assertation --- src/show-module.typ | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/show-module.typ b/src/show-module.typ index 45f2463..c18d912 100644 --- a/src/show-module.typ +++ b/src/show-module.typ @@ -97,6 +97,11 @@ if local-names == auto { local-names = (:) + } else { + assert( + type(local-names) == dictionary, + message: "The parameter `local-names` expects a dictionary of translations. " + ) } let style-args = ( From 7eda48ecb11a4b18e6503e08099cea044415a8b6 Mon Sep 17 00:00:00 2001 From: Mc-Zen <52877387+Mc-Zen@users.noreply.github.com> Date: Thu, 17 Apr 2025 23:26:59 +0200 Subject: [PATCH 5/5] [refactor] move `text.lang` query into helper function This will also make it easier to adopt custom types later. --- src/styles/default.typ | 12 ++++++------ src/styles/minimal.typ | 4 ++-- src/utilities.typ | 8 +++++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/styles/default.typ b/src/styles/default.typ index 10fa0b2..48f1cb4 100644 --- a/src/styles/default.typ +++ b/src/styles/default.typ @@ -75,8 +75,8 @@ list(..module-doc.functions.map(fn => gen-entry(fn.name + "()"))) } - context if module-doc.variables.len() > 0 { - text(get-local-name("variables", style-args: style-args, language: text.lang), weight: "bold") + if module-doc.variables.len() > 0 { + text(get-local-name("variables", style-args: style-args), weight: "bold") list(..module-doc.variables.map(var => gen-entry(var.name))) } } @@ -139,9 +139,9 @@ #types.map(x => (style-args.style.show-type)(x, style-args: style-args)).join([ #text("or",size:.6em) ]) #content - #context if show-default [ + #if show-default [ #parbreak() - #get-local-name("default", style-args: style-args, language: text.lang): #raw(lang: "typc", default) + #get-local-name("default", style-args: style-args): #raw(lang: "typc", default) ] ] ) @@ -162,9 +162,9 @@ eval-docstring(fn.description, style-args) - block(breakable: style-args.break-param-descriptions, context { + block(breakable: style-args.break-param-descriptions, { heading( - get-local-name("parameters", style-args: style-args, language: text.lang), + get-local-name("parameters", style-args: style-args), level: style-args.first-heading-level + 2 ) (style-args.style.show-parameter-list)(fn, style-args: style-args) diff --git a/src/styles/minimal.typ b/src/styles/minimal.typ index 9e43dc5..71de51f 100644 --- a/src/styles/minimal.typ +++ b/src/styles/minimal.typ @@ -125,8 +125,8 @@ ) } - context if parameter-block != none { - [*#get-local-name("parameters", style-args: style-args, language: text.lang):*] + if parameter-block != none { + [*#get-local-name("parameters", style-args: style-args):*] parameter-block } v(4em, weak: true) diff --git a/src/utilities.typ b/src/utilities.typ index fc81e96..9b48909 100644 --- a/src/utilities.typ +++ b/src/utilities.typ @@ -75,17 +75,19 @@ /// Get the local name for a string with the given language. #let get-local-name( + /// String to get the local name for -> str target, + /// Style-args provided from styles -> dict - style-args: (:), - /// Target language known by context -> str - language: "en" + style-args: (:) + ) = context { if target in style-args.local-names { return style-args.local-names.at(target) } + let language = text.lang if language not in locales.local-names.keys() { panic("Unknown language '" + language + "', you can use custom translations with `#show-module(local-names: ...)`") }