Skip to content

Commit d39eab4

Browse files
committed
Convenience getter for localisation
1 parent 8ed024c commit d39eab4

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

src/modules/divvun/suggest.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use cg3::Block;
55
use divvun_runtime_macros::rt_command;
66
use fluent_bundle::FluentArgs;
77
use heck::ToTitleCase as _;
8+
use indexmap::IndexMap;
89
use once_cell::sync::Lazy;
910
use regex::Regex;
1011
use serde::Deserialize;
@@ -20,22 +21,22 @@ struct ErrorJsonEntry {
2021
re: Option<String>,
2122
}
2223

23-
fn load_error_mappings(context: &Arc<Context>) -> Result<HashMap<String, Vec<Id>>, Error> {
24+
fn load_error_mappings(context: &Arc<Context>) -> Result<IndexMap<String, Vec<Id>>, Error> {
2425
// Try to find errors.json in the bundle
2526
let errors_json_path = context.extract_to_temp_dir("errors.json")?;
2627

2728
if !errors_json_path.exists() {
2829
tracing::debug!("No errors.json found, using empty error mappings");
29-
return Ok(HashMap::new());
30+
return Ok(IndexMap::new());
3031
}
3132

3233
let content = fs::read_to_string(&errors_json_path)
3334
.map_err(|e| Error(format!("Failed to read errors.json: {}", e)))?;
3435

35-
let raw_mappings: HashMap<String, Vec<ErrorJsonEntry>> = serde_json::from_str(&content)
36+
let raw_mappings: IndexMap<String, Vec<ErrorJsonEntry>> = serde_json::from_str(&content)
3637
.map_err(|e| Error(format!("Failed to parse errors.json: {}", e)))?;
3738

38-
let mut mappings = HashMap::new();
39+
let mut mappings = IndexMap::new();
3940

4041
for (key, entries) in raw_mappings {
4142
let mut ids = Vec::new();
@@ -104,7 +105,7 @@ pub struct Suggest {
104105
_context: Arc<Context>,
105106
generator: Arc<hfst::Transducer>,
106107
fluent_loader: FluentLoader,
107-
error_mappings: Arc<HashMap<String, Vec<Id>>>,
108+
error_mappings: Arc<IndexMap<String, Vec<Id>>>,
108109
}
109110

110111
#[rt_command(
@@ -144,9 +145,45 @@ impl Suggest {
144145
}) as _)
145146
}
146147

147-
pub fn error_mappings(&self) -> &Arc<HashMap<String, Vec<Id>>> {
148+
pub fn error_mappings(&self) -> &Arc<IndexMap<String, Vec<Id>>> {
148149
&self.error_mappings
149150
}
151+
152+
pub fn error_preferences(&self, language_tags: &[&str]) -> IndexMap<String, String> {
153+
let mut prefs = IndexMap::new();
154+
155+
for key in self.error_mappings.keys() {
156+
let mut best_msg: Option<String> = None;
157+
for lang in language_tags {
158+
match self.fluent_loader.get_message(Some(&lang), key, None) {
159+
Ok((title, _)) => {
160+
best_msg = Some(title);
161+
}
162+
Err(_) => {
163+
continue;
164+
}
165+
}
166+
}
167+
168+
if best_msg.is_none() {
169+
// Try default language
170+
match self.fluent_loader.get_message(None, key, None) {
171+
Ok((title, _)) => {
172+
best_msg = Some(title);
173+
}
174+
Err(_) => {}
175+
}
176+
}
177+
178+
if let Some(msg) = best_msg {
179+
prefs.insert(key.clone(), msg);
180+
} else {
181+
prefs.insert(key.clone(), key.clone());
182+
}
183+
}
184+
185+
prefs
186+
}
150187
}
151188

152189
#[async_trait]

0 commit comments

Comments
 (0)