@discord/intl-message-database 0.0.1-rc.0 → 0.0.1-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +18 -10
- package/.cargo/config.toml +0 -12
- package/Cargo.toml +0 -38
- package/bench/keyless-json.js +0 -91
- package/bench/native.js +0 -114
- package/bench/util.js +0 -55
- package/build.rs +0 -5
- package/npm/darwin-arm64/README.md +0 -3
- package/npm/darwin-arm64/package.json +0 -18
- package/npm/darwin-x64/README.md +0 -3
- package/npm/darwin-x64/package.json +0 -18
- package/npm/linux-arm64-gnu/README.md +0 -3
- package/npm/linux-arm64-gnu/package.json +0 -21
- package/npm/linux-arm64-musl/README.md +0 -3
- package/npm/linux-arm64-musl/package.json +0 -21
- package/npm/linux-x64-gnu/README.md +0 -3
- package/npm/linux-x64-gnu/package.json +0 -21
- package/npm/linux-x64-musl/README.md +0 -3
- package/npm/linux-x64-musl/package.json +0 -21
- package/npm/win32-arm64-msvc/README.md +0 -3
- package/npm/win32-arm64-msvc/package.json +0 -18
- package/npm/win32-ia32-msvc/README.md +0 -3
- package/npm/win32-ia32-msvc/package.json +0 -18
- package/npm/win32-x64-msvc/README.md +0 -3
- package/npm/win32-x64-msvc/package.json +0 -18
- package/src/lib.rs +0 -14
- package/src/messages/database.rs +0 -343
- package/src/messages/error.rs +0 -43
- package/src/messages/message_definition.rs +0 -169
- package/src/messages/message_variables_visitor.rs +0 -299
- package/src/messages/meta.rs +0 -38
- package/src/messages/mod.rs +0 -27
- package/src/messages/source_file.rs +0 -75
- package/src/messages/symbols.rs +0 -164
- package/src/messages/translation.rs +0 -64
- package/src/messages/value.rs +0 -51
- package/src/napi.rs +0 -300
- package/src/services/mod.rs +0 -9
- package/src/services/precompile.rs +0 -91
- package/src/services/types.rs +0 -237
- package/src/services/validator.rs +0 -104
- package/src/sources/definitions_parser.rs +0 -347
- package/src/sources/mod.rs +0 -87
- package/src/sources/translations.rs +0 -75
- package/src/test.rs +0 -67
- package/src/threading.rs +0 -61
package/src/messages/value.rs
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
use crate::messages::FilePosition;
|
|
2
|
-
use intl_markdown::{parse_intl_message, Document};
|
|
3
|
-
use serde::Serialize;
|
|
4
|
-
|
|
5
|
-
use super::message_variables_visitor::{MessageVariables, MessageVariablesVisitor};
|
|
6
|
-
|
|
7
|
-
#[derive(Debug, Serialize)]
|
|
8
|
-
pub struct MessageValue {
|
|
9
|
-
pub raw: String,
|
|
10
|
-
pub parsed: Document,
|
|
11
|
-
pub variables: Option<MessageVariables>,
|
|
12
|
-
pub file_position: Option<FilePosition>,
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
impl MessageValue {
|
|
16
|
-
/// Creates a new value including the original raw content as given and
|
|
17
|
-
/// parsing the content to a compiled AST.
|
|
18
|
-
pub fn from_raw(content: &str) -> Self {
|
|
19
|
-
// By convention, messages starting immediately with a newline character are considered
|
|
20
|
-
// blocks and have paragraphs parsed in their content, while normal messages with no leading
|
|
21
|
-
// newline are treated as fully-inline.
|
|
22
|
-
let include_blocks = content.starts_with("\n");
|
|
23
|
-
let document = parse_intl_message(&content, include_blocks);
|
|
24
|
-
|
|
25
|
-
let mut variables = MessageVariables::new();
|
|
26
|
-
let variables = match MessageVariablesVisitor::visit(&document, &mut variables) {
|
|
27
|
-
Ok(_) => Some(variables),
|
|
28
|
-
_ => None,
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
Self {
|
|
32
|
-
raw: content.into(),
|
|
33
|
-
parsed: document,
|
|
34
|
-
variables,
|
|
35
|
-
file_position: None,
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
pub fn with_file_position(mut self, position: FilePosition) -> Self {
|
|
40
|
-
self.file_position = Some(position);
|
|
41
|
-
self
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Messages are equal if they have the same starting raw content. Everything
|
|
46
|
-
// else about a message is derived from that original string.
|
|
47
|
-
impl PartialEq for MessageValue {
|
|
48
|
-
fn eq(&self, other: &Self) -> bool {
|
|
49
|
-
self.raw == other.raw
|
|
50
|
-
}
|
|
51
|
-
}
|
package/src/napi.rs
DELETED
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
//! Native addon bindings for using intl-message-extractor in Node, using
|
|
2
|
-
//! napi-rs and neon. This lets the library use the full power of native
|
|
3
|
-
//! compilation when running locally on a machine rather than in a browser,
|
|
4
|
-
//! including parallelism for processing multiple files at once.
|
|
5
|
-
//!
|
|
6
|
-
//! This is the preferred way of using the library wherever possible.
|
|
7
|
-
use std::collections::HashMap;
|
|
8
|
-
|
|
9
|
-
use napi::{JsNumber, JsUnknown};
|
|
10
|
-
use napi::bindgen_prelude::*;
|
|
11
|
-
use napi_derive::napi;
|
|
12
|
-
|
|
13
|
-
use crate::messages::{
|
|
14
|
-
global_get_symbol, global_intern_string, KeySymbol, MessagesDatabase, MessagesError,
|
|
15
|
-
read_global_symbol_store,
|
|
16
|
-
};
|
|
17
|
-
use crate::services::IntlService;
|
|
18
|
-
use crate::services::precompile::{CompiledMessageFormat, IntlMessagePreCompiler};
|
|
19
|
-
use crate::services::types::IntlTypesGenerator;
|
|
20
|
-
use crate::services::validator;
|
|
21
|
-
use crate::sources::extract_message_translations;
|
|
22
|
-
use crate::threading::run_in_thread_pool;
|
|
23
|
-
|
|
24
|
-
#[napi]
|
|
25
|
-
pub struct IntlMessagesDatabase {
|
|
26
|
-
database: MessagesDatabase,
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
#[napi(object)]
|
|
30
|
-
pub struct IntlDiagnostic {
|
|
31
|
-
pub key: String,
|
|
32
|
-
pub diagnostics: JsUnknown,
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// This is an unused struct purely for generating functional TS types.
|
|
36
|
-
#[napi(object)]
|
|
37
|
-
pub struct IntlSourceFile {
|
|
38
|
-
#[napi(js_name = "type")]
|
|
39
|
-
pub ty: String,
|
|
40
|
-
pub file: String,
|
|
41
|
-
#[napi(js_name = "messageKeys")]
|
|
42
|
-
pub message_keys: Vec<JsNumber>,
|
|
43
|
-
pub meta: IntlMessageMeta,
|
|
44
|
-
pub locale: Option<JsNumber>,
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
#[napi(object)]
|
|
48
|
-
pub struct IntlMessageMeta {
|
|
49
|
-
pub secret: bool,
|
|
50
|
-
pub translate: bool,
|
|
51
|
-
pub bundle_secrets: bool,
|
|
52
|
-
pub translations_path: String,
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
#[napi]
|
|
56
|
-
pub enum IntlCompiledMessageFormat {
|
|
57
|
-
Json,
|
|
58
|
-
KeylessJson,
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
impl From<IntlCompiledMessageFormat> for CompiledMessageFormat {
|
|
62
|
-
fn from(value: IntlCompiledMessageFormat) -> Self {
|
|
63
|
-
match value {
|
|
64
|
-
IntlCompiledMessageFormat::Json => CompiledMessageFormat::Json,
|
|
65
|
-
IntlCompiledMessageFormat::KeylessJson => CompiledMessageFormat::KeylessJson,
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
#[napi]
|
|
71
|
-
impl IntlMessagesDatabase {
|
|
72
|
-
#[napi(constructor)]
|
|
73
|
-
pub fn new() -> Self {
|
|
74
|
-
IntlMessagesDatabase {
|
|
75
|
-
database: MessagesDatabase::new(),
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
#[napi]
|
|
80
|
-
pub fn process_definitions_file(&mut self, file_path: String) -> anyhow::Result<u32> {
|
|
81
|
-
let content = std::fs::read_to_string(&file_path)?;
|
|
82
|
-
|
|
83
|
-
let source_file =
|
|
84
|
-
crate::sources::process_definitions_file(&mut self.database, &file_path, &content)?;
|
|
85
|
-
Ok(source_file.value() as u32)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
#[napi]
|
|
89
|
-
pub fn process_definitions_file_content(
|
|
90
|
-
&mut self,
|
|
91
|
-
file_path: String,
|
|
92
|
-
content: String,
|
|
93
|
-
) -> anyhow::Result<u32> {
|
|
94
|
-
let source_file =
|
|
95
|
-
crate::sources::process_definitions_file(&mut self.database, &file_path, &content)?;
|
|
96
|
-
Ok(source_file.value() as u32)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
#[napi]
|
|
100
|
-
pub fn process_all_translation_files(
|
|
101
|
-
&mut self,
|
|
102
|
-
locale_map: HashMap<String, String>,
|
|
103
|
-
) -> anyhow::Result<()> {
|
|
104
|
-
run_in_thread_pool(
|
|
105
|
-
locale_map.into_iter(),
|
|
106
|
-
|(locale, file_path)| {
|
|
107
|
-
let content = std::fs::read_to_string(&file_path)
|
|
108
|
-
.expect(&format!("Failed to read translation file at {}", file_path));
|
|
109
|
-
Ok((locale, file_path, extract_message_translations(&content)))
|
|
110
|
-
},
|
|
111
|
-
|(locale, file_path, translations)| {
|
|
112
|
-
let translations = translations?;
|
|
113
|
-
self.database
|
|
114
|
-
.insert_translations_from_file(&file_path, &locale, translations)?;
|
|
115
|
-
Ok(())
|
|
116
|
-
},
|
|
117
|
-
)?;
|
|
118
|
-
Ok(())
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
#[napi]
|
|
122
|
-
pub fn process_translation_file_content(
|
|
123
|
-
&mut self,
|
|
124
|
-
file_path: String,
|
|
125
|
-
locale: String,
|
|
126
|
-
content: String,
|
|
127
|
-
) -> anyhow::Result<u32> {
|
|
128
|
-
let translations = extract_message_translations(&content)?;
|
|
129
|
-
self.database
|
|
130
|
-
.insert_translations_from_file(&file_path, &locale, translations)?;
|
|
131
|
-
let file_key = global_intern_string(&file_path)?;
|
|
132
|
-
Ok(file_key.value() as u32)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
#[napi]
|
|
136
|
-
pub fn get_known_locales(&self) -> anyhow::Result<Vec<String>> {
|
|
137
|
-
let locales = &self.database.known_locales;
|
|
138
|
-
|
|
139
|
-
let mut result = vec![];
|
|
140
|
-
|
|
141
|
-
for locale in locales.iter() {
|
|
142
|
-
// Safety: We _shouldn't_ be able to have a locale symbol without that
|
|
143
|
-
// locale being interned already. If we do, something must be very
|
|
144
|
-
// corrupt, so we can just panic.
|
|
145
|
-
let store = read_global_symbol_store()?;
|
|
146
|
-
|
|
147
|
-
let locale = store
|
|
148
|
-
.resolve(*locale)
|
|
149
|
-
.ok_or_else(|| MessagesError::SymbolNotFound(*locale))?;
|
|
150
|
-
|
|
151
|
-
result.push(locale.into());
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
Ok(result)
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
#[napi(ts_return_type = "IntlSourceFile")]
|
|
158
|
-
pub fn get_source_file(&self, env: Env, file_path: String) -> anyhow::Result<JsUnknown> {
|
|
159
|
-
let file_symbol = global_get_symbol(&file_path)?;
|
|
160
|
-
let Some(source) = self.database.sources.get(&file_symbol) else {
|
|
161
|
-
return Err(MessagesError::SymbolNotFound(file_symbol).into());
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
Ok(env.to_js_value(source)?)
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
#[napi]
|
|
168
|
-
pub fn get_all_source_file_paths(&self) -> anyhow::Result<Vec<String>> {
|
|
169
|
-
let sources = self.database.sources.keys();
|
|
170
|
-
let mut paths = Vec::with_capacity(sources.len());
|
|
171
|
-
for key in sources {
|
|
172
|
-
if let Some(path) = resolve_symbol(key.value() as u32)? {
|
|
173
|
-
paths.push(path)
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
Ok(paths)
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
#[napi(ts_return_type = "string[]")]
|
|
181
|
-
pub fn get_source_file_hashed_keys(
|
|
182
|
-
&self,
|
|
183
|
-
env: Env,
|
|
184
|
-
file_path: String,
|
|
185
|
-
) -> anyhow::Result<JsUnknown> {
|
|
186
|
-
let file_symbol = global_get_symbol(&file_path)?;
|
|
187
|
-
let Some(source) = self.database.sources.get(&file_symbol) else {
|
|
188
|
-
return Err(MessagesError::SymbolNotFound(file_symbol).into());
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
let mut hashes = Vec::with_capacity(source.message_keys().len());
|
|
192
|
-
|
|
193
|
-
for key in source.message_keys() {
|
|
194
|
-
if let Some(message) = self.database.messages.get(key) {
|
|
195
|
-
hashes.push(message.hashed_key());
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
Ok(env.to_js_value(&hashes)?)
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
#[napi]
|
|
203
|
-
pub fn get_message(&self, env: Env, key: String) -> anyhow::Result<JsUnknown> {
|
|
204
|
-
let definition = &self
|
|
205
|
-
.database
|
|
206
|
-
.get_message(&key)
|
|
207
|
-
.ok_or_else(|| MessagesError::ValueNotInterned(key))?;
|
|
208
|
-
|
|
209
|
-
Ok(env.to_js_value(definition)?)
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
#[napi]
|
|
213
|
-
pub fn generate_types(
|
|
214
|
-
&self,
|
|
215
|
-
source_file_path: String,
|
|
216
|
-
output_file_path: String,
|
|
217
|
-
) -> anyhow::Result<()> {
|
|
218
|
-
let mut output_file = std::fs::File::create(output_file_path)?;
|
|
219
|
-
let source_file_key = global_get_symbol(&source_file_path)?;
|
|
220
|
-
IntlTypesGenerator::new(&self.database, source_file_key, &mut output_file).run()
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
#[napi]
|
|
224
|
-
pub fn precompile(
|
|
225
|
-
&self,
|
|
226
|
-
locale: String,
|
|
227
|
-
output_path: String,
|
|
228
|
-
format: Option<IntlCompiledMessageFormat>,
|
|
229
|
-
) -> anyhow::Result<()> {
|
|
230
|
-
let buffer = self.precompile_to_buffer(locale, format)?;
|
|
231
|
-
std::fs::write(output_path, buffer)?;
|
|
232
|
-
Ok(())
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
#[napi]
|
|
236
|
-
pub fn precompile_to_buffer(
|
|
237
|
-
&self,
|
|
238
|
-
locale: String,
|
|
239
|
-
format: Option<IntlCompiledMessageFormat>,
|
|
240
|
-
) -> anyhow::Result<Buffer> {
|
|
241
|
-
let locale_key = global_get_symbol(&locale)?;
|
|
242
|
-
let mut result: Vec<u8> = Vec::with_capacity(self.database.messages.len() * 40);
|
|
243
|
-
IntlMessagePreCompiler::new(
|
|
244
|
-
&self.database,
|
|
245
|
-
&mut result,
|
|
246
|
-
locale_key,
|
|
247
|
-
format.unwrap_or(IntlCompiledMessageFormat::Json).into(),
|
|
248
|
-
)
|
|
249
|
-
.run()?;
|
|
250
|
-
Ok(result.into())
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
#[napi]
|
|
254
|
-
pub fn validate_messages(&self, env: Env) -> anyhow::Result<Vec<IntlDiagnostic>> {
|
|
255
|
-
let key_store = read_global_symbol_store()?;
|
|
256
|
-
|
|
257
|
-
let mut results = vec![];
|
|
258
|
-
for (key, message) in self.database.messages.iter() {
|
|
259
|
-
let diagnostics = validator::validate_message(&message);
|
|
260
|
-
if diagnostics.is_empty() {
|
|
261
|
-
continue;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
let key = key_store
|
|
265
|
-
.resolve(*key)
|
|
266
|
-
.ok_or_else(|| MessagesError::SymbolNotFound(*key))?;
|
|
267
|
-
|
|
268
|
-
results.push(IntlDiagnostic {
|
|
269
|
-
key: key.into(),
|
|
270
|
-
diagnostics: env.to_js_value(&diagnostics)?,
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
Ok(results)
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
#[napi]
|
|
279
|
-
pub fn hash_message_key(key: String) -> String {
|
|
280
|
-
intl_message_utils::hash_message_key(&key)
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
#[napi]
|
|
284
|
-
pub fn is_message_definitions_file(key: String) -> bool {
|
|
285
|
-
intl_message_utils::is_message_definitions_file(&key)
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
#[napi]
|
|
289
|
-
pub fn is_message_translations_file(key: String) -> bool {
|
|
290
|
-
intl_message_utils::is_message_translations_file(&key)
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
#[napi]
|
|
294
|
-
pub fn resolve_symbol(symbol: u32) -> anyhow::Result<Option<String>> {
|
|
295
|
-
let store = read_global_symbol_store()?;
|
|
296
|
-
|
|
297
|
-
Ok(KeySymbol::from_usize(symbol as usize)
|
|
298
|
-
.and_then(|key| store.resolve(key))
|
|
299
|
-
.map(String::from))
|
|
300
|
-
}
|
package/src/services/mod.rs
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
use intl_markdown::compile_to_format_js;
|
|
2
|
-
|
|
3
|
-
use crate::{
|
|
4
|
-
messages::{KeySymbol, MessagesDatabase},
|
|
5
|
-
services::IntlService,
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
/// A struct for managing the pre-compilation of messages into a JSON format that the FormatJS
|
|
9
|
-
/// runtime understands. The output of this service is a complete JSON object representing all
|
|
10
|
-
/// known messages for the given locale, and is written as a stream for the caller to store it
|
|
11
|
-
/// wherever needed (an output file, in memory during bundling, etc.).
|
|
12
|
-
pub struct IntlMessagePreCompiler<'a, W: std::io::Write> {
|
|
13
|
-
database: &'a MessagesDatabase,
|
|
14
|
-
output: &'a mut W,
|
|
15
|
-
locale_key: KeySymbol,
|
|
16
|
-
format: CompiledMessageFormat,
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
pub enum CompiledMessageFormat {
|
|
20
|
-
Json,
|
|
21
|
-
KeylessJson,
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
impl<'a, W: std::io::Write> IntlMessagePreCompiler<'a, W> {
|
|
25
|
-
pub fn new(
|
|
26
|
-
database: &'a MessagesDatabase,
|
|
27
|
-
output: &'a mut W,
|
|
28
|
-
locale_key: KeySymbol,
|
|
29
|
-
format: CompiledMessageFormat,
|
|
30
|
-
) -> Self {
|
|
31
|
-
Self {
|
|
32
|
-
database,
|
|
33
|
-
output,
|
|
34
|
-
locale_key,
|
|
35
|
-
format,
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
impl<W: std::io::Write> IntlService for IntlMessagePreCompiler<'_, W> {
|
|
40
|
-
type Result = anyhow::Result<()>;
|
|
41
|
-
|
|
42
|
-
fn run(&mut self) -> Self::Result {
|
|
43
|
-
match self.format {
|
|
44
|
-
CompiledMessageFormat::Json => self.run_json(),
|
|
45
|
-
CompiledMessageFormat::KeylessJson => self.run_keyless_json(),
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
impl<W: std::io::Write> IntlMessagePreCompiler<'_, W> {
|
|
51
|
-
fn run_json(&mut self) -> anyhow::Result<()> {
|
|
52
|
-
let messages = self.database.messages.values();
|
|
53
|
-
write!(self.output, "{{")?;
|
|
54
|
-
let mut is_first = true;
|
|
55
|
-
for message in messages {
|
|
56
|
-
if let Some(translation) = message.translations().get(&self.locale_key) {
|
|
57
|
-
if !is_first {
|
|
58
|
-
write!(self.output, ",")?;
|
|
59
|
-
} else {
|
|
60
|
-
is_first = false;
|
|
61
|
-
}
|
|
62
|
-
write!(self.output, "\"{}\":", message.hashed_key())?;
|
|
63
|
-
serde_json::to_writer(&mut self.output, &translation.parsed)?;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
write!(self.output, "}}")?;
|
|
67
|
-
Ok(())
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
fn run_keyless_json(&mut self) -> anyhow::Result<()> {
|
|
71
|
-
let messages = self.database.messages.values();
|
|
72
|
-
write!(self.output, "{{")?;
|
|
73
|
-
let mut is_first = true;
|
|
74
|
-
for message in messages {
|
|
75
|
-
if let Some(translation) = message.translations().get(&self.locale_key) {
|
|
76
|
-
if !is_first {
|
|
77
|
-
write!(self.output, ",")?;
|
|
78
|
-
} else {
|
|
79
|
-
is_first = false;
|
|
80
|
-
}
|
|
81
|
-
write!(self.output, "\"{}\":", message.hashed_key())?;
|
|
82
|
-
keyless_json::to_writer(
|
|
83
|
-
&mut self.output,
|
|
84
|
-
&compile_to_format_js(&translation.parsed),
|
|
85
|
-
)?;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
write!(self.output, "}}")?;
|
|
89
|
-
Ok(())
|
|
90
|
-
}
|
|
91
|
-
}
|
package/src/services/types.rs
DELETED
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
use std::fmt::Write;
|
|
2
|
-
|
|
3
|
-
use rustc_hash::FxHashSet;
|
|
4
|
-
use thiserror::Error;
|
|
5
|
-
|
|
6
|
-
use intl_message_utils::RUNTIME_PACKAGE_NAME;
|
|
7
|
-
|
|
8
|
-
use crate::{
|
|
9
|
-
messages::{
|
|
10
|
-
KeySymbol, KeySymbolMap, Message, MessagesDatabase, MessagesError,
|
|
11
|
-
MessageVariableType, read_global_symbol_store,
|
|
12
|
-
},
|
|
13
|
-
services::IntlService,
|
|
14
|
-
};
|
|
15
|
-
use crate::messages::MessageValue;
|
|
16
|
-
|
|
17
|
-
pub struct IntlTypesGenerator<'a, W: std::io::Write> {
|
|
18
|
-
database: &'a MessagesDatabase,
|
|
19
|
-
source_file_key: KeySymbol,
|
|
20
|
-
output: &'a mut W,
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
impl<'a, W: std::io::Write> IntlTypesGenerator<'a, W> {
|
|
24
|
-
pub fn new(
|
|
25
|
-
database: &'a MessagesDatabase,
|
|
26
|
-
source_file_key: KeySymbol,
|
|
27
|
-
output: &'a mut W,
|
|
28
|
-
) -> Self {
|
|
29
|
-
Self {
|
|
30
|
-
database,
|
|
31
|
-
source_file_key,
|
|
32
|
-
output,
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
fn make_doc_comment(
|
|
37
|
-
&self,
|
|
38
|
-
message: &Message,
|
|
39
|
-
known_locales: &FxHashSet<KeySymbol>,
|
|
40
|
-
) -> anyhow::Result<String> {
|
|
41
|
-
if message.is_defined() {
|
|
42
|
-
self.make_normal_message_doc_comment(message, known_locales)
|
|
43
|
-
} else {
|
|
44
|
-
self.make_undefined_message_doc_comment(message, known_locales)
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
fn make_normal_message_doc_comment(
|
|
49
|
-
&self,
|
|
50
|
-
message: &Message,
|
|
51
|
-
known_locales: &FxHashSet<KeySymbol>,
|
|
52
|
-
) -> anyhow::Result<String> {
|
|
53
|
-
let key = message.hashed_key();
|
|
54
|
-
// SAFETY: The caller asserts that this message is defined, so it must have a source.
|
|
55
|
-
let default_translation = message.get_source_translation().unwrap();
|
|
56
|
-
let translation_links =
|
|
57
|
-
self.build_translation_links(message.translations(), Some(default_translation))?;
|
|
58
|
-
|
|
59
|
-
let found_locales: FxHashSet<KeySymbol> =
|
|
60
|
-
message.translations().keys().map(Clone::clone).collect();
|
|
61
|
-
|
|
62
|
-
let missing_locales = {
|
|
63
|
-
let diff = known_locales.difference(&found_locales);
|
|
64
|
-
let symbol_store = read_global_symbol_store()?;
|
|
65
|
-
let mut missing_names = vec![];
|
|
66
|
-
for key in diff {
|
|
67
|
-
missing_names.push(
|
|
68
|
-
symbol_store
|
|
69
|
-
.resolve(*key)
|
|
70
|
-
.map(|locale| format!("`{locale}`"))
|
|
71
|
-
.ok_or(MessagesError::SymbolNotFound(*key))?,
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
missing_names
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
let mut result = String::new();
|
|
78
|
-
write!(result, " /**\n * Key: `{key}`.")?;
|
|
79
|
-
|
|
80
|
-
if translation_links.is_empty() {
|
|
81
|
-
write!(result, " **Untranslated**\n")?;
|
|
82
|
-
} else if missing_locales.is_empty() {
|
|
83
|
-
write!(result, " Translated in all languages\n")?;
|
|
84
|
-
} else {
|
|
85
|
-
write!(
|
|
86
|
-
result,
|
|
87
|
-
" Missing {} translations: {}\n",
|
|
88
|
-
missing_locales.len(),
|
|
89
|
-
missing_locales.join(", ")
|
|
90
|
-
)?;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
write!(
|
|
94
|
-
result,
|
|
95
|
-
" * ```\n * {}\n * ```\n */",
|
|
96
|
-
default_translation.raw,
|
|
97
|
-
)?;
|
|
98
|
-
|
|
99
|
-
Ok(result)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
fn build_translation_links(
|
|
103
|
-
&self,
|
|
104
|
-
translations: &KeySymbolMap<MessageValue>,
|
|
105
|
-
default_translation: Option<&MessageValue>,
|
|
106
|
-
) -> anyhow::Result<Vec<String>> {
|
|
107
|
-
let symbol_store = read_global_symbol_store()?;
|
|
108
|
-
|
|
109
|
-
let mut links = Vec::with_capacity(translations.len() - 1);
|
|
110
|
-
|
|
111
|
-
for (key, translation) in translations {
|
|
112
|
-
if default_translation.is_some_and(|t| t == translation) {
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
let locale = symbol_store
|
|
116
|
-
.resolve(*key)
|
|
117
|
-
.ok_or(MessagesError::SymbolNotFound(*key))?;
|
|
118
|
-
// let file = symbol_store
|
|
119
|
-
// .resolve(translation.file())
|
|
120
|
-
// .ok_or(MessagesError::SymbolNotFound(*key))?;
|
|
121
|
-
// let link = format!("[{}]({})", locale, file);
|
|
122
|
-
links.push(locale.into())
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
Ok(links)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
fn make_undefined_message_doc_comment(
|
|
129
|
-
&self,
|
|
130
|
-
message: &Message,
|
|
131
|
-
_known_locales: &FxHashSet<KeySymbol>,
|
|
132
|
-
) -> anyhow::Result<String> {
|
|
133
|
-
let mut result = String::new();
|
|
134
|
-
let translation_links = self.build_translation_links(message.translations(), None)?;
|
|
135
|
-
|
|
136
|
-
write!(
|
|
137
|
-
result,
|
|
138
|
-
" /**\n * Translated in: {}\n * @deprecated - Not defined in default locale\n */",
|
|
139
|
-
translation_links.join(", "),
|
|
140
|
-
)?;
|
|
141
|
-
Ok(result)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
fn make_getter_type_def(&self, message: &Message) -> anyhow::Result<String> {
|
|
145
|
-
let symbol_store = read_global_symbol_store()?;
|
|
146
|
-
|
|
147
|
-
let name = message.key();
|
|
148
|
-
let variables = message.all_variables();
|
|
149
|
-
let mut entries = vec![];
|
|
150
|
-
for (name, instances) in variables.iter() {
|
|
151
|
-
let name = symbol_store
|
|
152
|
-
.resolve(*name)
|
|
153
|
-
.map(String::from)
|
|
154
|
-
.ok_or(MessagesError::SymbolNotFound(*name))?;
|
|
155
|
-
|
|
156
|
-
let type_names = instances
|
|
157
|
-
.iter()
|
|
158
|
-
.map(|instance| get_variable_type_name(&instance.kind))
|
|
159
|
-
.collect::<FxHashSet<&str>>();
|
|
160
|
-
let type_str = Vec::from_iter(type_names).join(" | ");
|
|
161
|
-
entries.push(format!("{}: {}", name, type_str));
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if entries.is_empty() {
|
|
165
|
-
Ok(format!(" '{name}': TypedIntlMessageGetter<undefined>,"))
|
|
166
|
-
} else {
|
|
167
|
-
Ok(format!(
|
|
168
|
-
" '{name}': TypedIntlMessageGetter<{{{}}}>,",
|
|
169
|
-
entries.join(", ")
|
|
170
|
-
))
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
fn get_variable_type_name(kind: &MessageVariableType) -> &str {
|
|
176
|
-
match kind {
|
|
177
|
-
MessageVariableType::Any => "any",
|
|
178
|
-
MessageVariableType::Number => "number",
|
|
179
|
-
MessageVariableType::Plural => "number",
|
|
180
|
-
MessageVariableType::Enum(_) => todo!(),
|
|
181
|
-
MessageVariableType::Date => "number | string | Date",
|
|
182
|
-
MessageVariableType::Time => "number | string | Date",
|
|
183
|
-
MessageVariableType::HookFunction => "HookFunction",
|
|
184
|
-
MessageVariableType::LinkFunction => "LinkFunction",
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
#[derive(Debug, Error)]
|
|
189
|
-
pub enum IntlTypesGeneratorError {
|
|
190
|
-
#[error("Requested source file '{0}' does not exist.")]
|
|
191
|
-
SourceFileNotFound(KeySymbol),
|
|
192
|
-
#[error("Message key '{0}' from source file '{1}' does not exist in the database.")]
|
|
193
|
-
SourceFileMessageNotFound(KeySymbol, KeySymbol),
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
impl<W: std::io::Write> IntlService for IntlTypesGenerator<'_, W> {
|
|
197
|
-
type Result = anyhow::Result<()>;
|
|
198
|
-
|
|
199
|
-
fn run(&mut self) -> Self::Result {
|
|
200
|
-
write!(
|
|
201
|
-
self.output,
|
|
202
|
-
"/* THIS FILE IS AUTOGENERATED. DO NOT EDIT MANUALLY. */
|
|
203
|
-
|
|
204
|
-
import {{TypedIntlMessageGetter}} from '{RUNTIME_PACKAGE_NAME}';
|
|
205
|
-
|
|
206
|
-
type LinkFunction = (content: any[]) => React.ReactNode;
|
|
207
|
-
type HookFunction = (content: any[]) => React.ReactNode;
|
|
208
|
-
|
|
209
|
-
declare const messages: {{
|
|
210
|
-
"
|
|
211
|
-
)?;
|
|
212
|
-
|
|
213
|
-
let known_locales = &self.database.known_locales;
|
|
214
|
-
let source_file = self.database.sources.get(&self.source_file_key).ok_or(
|
|
215
|
-
IntlTypesGeneratorError::SourceFileNotFound(self.source_file_key),
|
|
216
|
-
)?;
|
|
217
|
-
let source_message_keys = source_file.message_keys();
|
|
218
|
-
|
|
219
|
-
for message_key in source_message_keys {
|
|
220
|
-
let Some(message) = self.database.messages.get(message_key) else {
|
|
221
|
-
return Err(IntlTypesGeneratorError::SourceFileMessageNotFound(
|
|
222
|
-
*message_key,
|
|
223
|
-
self.source_file_key,
|
|
224
|
-
)
|
|
225
|
-
.into());
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
let doc_comment = self.make_doc_comment(message, known_locales)?;
|
|
229
|
-
let type_def = self.make_getter_type_def(message)?;
|
|
230
|
-
write!(self.output, "{doc_comment}\n{type_def}\n")?;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
write!(self.output, "}};\nexport default messages;")?;
|
|
234
|
-
|
|
235
|
-
Ok(())
|
|
236
|
-
}
|
|
237
|
-
}
|