silo:std.locale

Source src/silo:std.locale

1##! Locale, PluralCategory, and the ambient `CurrentLocale` aspect.
2##!
3##! Locale is a cross-cutting concern used by strings, formatting,
4##! conversions, temporal, markdown, and the localization system — not
5##! temporal-specific.
6
7:use
8  :open core AnyInt Bool Result Str Symbol Vec Ordering
9  :open traits Debug Display Eq Hash Ord TryFrom TryInto
10:end
11
12# si[impl prim.plural-category]
13## CLDR plural category for a given count. Variants follow Unicode CLDR
14## definitions: `zero`, `one`, `two`, `few`, `many`, `other`.
15:enum(pub) PluralCategory
16  | zero
17  | one
18  | two
19  | few
20  | many
21  | other
22:end
23
24# si[impl prim.locale+2]
25@lang(locale)
26:impl Locale
27  @host
28  .plural-category ( Locale AnyInt -> PluralCategory ) ;
29:end
30
31# si[impl aspect.current-locale+3]
32## Ambient current-locale aspect. Resolves the active `Locale` and the
33## locale fallback chain for lookups such as message formatting and
34## collation.
35:aspect(pub) CurrentLocale
36  ## Returns the currently-active locale.
37  @host
38  .locale ( -> Locale ) ;
39  ## Returns the ordered locale fallback chain used for lookup.
40  @host
41  .lookup-order ( -> (Vec Locale) ) ;
42:end
43
44:impl (CurrentLocale Locale)
45  .locale ( Locale -> Locale ) identity ;
46  @host
47  .lookup-order ( Locale -> (Vec Locale) ) ;
48:end
49
50:impl TryFrom Locale Symbol | Str
51  .try-from ( Symbol -> (Result Locale Str) ) locale-try-from-symbol ;
52:end
53
54:impl TryFrom Locale Str | Str
55  .try-from ( Str -> (Result Locale Str) ) locale-try-from-str ;
56:end
57
58:impl TryInto Symbol Locale | Str
59  .try-into ( Symbol -> (Result Locale Str) ) locale-try-from-symbol ;
60:end
61
62:impl TryInto Str Locale | Str
63  .try-into ( Str -> (Result Locale Str) ) locale-try-from-str ;
64:end
65
66:impl Display Locale .fmt ( Locale -> Str ) locale>str ; :end
67:impl Debug Locale .fmt-debug ( Locale -> Str ) locale>str ; :end
68:impl Hash Locale .hash ( Locale -> AnyInt ) hash-locale ; :end
69:impl Eq Locale Locale @host .= ( Locale Locale -> Bool ) ; :end
70:impl Ord Locale Locale
71  @host
72  .< ( Locale Locale -> Bool ) ;
73  @host
74  .> ( Locale Locale -> Bool ) ;
75:end