Skip to main content

cargo_zng/l10n/
generate_util.rs

1use std::{borrow::Cow, fmt::Write as _, fs, path::Path};
2
3use fluent_syntax::ast::{Attribute, CallArguments, Entry, Expression, Identifier, InlineExpression, Pattern, PatternElement, VariantKey};
4
5use crate::util::{self, glob_walker};
6
7pub fn transform_dir(
8    dir: &str,
9    to_name: &str,
10    file_header: &str,
11    filter: &dyn Fn(&str, &str) -> bool,
12    transform: &dyn Fn(&str) -> Cow<str>,
13    check: bool,
14) {
15    let dir_path = Path::new(dir);
16    let pattern = dir_path.join("**/*.ftl");
17    let to_dir = dir_path.with_file_name(to_name);
18    for entry in glob_walker(&pattern.display().to_string(), true).unwrap_or_else(|e| fatal!("cannot read `{dir}`, {e}")) {
19        let entry = entry.unwrap_or_else(|e| fatal!("cannot read `{dir}` entry, {e}")).into_path();
20        let relative_entry = entry.strip_prefix(dir_path).unwrap();
21        let to_file = to_dir.join(relative_entry);
22        let _ = util::check_or_create_dir_all(check, to_file.parent().unwrap());
23        let ok = transform_file(&entry, &to_file, file_header, filter, transform, check);
24        if ok {
25            let display_to = to_file.strip_prefix(to_dir.parent().unwrap()).unwrap();
26            println!("  generated {}", display_to.display());
27        }
28    }
29}
30
31#[allow(clippy::too_many_arguments)]
32pub fn transform_file(
33    from: &Path,
34    to: &Path,
35    file_header: &str,
36    filter: &dyn Fn(&str, &str) -> bool,
37    transform: &dyn Fn(&str) -> Cow<str>,
38    check: bool,
39) -> bool {
40    let source = match fs::read_to_string(from) {
41        Ok(s) => s,
42        Err(e) => {
43            error!("cannot read `{}`, {e}", from.display());
44            return false;
45        }
46    };
47
48    let mut all_ok = true;
49    let source = match fluent_syntax::parser::parse(source) {
50        Ok(s) => s,
51        Err((s, e)) => {
52            all_ok = false;
53            error!(
54                "cannot parse `{}`\n{}",
55                from.display(),
56                e.into_iter().map(|e| format!("    {e}")).collect::<Vec<_>>().join("\n")
57            );
58            s
59        }
60    };
61
62    let mut output = file_header.to_owned();
63
64    for entry in source.body {
65        match entry {
66            Entry::Message(m) => write_entry(&mut output, false, &m.id, m.value.as_ref(), &m.attributes, filter, transform),
67            Entry::Term(t) => write_entry(&mut output, true, &t.id, Some(&t.value), &t.attributes, filter, transform),
68            Entry::Comment(_) | Entry::GroupComment(_) | Entry::ResourceComment(_) | Entry::Junk { .. } => {}
69        }
70    }
71
72    if let Err(e) = util::check_or_write(check, to, output.trim().as_bytes()) {
73        all_ok = false;
74        error!("cannot write `{}`, {e}", to.display());
75    }
76
77    all_ok
78}
79
80fn write_entry(
81    output: &mut String,
82    is_term: bool,
83    id: &Identifier<String>,
84    value: Option<&Pattern<String>>,
85    attributes: &[Attribute<String>],
86    filter: &dyn Fn(&str, &str) -> bool,
87    transform: &dyn Fn(&str) -> Cow<str>,
88) {
89    if !is_term && !filter(&id.name, "") {
90        return;
91    }
92
93    write!(output, "\n\n{}{} = ", if is_term { "-" } else { "" }, id.name).unwrap();
94    if let Some(value) = value {
95        write_pattern(output, value, transform, 1);
96    }
97    for attr in attributes {
98        if !is_term && !filter(&id.name, &attr.id.name) {
99            return;
100        }
101
102        write!(output, "\n    .{} = ", attr.id.name).unwrap();
103        write_pattern(output, &attr.value, transform, 2);
104    }
105}
106
107fn write_pattern(output: &mut String, pattern: &Pattern<String>, transform: &dyn Fn(&str) -> Cow<str>, depth: usize) {
108    for el in &pattern.elements {
109        match el {
110            PatternElement::TextElement { value } => {
111                let mut prefix = String::new();
112                for line in value.split('\n') {
113                    // not .lines() because is consumes trailing empty lines
114                    write!(output, "{prefix}{}", transform(line)).unwrap();
115                    prefix = format!("\n{}", " ".repeat(depth * 4));
116                }
117            }
118            PatternElement::Placeable { expression } => write_expression(output, expression, transform, depth),
119        }
120    }
121}
122
123fn write_expression(output: &mut String, expr: &Expression<String>, transform: &dyn Fn(&str) -> Cow<str>, depth: usize) {
124    match expr {
125        Expression::Select { selector, variants } => {
126            write!(output, "{{").unwrap();
127            write_inline_expression_inner(output, selector, transform, depth);
128            writeln!(output, " ->").unwrap();
129
130            for v in variants {
131                write!(output, "{}", " ".repeat((depth + 1) * 4)).unwrap();
132                if v.default {
133                    write!(output, "*").unwrap();
134                }
135                let key = match &v.key {
136                    VariantKey::Identifier { name } => name,
137                    VariantKey::NumberLiteral { value } => value,
138                };
139                write!(output, "[{key}] ").unwrap();
140
141                write_pattern(output, &v.value, transform, depth + 2);
142                writeln!(output).unwrap();
143            }
144
145            writeln!(output, "}}").unwrap();
146        }
147        Expression::Inline(e) => write_inline_expression(output, e, transform, depth),
148    }
149}
150fn write_inline_expression(output: &mut String, expr: &InlineExpression<String>, transform: &dyn Fn(&str) -> Cow<str>, depth: usize) {
151    write!(output, "{{ ").unwrap();
152    write_inline_expression_inner(output, expr, transform, depth);
153    write!(output, " }} ").unwrap();
154}
155fn write_inline_expression_inner(output: &mut String, expr: &InlineExpression<String>, transform: &dyn Fn(&str) -> Cow<str>, depth: usize) {
156    match expr {
157        InlineExpression::StringLiteral { value } => {
158            let value = transform(value);
159            let value = value.replace('\\', "\\\\").replace('"', "\\\"");
160            write!(output, "\"{value}\"").unwrap()
161        }
162        InlineExpression::NumberLiteral { value } => write!(output, "{value}").unwrap(),
163        InlineExpression::FunctionReference { id, arguments } => {
164            write!(output, "{}", id.name).unwrap();
165            write_arguments(output, arguments, transform, depth);
166        }
167        InlineExpression::MessageReference { id, attribute } => {
168            write!(output, "{}", id.name).unwrap();
169            if let Some(a) = attribute {
170                write!(output, ".{}", a.name).unwrap();
171            }
172        }
173        InlineExpression::TermReference { id, attribute, arguments } => {
174            write!(output, "-{}", id.name).unwrap();
175            if let Some(a) = attribute {
176                write!(output, ".{}", a.name).unwrap();
177            }
178            if let Some(args) = arguments {
179                write_arguments(output, args, transform, depth);
180            }
181        }
182        InlineExpression::VariableReference { id } => write!(output, "${}", id.name).unwrap(),
183        InlineExpression::Placeable { expression } => {
184            write_expression(output, expression, transform, depth);
185        }
186    }
187}
188
189fn write_arguments(output: &mut String, arguments: &CallArguments<String>, transform: &dyn Fn(&str) -> Cow<str>, depth: usize) {
190    write!(output, "(").unwrap();
191    let mut sep = "";
192    for a in &arguments.positional {
193        write!(output, "{sep}").unwrap();
194        write_inline_expression_inner(output, a, transform, depth);
195        sep = ", ";
196    }
197    for a in &arguments.named {
198        write!(output, "{sep}{}:", a.name.name).unwrap();
199        write_inline_expression_inner(output, &a.value, transform, depth);
200        sep = ", ";
201    }
202    write!(output, ")").unwrap();
203}
204
205#[cfg(test)]
206mod tests {
207    use super::*;
208
209    #[test]
210    fn basic_write_entry() {
211        let source = r#"
212-lang = en-US
213
214button = Button
215
216window = 
217    .title = Localize Example ({-lang})
218
219click-count = {$n ->
220    [one] Clicked {$n} time
221    *[other] Clicked {$n} times
222}
223key-count = {NUMBER($n) ->
224    [one] Clicked {$n} time
225    *[other] Clicked {$n} times
226}
227        "#;
228        let source = fluent_syntax::parser::parse(source.to_owned()).unwrap();
229
230        let mut output = String::new();
231        for entry in &source.body {
232            match entry {
233                Entry::Message(m) => write_entry(&mut output, false, &m.id, m.value.as_ref(), &m.attributes, &|_, _| true, &|a| {
234                    Cow::Borrowed(a)
235                }),
236                Entry::Term(t) => write_entry(&mut output, true, &t.id, Some(&t.value), &t.attributes, &|_, _| true, &|a| {
237                    Cow::Borrowed(a)
238                }),
239                _ => {}
240            }
241        }
242
243        let _ = fluent_syntax::parser::parse(output.clone()).unwrap_or_else(|e| panic!("write_entry output invalid\n{}\n{output}", e.1[0]));
244    }
245}