roblox-slang 1.1.3

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
use crate::roblox::types::CloudConfig;
use crate::utils::locales;
use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};

/// Main configuration structure for Roblox Slang
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Config {
    /// Base locale (e.g., "en")
    pub base_locale: String,

    /// List of supported locales (e.g., ["en", "id", "es"])
    pub supported_locales: Vec<String>,

    /// Input directory containing translation files
    #[serde(default = "default_input_directory")]
    pub input_directory: String,

    /// Output directory for generated files
    #[serde(default = "default_output_directory")]
    pub output_directory: String,

    /// Optional namespace prefix for generated code
    #[serde(default)]
    pub namespace: Option<String>,

    /// Override configuration
    #[serde(default)]
    pub overrides: Option<OverrideConfig>,

    /// Analytics configuration
    #[serde(default)]
    pub analytics: Option<AnalyticsConfig>,

    /// Cloud sync configuration
    #[serde(default)]
    pub cloud: Option<CloudConfig>,
}

/// Override configuration
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct OverrideConfig {
    /// Enable override system
    #[serde(default)]
    pub enabled: bool,

    /// Path to override file (relative to project root)
    #[serde(default = "default_override_file")]
    pub file: String,
}

/// Analytics configuration
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AnalyticsConfig {
    /// Enable analytics tracking
    #[serde(default)]
    pub enabled: bool,

    /// Track missing translations
    #[serde(default = "default_true")]
    pub track_missing: bool,

    /// Track translation usage
    #[serde(default)]
    pub track_usage: bool,

    /// Optional custom callback module path
    #[serde(default)]
    pub callback: Option<String>,
}

impl Config {
    /// Validate configuration values
    pub fn validate(&self) -> Result<()> {
        // Validate base_locale
        if self.base_locale.is_empty() {
            bail!(
                "Configuration error: base_locale cannot be empty\n\
                 \n\
                 Expected format: base_locale: en\n\
                 \n\
                 Hint: The base_locale is your primary language (fallback).\n\
                 Common values: en, es, pt, de, fr, ja, ko, zh-cn, zh-tw"
            );
        }

        // Validate supported_locales
        if self.supported_locales.is_empty() {
            bail!(
                "Configuration error: supported_locales cannot be empty\n\
                 \n\
                 Expected format:\n\
                 supported_locales:\n\
                   - en\n\
                   - id\n\
                   - es\n\
                 \n\
                 Hint: List all languages your game will support."
            );
        }

        // Validate base_locale is in supported_locales
        if !self.supported_locales.contains(&self.base_locale) {
            bail!(
                "Configuration error: base_locale '{}' must be included in supported_locales\n\
                 \n\
                 Current supported_locales: [{}]\n\
                 \n\
                 Fix: Add '{}' to your supported_locales list:\n\
                 supported_locales:\n\
                   - {}\n\
                 {}",
                self.base_locale,
                self.supported_locales.join(", "),
                self.base_locale,
                self.base_locale,
                self.supported_locales
                    .iter()
                    .map(|l| format!("  - {}", l))
                    .collect::<Vec<_>>()
                    .join("\n")
            );
        }

        // Validate that all locales are supported by Roblox
        let mut unsupported = Vec::new();
        for locale in &self.supported_locales {
            if !locales::is_roblox_locale(locale) {
                unsupported.push(locale.clone());
            }
        }

        if !unsupported.is_empty() {
            let supported = locales::get_supported_locale_codes();
            bail!(
                "Configuration error: Unsupported locale(s): {}\n\
                 \n\
                 Roblox supports these 17 locales:\n\
                 {}\n\
                 \n\
                 Common mistakes:\n\
                 - Using uppercase (use 'en' not 'EN')\n\
                 - Using wrong format (use 'zh-cn' not 'zh_CN')\n\
                 - Using unsupported locales\n\
                 \n\
                 Hint: Check https://create.roblox.com/docs/production/localization for details.",
                unsupported.join(", "),
                supported
                    .iter()
                    .map(|l| format!("  • {}", l))
                    .collect::<Vec<_>>()
                    .join("\n")
            );
        }

        // Validate input_directory
        if self.input_directory.is_empty() {
            bail!(
                "Configuration error: input_directory cannot be empty\n\
                 \n\
                 Expected format: input_directory: translations\n\
                 \n\
                 Hint: This is where your JSON/YAML translation files are located."
            );
        }

        // Validate output_directory
        if self.output_directory.is_empty() {
            bail!(
                "Configuration error: output_directory cannot be empty\n\
                 \n\
                 Expected format: output_directory: output\n\
                 \n\
                 Hint: This is where generated Luau code will be placed."
            );
        }

        // Validate input and output are different
        if self.input_directory == self.output_directory {
            bail!(
                "Configuration error: input_directory and output_directory cannot be the same\n\
                 \n\
                 Current value: '{}'\n\
                 \n\
                 Hint: Use different directories to avoid overwriting source files.\n\
                 Example:\n\
                   input_directory: translations\n\
                   output_directory: output",
                self.input_directory
            );
        }

        Ok(())
    }
}

fn default_input_directory() -> String {
    "translations".to_string()
}

fn default_output_directory() -> String {
    "output".to_string()
}

fn default_override_file() -> String {
    "overrides.yaml".to_string()
}

fn default_true() -> bool {
    true
}

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

    #[test]
    fn test_config_validate_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,
        };

        assert!(config.validate().is_ok());
    }

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

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("base_locale cannot be empty"));
    }

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

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("supported_locales cannot be empty"));
    }

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

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("must be included in supported_locales"));
    }

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

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Unsupported locale"));
    }

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

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("input_directory cannot be empty"));
    }

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

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("output_directory cannot be empty"));
    }

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

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("cannot be the same"));
    }

    #[test]
    fn test_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,
        };

        assert!(config.validate().is_ok());
        assert_eq!(config.namespace, Some("MyGame".to_string()));
    }

    #[test]
    fn test_override_config_defaults() {
        let override_config = OverrideConfig {
            enabled: false,
            file: default_override_file(),
        };

        assert!(!override_config.enabled);
        assert_eq!(override_config.file, "overrides.yaml");
    }

    #[test]
    fn test_analytics_config_defaults() {
        let analytics = AnalyticsConfig {
            enabled: false,
            track_missing: default_true(),
            track_usage: false,
            callback: None,
        };

        assert!(!analytics.enabled);
        assert!(analytics.track_missing);
        assert!(!analytics.track_usage);
        assert!(analytics.callback.is_none());
    }

    #[test]
    fn test_analytics_config_with_callback() {
        let analytics = AnalyticsConfig {
            enabled: true,
            track_missing: true,
            track_usage: true,
            callback: Some("game.Analytics.TrackTranslation".to_string()),
        };

        assert!(analytics.enabled);
        assert_eq!(
            analytics.callback,
            Some("game.Analytics.TrackTranslation".to_string())
        );
    }

    #[test]
    fn test_default_functions() {
        assert_eq!(default_input_directory(), "translations");
        assert_eq!(default_output_directory(), "output");
        assert_eq!(default_override_file(), "overrides.yaml");
        assert!(default_true());
    }
}