forked from greggirwin/red-formatting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locale.red
125 lines (108 loc) · 3.97 KB
/
locale.red
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
Red [
Title: "Locale data"
Description: "WIP locale structure, extracted from CLDR data"
Author: @hiiamboris
License: {
Distributed under the Boost Software License, Version 1.0.
See https://github.com/red/red/blob/master/BSL-License.txt
}
]
;@@ right now it loads data from different scripts
;@@ later this data should be unified
system/locale: make system/locale [list: #[]]
#include %system.red
#include %../common/assert.red
; #assert off
#include %../common/composite.red
#include %../common/error-macro.red
context [unless map? select system/locale 'list [
system/locale: make make reactor! system/locale [ ;-- reactor here so one can subscribe to locale changes
;; data for chosen locale:
name: none ;-- "language (region)" spelled in locale's language
lang-name: none ;-- language name only (e.g. "English")
region-name: none ;-- region name only (e.g. "United States")
region: none ;-- abbreviated word name of the region (e.g. 'US)
currency: none ;-- default currency for locale
numbers: #[] ;-- digits, symbols, numeric masks
calendar: #[] ;-- standalone, format, date masks
;; collective data:
list: #[] ;-- locale data for all supported (loaded) locales
numbering-systems: #[] ;-- all numbering systems from CLDR (they're small)
cardinal: [] ;-- cardinal quantities spelling rules
ordinal: [] ;-- ordinal quantities spelling rules
]
#include %locales.red
#include %numbering-systems.red
#include %plural.red
get-user-locale-id: function [/local lang regn] [ ;-- returns 'en_US or something
lower: charset [#"a" - #"z"]
upper: charset [#"A" - #"Z"]
non-alpha: negate union lower upper
sep: [#"_" | #"-"]
=language=: [2 lower ahead [non-alpha | end]]
=region=: [2 upper ahead [non-alpha | end]]
parse s: get-user-locale-id* [
["C" | "POSIX"] opt ["." to end] end (return 'red) ;-- portable POSIX locale, 'red' is our portable locale
| copy lang =language= opt [to =region= copy regn =region=] (
if regn [repend lang ["_" regn]]
return to word! lang
)
| (return 'red) ;-- unindentified, default to 'red'
]
]
get-best-locale-id: function [] [ ;-- returns best locale from those supported
loc: get-user-locale-id
case [
system/locale/list/:loc [loc]
all [
formed: form loc
clear find loc "_"
lang: to word! formed
system/locale/list/:lang
] [lang]
'fallback ['red]
]
]
inherit: function [src dst] [ ;-- links data between maps without override
foreach [key srcval] src [
case [
not find dst key [dst/:key: srcval] ;-- carry over as reference when possible
all [map? dst/:key map? srcval] [
inherit srcval dst/:key
]
]
]
]
;; useful when we want to use a locale without loading it as default
set 'expand-locale function [
"Expand given locale from minimized form into a working state"
name [word!]
][
loc: system/locale/list/:name
unless loc/parent [exit] ;-- already expanded
expand-locale par: system/locale/list/(loc/parent)
inherit par loc
remove/key loc 'parent ;-- mark as expanded
]
set 'load-locale function [
"Load given locale as default into system/locale"
name [word!]
][
expand-locale name
sl: system/locale
unless data: sl/list/:name [ERROR "Data for locale '(name)' is not loaded"]
set sl data
sl/locale: name
set bind [language region] sl split form name #"_"
sl/name: copy data/lang-name
if sl/region [repend sl/name ["(" data/region-name ")"]]
sl/currencies/names: data/currency-names
;; for R2 compatibility:
sl/months: data/calendar/standalone/months/full
m: data/calendar/standalone/days/full
sl/days: reduce [m/mon m/tue m/wed m/thu m/fri m/sat m/sun] ;-- in R2 it started from monday always
() ;-- no return value
]
load-locale get-best-locale-id
]]
; ? system/locale