Repository navigation
Expand file tree
/
Copy pathi18n.cpp
More file actions
302 lines (285 loc) · 11.3 KB
/
Copy pathi18n.cpp
File metadata and controls
302 lines (285 loc) · 11.3 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <teaql/runtime/i18n.hpp>
#include <teaql/runtime/detail/builtin_i18n_data.hpp>
#include <teaql/core/detail/number_text.hpp>
#include <charconv>
namespace teaql {
namespace {
std::optional<std::string_view>
builtin_entry(std::string_view locale, bool vocabulary, std::string_view key) {
for (const auto& entry : detail::builtin_i18n_entries) {
if (entry.locale == locale && entry.vocabulary == vocabulary && entry.key == key) {
return entry.text;
}
}
return {};
}
const Json* field(const Json::Object& object, std::string_view key) {
auto found = object.find(key);
return found == object.end() ? nullptr : &found->second;
}
const String* text(const Json* value) {
return value ? std::get_if<String>(&value->data) : nullptr;
}
const Json::Object* object(const Json* value) {
return value ? std::get_if<Json::Object>(&value->data) : nullptr;
}
std::optional<String> template_error(Memory r, std::string_view value) {
for (std::size_t i = 0; i < value.size(); ++i) {
if (value[i] == '}') {
return String("unmatched template closing brace", r);
}
if (value[i] != '{') {
continue;
}
auto first = ++i;
while (i < value.size() && value[i] != '}') {
if (value[i] == '{') {
return String("nested template opening brace", r);
}
++i;
}
if (i == value.size()) {
return String("unclosed template placeholder", r);
}
auto name = value.substr(first, i - first);
if (name != "location" && name != "system" && name != "input" && name != "input_len") {
String out("unknown template placeholder: ", r);
out += name;
return out;
}
}
return {};
}
template <class T>
String integer_text(Memory r, T value) {
char buffer[32];
auto result = std::to_chars(buffer, buffer + sizeof(buffer), value);
return String(buffer, result.ptr, r);
}
String format_value(Memory r, const Value& value) {
return std::visit(
[&](const auto& data) -> String {
using T = std::decay_t<decltype(data)>;
if constexpr (std::same_as<T, std::monostate> || std::same_as<T, TypedNull>) {
return String("null", r);
} else if constexpr (std::same_as<T, bool>) {
return String(data ? "true" : "false", r);
} else if constexpr (std::integral<T>) {
return integer_text(r, data);
} else if constexpr (std::same_as<T, double>) {
String out(r);
detail::append_float(out, data, false);
return out;
} else if constexpr (std::same_as<T, String>) {
return String(data, r);
} else if constexpr (std::same_as<T, Json>) {
return data.dump(r);
} else if constexpr (std::same_as<T, Timestamp>) {
return integer_text(r, data.as_millis());
} else if constexpr (std::same_as<T, Date> || std::same_as<T, Decimal>) {
return data.to_string(r);
} else if constexpr (std::same_as<T, Value::Object>) {
return String("<object>", r);
} else {
return String("<list>", r);
}
},
value.data);
}
// Rust String::replace scans each original pass, then the next argument sees
// the previous pass's replacements. It does not recursively replace one key.
String replace(Memory r, std::string_view source, std::string_view key, std::string_view value) {
String result(r);
std::size_t position = 0;
for (;;) {
auto found = source.find(key, position);
if (found == source.npos) {
result += source.substr(position);
return result;
}
result += source.substr(position, found - position);
result += value;
position = found + key.size();
}
}
}
I18nCatalog::I18nCatalog(std::allocator_arg_t, Allocator a)
: default_locale_(a.resource()), locales_(a.resource()) {}
I18nCatalog::I18nCatalog(std::allocator_arg_t, Allocator a, const I18nCatalog& other)
: default_locale_(other.default_locale_, a.resource()), locales_(other.locales_, a.resource()),
builtin_(other.builtin_), fallback_(other.fallback_) {}
I18nCatalog I18nCatalog::builtin(Memory r) {
I18nCatalog out(std::allocator_arg, Allocator(r));
out.default_locale_ = "en";
out.builtin_ = true;
return out;
}
Result<I18nCatalog, RuntimeError> I18nCatalog::from_json(Memory r, std::string_view source) {
using Output = Result<I18nCatalog, RuntimeError>;
auto fail = [&](std::string_view message) {
return Output::err(r, RuntimeError::language(r, message));
};
auto parsed = Json::parse_result(r, source);
if (!parsed) {
String error("invalid i18n catalog: ", r);
error += parsed.error().to_string(r);
return fail(error);
}
const auto* root = object(&parsed.value());
if (!root) {
return fail("i18n catalog root must be an object");
}
if (root->size() != 3 || !root->contains("schema") || !root->contains("defaultLocale") ||
!root->contains("locales")) {
return fail("i18n catalog root must contain only schema, defaultLocale and locales");
}
auto* schema = text(field(*root, "schema"));
if (!schema || *schema != "teaql.i18n/v1") {
return fail("unsupported i18n catalog schema");
}
auto* default_locale = text(field(*root, "defaultLocale"));
if (!default_locale) {
return fail("missing i18n defaultLocale");
}
auto default_language = language_from_code(*default_locale);
if (!default_language) {
String error("unsupported i18n defaultLocale: ", r);
error += *default_locale;
return fail(error);
}
auto* locales = object(field(*root, "locales"));
if (!locales) {
return fail("missing i18n locales");
}
I18nCatalog out(std::allocator_arg, Allocator(r));
out.default_locale_ = language_code(*default_language);
for (const auto& [code, value] : *locales) {
auto language = language_from_code(code);
if (!language) {
String error("unsupported i18n locale: ", r);
error += code;
return fail(error);
}
if (code != language_code(*language)) {
String error("i18n locale keys must be canonical: ", r);
error += code;
return fail(error);
}
String prefix("i18n locale ", r);
prefix += code;
auto* entry = object(&value);
if (!entry) {
prefix += " must be an object";
return fail(prefix);
}
if (entry->size() != 2 || !entry->contains("messages") || !entry->contains("vocabulary")) {
prefix += " must contain only messages and vocabulary";
return fail(prefix);
}
auto& target = out.locales_.try_emplace(code).first->second;
for (bool vocabulary : {false, true}) {
std::string_view name = vocabulary ? "vocabulary" : "messages";
auto* entries = object(field(*entry, name));
String path(code, r);
path += '.';
path += name;
if (!entries) {
String error("i18n ", r);
error += path;
error += " must be an object";
return fail(error);
}
auto& destination = vocabulary ? target.vocabulary : target.messages;
for (const auto& [key, translation] : *entries) {
String key_path(path, r);
key_path += '.';
key_path += key;
auto* content = text(&translation);
if (!content || content->empty()) {
String error("empty i18n translation: ", r);
error += key_path;
return fail(error);
}
if (!vocabulary) {
if (auto error = template_error(r, *content)) {
key_path += ": ";
key_path += *error;
return fail(key_path);
}
}
destination.emplace(key, *content);
}
}
}
if (!out.locales_.contains(out.default_locale_)) {
return fail("i18n catalog does not contain its default locale");
}
out.fallback_ = true;
return Output::ok(r, std::move(out));
}
std::optional<std::string_view>
I18nCatalog::lookup_exact(std::string_view locale, bool vocabulary, std::string_view key) const {
if (builtin_) {
return builtin_entry(locale, vocabulary, key);
}
auto catalog = locales_.find(locale);
if (catalog == locales_.end()) {
return {};
}
const auto& entries = vocabulary ? catalog->second.vocabulary : catalog->second.messages;
auto entry = entries.find(key);
return entry == entries.end() ? std::optional<std::string_view>{} : entry->second;
}
String
I18nCatalog::lookup(Memory r, Language language, bool vocabulary, std::string_view key) const {
auto locale = language_code(language);
auto result = lookup_exact(locale, vocabulary, key);
if (!result && fallback_) {
result = builtin_entry(locale, vocabulary, key);
}
if (!result) {
result = lookup_exact(default_locale_, vocabulary, key);
}
if (!result && fallback_) {
result = builtin_entry("en", vocabulary, key);
}
return String(result.value_or(key), r);
}
String I18nCatalog::message(Memory r, Language language, std::string_view key) const {
return lookup(r, language, false, key);
}
String I18nCatalog::vocabulary(Memory r, Language language, std::string_view key) const {
return lookup(r, language, true, key);
}
String
I18nCatalog::translate_check_result(Memory r, Language language, const CheckResult& result) const {
constexpr std::array<std::string_view, 8> keys{"checker.required",
"checker.invalidType",
"checker.min",
"checker.max",
"checker.minLength",
"checker.maxLength",
"checker.contextRootMissing",
"checker.contextRootMismatch"};
auto output = message(r, language, keys.at(std::size_t(result.rule)));
auto location = translate_location(r, language, result.location);
auto system = result.system_value ? format_value(r, *result.system_value) : String("-", r);
auto input = result.input_value ? format_value(r, *result.input_value) : String("-", r);
std::size_t length = 0;
if (result.input_value) {
if (auto* value = std::get_if<String>(&result.input_value->data)) {
for (unsigned char byte : *value) {
if ((byte & 0xc0) != 0x80) {
++length;
}
}
}
}
auto input_len = integer_text(r, length);
output = replace(r, output, "{location}", location);
output = replace(r, output, "{system}", system);
output = replace(r, output, "{input}", input);
output = replace(r, output, "{input_len}", input_len);
return output;
}
}