Skip to main content

factorio_ir/
locale.rs

1#[derive(Debug, Clone, PartialEq, Eq)]
2pub struct LocaleEntry {
3    /// Factorio category name (e.g. `mod-setting-name`), if any.
4    pub category: Option<String>,
5    /// Locale key (e.g. `msr-casual-mode`).
6    pub key: String,
7    /// Translated / English template string.
8    pub value: String,
9}
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct LocaleFile {
13    /// Language code (`en`, `de`, ...).
14    pub lang: String,
15    /// File stem written under `locale/<lang>/` (without `.cfg`).
16    pub file: String,
17    pub entries: Vec<LocaleEntry>,
18}
19
20impl LocaleFile {
21    /// Serialize to Factorio's `.cfg` format.
22    #[must_use]
23    pub fn to_cfg(&self) -> String {
24        let mut output = String::new();
25        let mut current_category: Option<&str> = None;
26        let mut first_section = true;
27
28        for entry in &self.entries {
29            let category = entry.category.as_deref();
30            if category != current_category {
31                if !first_section {
32                    output.push('\n');
33                }
34                if let Some(name) = category {
35                    output.push('[');
36                    output.push_str(name);
37                    output.push_str("]\n");
38                }
39                current_category = category;
40                first_section = false;
41            }
42
43            output.push_str(&entry.key);
44            output.push('=');
45            output.push_str(&entry.value);
46            output.push('\n');
47        }
48
49        output
50    }
51}