roblox-slang 3.0.1

Type-safe internationalization for Roblox experiences
Documentation
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use anyhow::{bail, Result};
use std::path::Path;

use crate::config::Config;
pub fn validate_locale_code(locale: &str) -> Result<()> {
    if crate::utils::locales::is_roblox_locale(locale) {
        return Ok(());
    }

    let supported = crate::utils::locales::get_supported_locale_codes();
    bail!(
        "Unsupported locale '{}'\n\
         \n\
         Roblox supports these locales:\n\
         {}",
        locale,
        supported.join(", ")
    )
}
pub fn validate_translation_key(key: &str) -> Result<()> {
    if key.is_empty() {
        bail!("Translation key cannot be empty");
    }
    if key.starts_with('.') {
        bail!(
            "Invalid translation key '{}': Cannot start with a dot\n\
             \n\
             Fix: Remove the leading dot\n\
             Example: '.ui.button' → 'ui.button'",
            key
        );
    }
    if key.ends_with('.') {
        bail!(
            "Invalid translation key '{}': Cannot end with a dot\n\
             \n\
             Fix: Remove the trailing dot\n\
             Example: 'ui.button.' → 'ui.button'",
            key
        );
    }
    if key.contains("..") {
        bail!(
            "Invalid translation key '{}': Cannot contain consecutive dots\n\
             \n\
             Fix: Remove extra dots\n\
             Example: 'ui..button' → 'ui.button'",
            key
        );
    }
    let reserved_chars = ['/', '\\', ':', '*', '?', '"', '<', '>', '|', '\0'];
    if let Some(invalid_char) = key.chars().find(|c| reserved_chars.contains(c)) {
        bail!(
            "Invalid translation key '{}': Contains reserved character '{}'\n\
             \n\
             Reserved characters that cannot be used in keys:\n\
             / \\ : * ? \" < > | (null)\n\
             \n\
             These characters are reserved for file system compatibility.",
            key,
            invalid_char
        );
    }
    if key.chars().any(|c| c.is_whitespace()) {
        bail!(
            "Invalid translation key '{}': Cannot contain whitespace\n\
             \n\
             Fix: Replace spaces with dots or underscores\n\
             Example: 'ui button' → 'ui.button' or 'ui_button'",
            key
        );
    }

    Ok(())
}
pub fn validate_file_exists(path: &Path, description: &str) -> Result<()> {
    if !path.exists() {
        bail!(
            "{} not found: {}\n\
             \n\
             Check that the file exists at the specified path.\n\
             \n\
             If you're starting a new project, run:\n\
             roblox-slang init",
            description
                .chars()
                .next()
                .unwrap()
                .to_uppercase()
                .to_string()
                + &description[1..],
            path.display()
        );
    }

    if !path.is_file() {
        bail!(
            "{} is not a file: {}\n\
             \n\
             Expected a file, but found a directory.",
            description
                .chars()
                .next()
                .unwrap()
                .to_uppercase()
                .to_string()
                + &description[1..],
            path.display()
        );
    }

    Ok(())
}
pub fn validate_directory_exists(path: &Path, description: &str) -> Result<()> {
    if !path.exists() {
        bail!(
            "{} not found: {}\n\
             \n\
             Check that the directory exists at the specified path.\n\
             \n\
             If you're starting a new project, run:\n\
             roblox-slang init",
            description
                .chars()
                .next()
                .unwrap()
                .to_uppercase()
                .to_string()
                + &description[1..],
            path.display()
        );
    }

    if !path.is_dir() {
        bail!(
            "{} is not a directory: {}\n\
             \n\
             Expected a directory, but found a file.",
            description
                .chars()
                .next()
                .unwrap()
                .to_uppercase()
                .to_string()
                + &description[1..],
            path.display()
        );
    }

    Ok(())
}
pub fn validate_safe_path(path: &Path) -> Result<()> {
    let path_str = path.to_string_lossy();
    if path_str.contains("..") {
        bail!(
            "Invalid path '{}': Path traversal not allowed\n\
             \n\
             Paths cannot contain '..' for security reasons.\n\
             Please use absolute paths or paths relative to the project root.",
            path.display()
        );
    }
    #[cfg(unix)]
    {
        if path.is_absolute() && !path_str.starts_with("/tmp") {
            if let Ok(current_dir) = std::env::current_dir() {
                if let Ok(canonical) = path.canonicalize() {
                    if !canonical.starts_with(&current_dir) {
                        bail!(
                            "Invalid path '{}': Absolute paths outside project directory not allowed\n\
                             \n\
                             Please use paths relative to the project root.",
                            path.display()
                        );
                    }
                }
            }
        }
    }

    Ok(())
}
pub fn validate_config(config: &Config) -> Result<()> {
    config.validate()?;
    validate_locale_code(&config.base_locale)
        .map_err(|e| anyhow::anyhow!("Configuration error in base_locale:\n{}", e))?;

    for locale in &config.supported_locales {
        validate_locale_code(locale)
            .map_err(|e| anyhow::anyhow!("Configuration error in supported_locales:\n{}", e))?;
    }
    if let Some(ref namespace) = config.namespace {
        if namespace.is_empty() {
            bail!(
                "Configuration error: namespace cannot be empty\n\
                 \n\
                 Either remove the namespace field or provide a valid value.\n\
                 Example: namespace: MyGame"
            );
        }
        if !namespace.chars().all(|c| c.is_alphanumeric() || c == '_') {
            bail!(
                "Configuration error: namespace '{}' contains invalid characters\n\
                 \n\
                 Namespace can only contain:\n\
                 - Letters (a-z, A-Z)\n\
                 - Digits (0-9)\n\
                 - Underscores (_)\n\
                 \n\
                 Example: namespace: MyGame_Translations",
                namespace
            );
        }
        if namespace.chars().next().unwrap().is_ascii_digit() {
            bail!(
                "Configuration error: namespace '{}' cannot start with a digit\n\
                 \n\
                 Luau identifiers cannot start with digits.\n\
                 Example: '{}' → 'Game{}'",
                namespace,
                namespace,
                namespace
            );
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validate_locale_code_valid() {
        assert!(validate_locale_code("en").is_ok());
        assert!(validate_locale_code("id").is_ok());
        assert!(validate_locale_code("es").is_ok());
        assert!(validate_locale_code("zh-cn").is_ok());
        assert!(validate_locale_code("zh-tw").is_ok());
    }

    #[test]
    fn test_validate_locale_code_empty() {
        assert!(validate_locale_code("").is_err());
    }

    #[test]
    fn test_validate_locale_code_uppercase() {
        let result = validate_locale_code("EN");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Unsupported"));
        assert!(validate_locale_code("en-us").is_err());
    }

    #[test]
    fn test_validate_locale_code_invalid_chars() {
        assert!(validate_locale_code("en US").is_err());
        assert!(validate_locale_code("en_US").is_err());
        assert!(validate_locale_code("en@US").is_err());
    }

    #[test]
    fn test_validate_locale_code_invalid_format() {
        assert!(validate_locale_code("e").is_err()); // Too short
        assert!(validate_locale_code("engl").is_err()); // Too long
    }

    #[test]
    fn test_validate_translation_key_valid() {
        assert!(validate_translation_key("ui").is_ok());
        assert!(validate_translation_key("ui.button").is_ok());
        assert!(validate_translation_key("ui.buttons.buy").is_ok());
        assert!(validate_translation_key("ui.buttons.buy_now").is_ok());
        assert!(validate_translation_key("ui.buttons.buy-now").is_ok());
    }

    #[test]
    fn test_validate_translation_key_empty() {
        assert!(validate_translation_key("").is_err());
    }

    #[test]
    fn test_validate_translation_key_leading_dot() {
        let result = validate_translation_key(".ui.button");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Cannot start with a dot"));
    }

    #[test]
    fn test_validate_translation_key_trailing_dot() {
        let result = validate_translation_key("ui.button.");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Cannot end with a dot"));
    }

    #[test]
    fn test_validate_translation_key_consecutive_dots() {
        let result = validate_translation_key("ui..button");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("consecutive dots"));
    }

    #[test]
    fn test_validate_translation_key_reserved_chars() {
        assert!(validate_translation_key("ui/button").is_err());
        assert!(validate_translation_key("ui\\button").is_err());
        assert!(validate_translation_key("ui:button").is_err());
        assert!(validate_translation_key("ui*button").is_err());
        assert!(validate_translation_key("ui?button").is_err());
    }

    #[test]
    fn test_validate_translation_key_whitespace() {
        let result = validate_translation_key("ui button");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("whitespace"));
    }

    #[test]
    fn test_validate_safe_path_valid() {
        assert!(validate_safe_path(Path::new("translations/en.json")).is_ok());
        assert!(validate_safe_path(Path::new("output/Translations.lua")).is_ok());
    }

    #[test]
    fn test_validate_safe_path_traversal() {
        let result = validate_safe_path(Path::new("../etc/passwd"));
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Path traversal"));
    }

    #[test]
    fn test_validate_safe_path_double_dot() {
        assert!(validate_safe_path(Path::new("../../secret")).is_err());
        assert!(validate_safe_path(Path::new("translations/../../../etc")).is_err());
    }

    #[test]
    fn test_validate_config_valid() {
        let config = Config {
            base_locale: "en".to_string(),
            supported_locales: vec!["en".to_string(), "id".to_string()],
            input_directory: "translations".to_string(),
            output_directory: "output".to_string(),
            namespace: None,
            overrides: None,
            analytics: None,
            cloud: None,
            localization: None,
        };

        assert!(validate_config(&config).is_ok());
    }

    #[test]
    fn test_validate_config_with_namespace() {
        let config = Config {
            base_locale: "en".to_string(),
            supported_locales: vec!["en".to_string()],
            input_directory: "translations".to_string(),
            output_directory: "output".to_string(),
            namespace: Some("MyGame".to_string()),
            overrides: None,
            analytics: None,
            cloud: None,
            localization: None,
        };

        assert!(validate_config(&config).is_ok());
    }

    #[test]
    fn test_validate_config_invalid_base_locale() {
        let config = Config {
            base_locale: "EN".to_string(), // Uppercase not allowed
            supported_locales: vec!["EN".to_string()],
            input_directory: "translations".to_string(),
            output_directory: "output".to_string(),
            namespace: None,
            overrides: None,
            analytics: None,
            cloud: None,
            localization: None,
        };

        let result = validate_config(&config);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Unsupported") || err.contains("base_locale"));
    }

    #[test]
    fn test_validate_config_invalid_supported_locale() {
        let config = Config {
            base_locale: "en".to_string(),
            supported_locales: vec!["en".to_string(), "EN US".to_string()], // Invalid format
            input_directory: "translations".to_string(),
            output_directory: "output".to_string(),
            namespace: None,
            overrides: None,
            analytics: None,
            cloud: None,
            localization: None,
        };

        let result = validate_config(&config);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Unsupported") || err.contains("supported_locales"));
    }

    #[test]
    fn test_validate_config_empty_namespace() {
        let config = Config {
            base_locale: "en".to_string(),
            supported_locales: vec!["en".to_string()],
            input_directory: "translations".to_string(),
            output_directory: "output".to_string(),
            namespace: Some("".to_string()), // Empty namespace
            overrides: None,
            analytics: None,
            cloud: None,
            localization: None,
        };

        let result = validate_config(&config);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("namespace"));
    }

    #[test]
    fn test_validate_config_invalid_namespace_chars() {
        let config = Config {
            base_locale: "en".to_string(),
            supported_locales: vec!["en".to_string()],
            input_directory: "translations".to_string(),
            output_directory: "output".to_string(),
            namespace: Some("My-Game".to_string()), // Hyphen not allowed
            overrides: None,
            analytics: None,
            cloud: None,
            localization: None,
        };

        let result = validate_config(&config);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("invalid characters"));
    }

    #[test]
    fn test_validate_config_namespace_starts_with_digit() {
        let config = Config {
            base_locale: "en".to_string(),
            supported_locales: vec!["en".to_string()],
            input_directory: "translations".to_string(),
            output_directory: "output".to_string(),
            namespace: Some("123Game".to_string()), // Starts with digit
            overrides: None,
            analytics: None,
            cloud: None,
            localization: None,
        };

        let result = validate_config(&config);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("cannot start with a digit"));
    }
}