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
use std::collections::HashMap;
use bracket::{
error::HelperError,
helper::{Helper, HelperValue},
parser::ast::{Node, ParameterValue},
render::{Context, Render, Type},
};
use serde_json::Value;
use fluent_templates::fluent_bundle::FluentValue;
use fluent_templates::LanguageIdentifier;
use fluent_templates::Loader;
static FLUENT_PARAM: &str = "fluentparam";
pub struct FluentHelper {
loader: Box<dyn Loader + Send + Sync>,
escaped: bool,
trim: bool,
}
impl FluentHelper {
pub fn new(
loader: Box<dyn Loader + Send + Sync>,
escaped: bool,
trim: bool,
) -> Self {
Self {
loader,
escaped,
trim,
}
}
}
impl Helper for FluentHelper {
fn call<'render, 'call>(
&self,
rc: &mut Render<'render>,
ctx: &Context<'call>,
template: Option<&'render Node<'render>>,
) -> HelperValue {
ctx.arity(1..usize::MAX)?;
let msg_id = ctx.try_get(0, &[Type::String])?.as_str().unwrap();
let lang = rc
.evaluate("@root.lang")?
.ok_or_else(|| {
HelperError::Message(format!(
"Helper '{}' requires a 'lang' variable in the template data",
ctx.name()
))
})?
.as_str()
.ok_or_else(|| {
HelperError::Message(format!(
"Type error in helper '{}' the 'lang' variable must be a string",
ctx.name()
))
})?;
let mut args: Option<HashMap<String, FluentValue>> =
if ctx.parameters().is_empty() {
None
} else {
let map = ctx
.parameters()
.iter()
.filter_map(|(k, v)| {
let val = match v {
Value::Number(n) => n.as_f64().unwrap().into(),
Value::String(s) => s.to_owned().into(),
_ => return None,
};
Some((k.to_string(), val))
})
.collect();
Some(map)
};
if let Some(node) = template {
for child in node.into_iter() {
match child {
Node::Block(ref block) => {
if let Some("fluentparam") = block.name() {
let content = rc.buffer(child)?;
let param = block.call().arguments().get(0).ok_or_else(|| {
HelperError::new(
format!("Block '{}' must have a single argument", FLUENT_PARAM)
)
})?;
if let ParameterValue::Json(ref value) = param {
if let Value::String(ref s) = value {
let params =
args.get_or_insert(HashMap::new());
let value = if self.trim {
content.trim().to_string()
} else {
content
};
params.insert(
s.to_string(),
FluentValue::String(value.into()),
);
} else {
return Err(HelperError::new(
format!("Block '{}' expects a string argument", FLUENT_PARAM)
));
}
} else {
return Err(HelperError::new(
format!("Block '{}' expects a JSON literal argument", FLUENT_PARAM)
));
}
} else {
return Err(HelperError::new(format!(
"Helper '{}' only allows '{}' blocks",
ctx.name(), FLUENT_PARAM
)));
}
}
_ => {}
}
}
}
let lang_id = lang
.parse::<LanguageIdentifier>()
.map_err(|e| HelperError::new(e.to_string()))?;
let message =
self.loader
.lookup_complete(&lang_id, &msg_id, args.as_ref());
if self.escaped {
rc.write_escaped(&message)?;
} else {
rc.write(&message)?;
}
Ok(None)
}
}