
Localization.pudu
Pudu42 lines1.4 KB
1/** @Validation.Localization.Catalog — resolves application message text */2module PuduLangValidator.Localization34import Std.Map as Map5import PuduLangValidator.Rule as Rule67/** @Validation.Localization.Catalog.Value — maps locale and code to text */8export type Catalog = { entries: Map[(Str, Str), Str] }91011export fn empty() -> Catalog { Catalog{entries: mapOf([])} }121314export fn withMessage(catalog: Catalog, locale: Str, code: Str, message: Str) -> Catalog {15 Catalog{entries: catalog.entries.insert((locale.toLower(), code), message)}16}171819export fn messageFor(catalog: &Catalog, locale: Str, code: Str, fallback: Str) -> Str {20 let normalized = locale.toLower()21 match Map.get(&catalog.entries, (normalized, code)) {22 case Some(found) => found23 case None => {24 let language = normalized.split("-")[0]25 match Map.get(&catalog.entries, (language, code)) {26 case Some(found) => found27 case None => fallback28 }29 }30 }31}323334export fn localized[T, V](35 builder: Rule.RuleBuilder[T, V], catalog: Catalog, locale: fn(T) -> Str,36 code: Str, fallback: Str37) -> Rule.RuleBuilder[T, V] {38 Rule.withMessageFrom(builder, fn(root: T, _value: V) -> Str {39 messageFor(&catalog, locale(root), code, fallback)40 })41}42