-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n.rs
More file actions
356 lines (309 loc) · 11.5 KB
/
Copy pathi18n.rs
File metadata and controls
356 lines (309 loc) · 11.5 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
//
// Internationalisation (i18n) module for wokelangiser — extracts translatable
// strings from source files, generates locale template files, and produces
// a translation key manifest for integration with existing i18n frameworks.
use anyhow::{Context, Result};
use std::collections::BTreeMap;
use std::fs;
use std::path::Path;
use crate::abi::{I18nString, Locale, LocaleFile};
use crate::manifest::Manifest;
// ---------------------------------------------------------------------------
// Locale file generation
// ---------------------------------------------------------------------------
/// Generate locale files for all supported locales based on extracted strings.
///
/// Output structure:
/// {output_dir}/i18n/
/// en-GB.json — default locale with original values
/// fr-FR.json — template with keys but empty values (to be translated)
/// de-DE.json — template with keys but empty values
/// ...
/// keys.json — key manifest with metadata (source file, line, context)
pub fn generate_locale_files(
manifest: &Manifest,
strings: &[I18nString],
output_dir: &str,
) -> Result<Vec<LocaleFile>> {
let i18n_dir = Path::new(output_dir).join("i18n");
fs::create_dir_all(&i18n_dir)
.with_context(|| format!("Failed to create i18n output dir: {}", i18n_dir.display()))?;
let mut locale_files = Vec::new();
// Generate the default locale file with all extracted values.
let default_locale = Locale::new(&manifest.i18n.default_locale);
let default_translations: Vec<(String, String)> = strings
.iter()
.map(|s| (s.key.clone(), s.default_value.clone()))
.collect();
let default_file = LocaleFile {
locale: default_locale.clone(),
translations: default_translations,
};
write_locale_json(&i18n_dir, &default_file)?;
locale_files.push(default_file);
// Generate template files for all non-default supported locales.
// These contain the same keys but with empty values, ready for translation.
for locale_tag in &manifest.i18n.supported_locales {
if locale_tag == &manifest.i18n.default_locale {
continue; // already generated above
}
let locale = Locale::new(locale_tag);
let translations: Vec<(String, String)> = strings
.iter()
.map(|s| (s.key.clone(), String::new()))
.collect();
let locale_file = LocaleFile {
locale: locale.clone(),
translations,
};
write_locale_json(&i18n_dir, &locale_file)?;
locale_files.push(locale_file);
}
// Generate the key manifest with metadata about each extracted string.
let keys_manifest = generate_keys_manifest(strings);
fs::write(i18n_dir.join("keys.json"), &keys_manifest).context("Failed to write keys.json")?;
println!(
" Generated {} locale files with {} keys in {}",
locale_files.len(),
strings.len(),
i18n_dir.display()
);
Ok(locale_files)
}
/// Write a single locale file as JSON.
/// Format: { "key1": "value1", "key2": "value2", ... }
fn write_locale_json(i18n_dir: &Path, locale_file: &LocaleFile) -> Result<()> {
let map: BTreeMap<&str, &str> = locale_file
.translations
.iter()
.map(|(k, v)| (k.as_str(), v.as_str()))
.collect();
let json = serde_json::to_string_pretty(&map).context("Failed to serialise locale file")?;
let filename = format!("{}.json", locale_file.locale.tag);
fs::write(i18n_dir.join(&filename), &json)
.with_context(|| format!("Failed to write locale file: {}", filename))?;
Ok(())
}
/// Generate a JSON manifest of all translation keys with metadata.
/// This helps translators understand the context of each string.
fn generate_keys_manifest(strings: &[I18nString]) -> String {
let mut entries = Vec::new();
for s in strings {
let context = s.context.as_deref().unwrap_or("none");
entries.push(format!(
r#" {{
"key": "{}",
"default_value": "{}",
"source_file": "{}",
"line": {},
"context": "{}"
}}"#,
escape_json(&s.key),
escape_json(&s.default_value),
escape_json(&s.source_file),
s.line,
escape_json(context),
));
}
format!(
"{{\n \"total_keys\": {},\n \"keys\": [\n{}\n ]\n}}\n",
strings.len(),
entries.join(",\n")
)
}
/// Minimal JSON string escaping for embedded values.
fn escape_json(s: &str) -> String {
s.replace('\\', "\\\\")
.replace('"', "\\\"")
.replace('\n', "\\n")
.replace('\r', "\\r")
.replace('\t', "\\t")
}
// ---------------------------------------------------------------------------
// i18n integration code generation
// ---------------------------------------------------------------------------
/// Generate a lightweight i18n integration module that loads locale files
/// and provides a `t(key)` translation function.
pub fn generate_i18n_module(manifest: &Manifest, output_dir: &str) -> Result<()> {
let i18n_dir = Path::new(output_dir).join("i18n");
fs::create_dir_all(&i18n_dir)?;
let supported: Vec<String> = manifest
.i18n
.supported_locales
.iter()
.map(|l| format!("\"{}\"", l))
.collect();
let module_code = format!(
r#"// SPDX-License-Identifier: MPL-2.0
// Generated by wokelangiser — i18n integration module
// Project: {project}
// Default locale: {default_locale}
// DO NOT EDIT — regenerate with `wokelangiser generate`
/**
* Lightweight i18n module for wokelangiser-generated translations.
*
* Usage:
* import {{ t, setLocale }} from './i18n/i18n.js';
* setLocale('fr-FR');
* console.log(t('str_1')); // translated string
*/
const SUPPORTED_LOCALES = [{supported}];
const DEFAULT_LOCALE = "{default_locale}";
let currentLocale = DEFAULT_LOCALE;
let translations = {{}};
/**
* Load translations for a locale from the corresponding JSON file.
* @param {{string}} locale - BCP 47 locale tag.
* @returns {{Promise<Object>}} The loaded translations object.
*/
async function loadLocale(locale) {{
if (!SUPPORTED_LOCALES.includes(locale)) {{
console.warn(`[wokelangiser i18n] Unsupported locale: ${{locale}}, falling back to ${{DEFAULT_LOCALE}}`);
locale = DEFAULT_LOCALE;
}}
try {{
const response = await fetch(`./${{locale}}.json`);
translations[locale] = await response.json();
}} catch (err) {{
console.error(`[wokelangiser i18n] Failed to load locale ${{locale}}:`, err);
translations[locale] = {{}};
}}
return translations[locale];
}}
/**
* Set the active locale and load its translations.
* @param {{string}} locale - BCP 47 locale tag.
*/
export async function setLocale(locale) {{
currentLocale = locale;
if (!translations[locale]) {{
await loadLocale(locale);
}}
}}
/**
* Translate a key using the current locale.
* Falls back to the default locale if the key is not translated,
* and to the key itself as a last resort.
*
* @param {{string}} key - The translation key (e.g. "str_1").
* @returns {{string}} The translated string.
*/
export function t(key) {{
// Try current locale first.
if (translations[currentLocale] && translations[currentLocale][key]) {{
return translations[currentLocale][key];
}}
// Fall back to default locale.
if (translations[DEFAULT_LOCALE] && translations[DEFAULT_LOCALE][key]) {{
return translations[DEFAULT_LOCALE][key];
}}
// Last resort: return the key itself.
return key;
}}
/**
* Get the current locale tag.
* @returns {{string}} The active BCP 47 locale tag.
*/
export function getLocale() {{
return currentLocale;
}}
/**
* Get the list of supported locales.
* @returns {{string[]}} Array of BCP 47 locale tags.
*/
export function getSupportedLocales() {{
return [...SUPPORTED_LOCALES];
}}
// Initialise with the default locale.
loadLocale(DEFAULT_LOCALE);
"#,
project = manifest.project.name,
default_locale = manifest.i18n.default_locale,
supported = supported.join(", "),
);
fs::write(i18n_dir.join("i18n.js"), &module_code).context("Failed to write i18n.js module")?;
Ok(())
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_escape_json() {
assert_eq!(escape_json("hello"), "hello");
assert_eq!(escape_json("say \"hi\""), "say \\\"hi\\\"");
assert_eq!(escape_json("line\nnew"), "line\\nnew");
}
#[test]
fn test_generate_keys_manifest_empty() {
let manifest = generate_keys_manifest(&[]);
assert!(manifest.contains("\"total_keys\": 0"));
}
#[test]
fn test_generate_keys_manifest_with_strings() {
let strings = vec![
I18nString {
key: "str_1".to_string(),
default_value: "Hello World".to_string(),
source_file: "index.html".to_string(),
line: 10,
context: Some("heading".to_string()),
},
I18nString {
key: "str_2".to_string(),
default_value: "Click here".to_string(),
source_file: "index.html".to_string(),
line: 20,
context: None,
},
];
let manifest = generate_keys_manifest(&strings);
assert!(manifest.contains("\"total_keys\": 2"));
assert!(manifest.contains("Hello World"));
assert!(manifest.contains("str_1"));
assert!(manifest.contains("str_2"));
}
#[test]
fn test_locale_file_generation() {
let dir = tempfile::tempdir().expect("TODO: handle error");
let output_dir = dir.path().to_str().expect("TODO: handle error");
let manifest = crate::manifest::Manifest {
project: crate::manifest::ProjectConfig {
name: "test".to_string(),
source_root: ".".to_string(),
},
consent: Default::default(),
accessibility: Default::default(),
i18n: crate::manifest::I18nConfig {
default_locale: "en-GB".to_string(),
supported_locales: vec!["en-GB".to_string(), "fr-FR".to_string()],
extract_strings: true,
},
report: Default::default(),
};
let strings = vec![I18nString {
key: "str_1".to_string(),
default_value: "Hello".to_string(),
source_file: "test.html".to_string(),
line: 1,
context: None,
}];
let files =
generate_locale_files(&manifest, &strings, output_dir).expect("TODO: handle error");
assert_eq!(files.len(), 2); // en-GB + fr-FR
// Check that the default locale file has the value.
let en_path = dir.path().join("i18n").join("en-GB.json");
let en_content = std::fs::read_to_string(en_path).expect("TODO: handle error");
assert!(en_content.contains("Hello"));
// Check that the fr-FR file has empty values.
let fr_path = dir.path().join("i18n").join("fr-FR.json");
let fr_content = std::fs::read_to_string(fr_path).expect("TODO: handle error");
assert!(fr_content.contains("str_1"));
// The value should be empty string.
assert!(fr_content.contains("\"str_1\": \"\""));
}
}