Split translation file in multiple files: one per language

This commit is contained in:
Greg Burri 2025-05-05 23:58:09 +02:00
parent ec36391ec8
commit 6010c8600f
6 changed files with 339 additions and 331 deletions

View file

@ -9,7 +9,7 @@ pub const FILE_CONF: &str = "conf.ron";
/// The name of the translation file.
/// it's located in the current directory.
pub const TRANSLATION_FILE: &str = "translation.ron";
pub const TRANSLATION_DIR: &str = "translations";
/// Filename of the database.
/// It's located in the `DB_DIRECTORY` directory.

View file

@ -1,4 +1,4 @@
use std::{borrow::Borrow, fs::File, sync::LazyLock};
use std::{borrow::Borrow, fs::File, path::Path, sync::LazyLock};
use chrono::Weekday;
pub use common::translation::Sentence;
@ -6,7 +6,7 @@ use common::utils;
use ron::de::from_reader;
use serde::Deserialize;
use strum::EnumCount;
use tracing::warn;
use tracing::{error, warn};
use crate::consts;
@ -215,28 +215,38 @@ fn get_language_translation(code: &str) -> &'static Language {
}
}
/// Lazy load all translation files.
static TRANSLATIONS: LazyLock<Vec<Language>> =
LazyLock::new(|| match File::open(consts::TRANSLATION_FILE) {
Ok(file) => {
let stored_languages: Vec<StoredLanguage> = from_reader(file).unwrap_or_else(|error| {
{
panic!(
"Failed to read translation file {}: {}",
consts::TRANSLATION_FILE,
error
)
LazyLock::new(|| match Path::new(consts::TRANSLATION_DIR).read_dir() {
Ok(entries) => entries
.filter_map(|entry| {
match entry {
Ok(entry) => match File::open(entry.path()) {
Ok(file) => match from_reader(file) {
Ok(language) => return Some(Language::from_stored_language(language)),
Err(error) => {
error!(
"Error when reading translation file {}: {}",
entry.path().to_str().unwrap_or_default(),
error
)
}
},
Err(error) => {
error!("Unable to read translation file: {}", error);
}
},
Err(error) => error!("Unable to read translation entry: {}", error),
}
});
stored_languages
.into_iter()
.map(Language::from_stored_language)
.collect()
}
None
})
.collect(),
Err(error) => {
panic!(
"Failed to open translation file {}: {}",
consts::TRANSLATION_FILE,
error!(
"Unable to read translations directory {}: {}",
consts::TRANSLATION_DIR,
error
)
);
vec![]
}
});