This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Locale.ceylon
202 lines (169 loc) · 6.37 KB
/
Locale.ceylon
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/********************************************************************************
* Copyright (c) 2011-2017 Red Hat Inc. and/or its affiliates and others
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* http://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
import ceylon.collection {
HashMap
}
import ceylon.language.meta.declaration {
Module,
Package
}
"Aggregates localized information associated with a certain
locale, including:
- the local [[language]],
- the local [[currency]],
- localized date, time, currency, and numeric [[formats]],
- local representations of other [[languages]] and
[[currencies]].
The locale also provides access to associated localized
message bundles via [[messages]]."
shared sealed class Locale(language, formats,
languages, currencies, currencyCode=null,
lowercaseMappings = emptyMap,
uppercaseMappings = emptyMap) {
"The language of this locale."
shared Language language;
"Localized date, time, currency, and numeric formats
for this locale."
shared Formats formats;
"Localized representations of other languages."
shared Map<String,Language> languages;
"Localized representations of other currencies."
shared Map<String,Currency> currencies;
"Localized mappings of uppercase characters to lowercase."
Map<Character,String> lowercaseMappings;
"Localized mappings of lowercase characters to uppercase."
Map<Character,String> uppercaseMappings;
String? currencyCode;
"The currency of this locale."
shared Currency? currency
=> if (exists currencyCode)
then currencies[currencyCode]
else null;
"A string with the characters of the given string
converted to uppercase according to the rules of this
locale."
shared String uppercase(String string)
=> uppercaseMappings.fold(string, (str, mapping)
=> str.replace(mapping.key.string, mapping.item))
.uppercased;
"A string with the characters of the given string
converted to lowercase according to the rules of this
locale."
shared String lowercase(String string)
=> lowercaseMappings.fold(string, (str, mapping)
=> str.replace(mapping.key.string, mapping.item))
.lowercased;
string => language.string;
function search(Module mod, String name) {
function find(String tag)
=> mod.resourceByPath(
name + tag + ".properties");
value lang = language.languageCode;
value country = language.countryCode;
value variant = language.variant;
if (exists country) {
if (exists variant) {
value tag =
"_" + lang +
"_" + country +
"_" + variant;
if (exists result = find(tag)) {
return result;
}
}
value tag =
"_" + lang +
"_" + country;
if (exists result = find(tag)) {
return result;
}
}
value tag = "_" + lang;
if (exists result = find(tag)) {
return result;
}
return find("");
}
function path(Package pack, String name)
=> "/" + pack.qualifiedName.replace(".", "/")
+ "/" + name;
"Given a [[Module]] or [[Package]] and the name of a
resource bundle belonging to that package or module,
return a map of string keys to string values for this
locale.
For example, suppose the system locale is `en-AU`, and
this code occurs in the module `hello.world`:
value messages = systemLocale.messages(`module`, \"Errors\");
Then the returned map `messages` will contain
entries from a properties file in the resources of the
module `hello.world`. The following files will be
searched, in order:
1. `/hello/world/Errors_en_AU.properties`
2. `/hello/world/Errors_en.properties`
3. `/hello/world/Errors.properties`
If no properties file is found, the map with be empty."
shared Map<String,String> messages(
"The module to which the resource bundle belongs."
Module|Package component,
"The name of the resource bundle, or `\"Messages\"`
by default."
String name = "Messages") {
value resource =
switch (component)
case (Module)
search(component, name)
case (Package)
component.container
.resourceByPath(path(component, name));
value map = HashMap<String, String>();
if (exists resource) {
parsePropertiesFile {
textContent = resource.textContent();
handleEntry = map.put;
};
}
return map;
}
}
"Returns a [[Locale]] containing information about the
locale with the given locale [[tag]]."
shared Locale? locale(String tag) {
value filePath = tag + ".txt";
if (exists resource =
localeModule.resourceByPath(filePath)) {
value lines =
resource.textContent()
.lines.iterator();
let ([language,currency]
= parseLanguage(lines, tag));
return Locale {
language = language;
currencyCode = currency;
formats = parseFormats(lines);
languages = parseLanguages(lines);
currencies = parseCurrencies(lines);
lowercaseMappings = parseCaseMappings(lines);
uppercaseMappings = parseCaseMappings(lines);
};
}
else {
return null;
}
}
"Returns a [[Locale]] containing information about the
locale of the current system."
see (`value system.locale`)
shared Locale systemLocale {
"locale data for current locale ``system.locale`` must exist"
assert (exists systemLocaleCache);
return systemLocaleCache;
}
Locale? systemLocaleCache = locale(system.locale);
Module localeModule = `module`;