From f51fb879c246f07c8545b4905566a38fe61cc67a Mon Sep 17 00:00:00 2001 From: Jannis Date: Mon, 14 Apr 2025 19:41:10 +0200 Subject: [PATCH 1/3] Implement read config from file --- crates/cli/src/main.rs | 18 +++++++++++++++++- crates/support/src/config.rs | 22 +++++++++++++++++++++- examples/foo/i18n.toml | 3 +++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 examples/foo/i18n.toml diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index f1722a1..292b302 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -5,6 +5,8 @@ use rust_i18n_extract::{extractor, generator, iter}; use rust_i18n_support::{I18nConfig, MinifyKey}; use std::{collections::HashMap, path::Path}; +const I18N_CONFIG_FILE: &str = "i18n.toml"; + #[derive(Parser)] #[command(name = "cargo")] #[command(bin_name = "cargo")] @@ -96,6 +98,20 @@ fn add_translations( } } +fn read_config(source_path: &str) -> std::io::Result { + let root_dir = std::path::Path::new(&source_path); + let config_file = root_dir.join(I18N_CONFIG_FILE); + + let config_file_exists = std::fs::exists(&config_file) + .unwrap_or_else(|err| panic!("Error opening '{I18N_CONFIG_FILE}': {err}")); + + if config_file_exists { + I18nConfig::load_from_file(&config_file) + } else { + I18nConfig::load(&root_dir.join("Cargo.toml")) + } +} + fn main() -> Result<(), Error> { let CargoCli::I18n(args) = CargoCli::parse(); @@ -103,7 +119,7 @@ fn main() -> Result<(), Error> { let source_path = args.source.expect("Missing source path"); - let cfg = I18nConfig::load(std::path::Path::new(&source_path))?; + let cfg = read_config(&source_path)?; iter::iter_crate(&source_path, |path, source| { extractor::extract(&mut results, path, source, cfg.clone()) diff --git a/crates/support/src/config.rs b/crates/support/src/config.rs index ce9ba88..62ec6ec 100644 --- a/crates/support/src/config.rs +++ b/crates/support/src/config.rs @@ -54,7 +54,17 @@ impl I18nConfig { pub fn load(cargo_root: &Path) -> io::Result { let cargo_file = cargo_root.join("Cargo.toml"); let mut file = fs::File::open(&cargo_file) - .unwrap_or_else(|e| panic!("Fail to open {}, {}", cargo_file.display(), e)); + .unwrap_or_else(|e| panic!("Failed to open {}, {}", cargo_file.display(), e)); + + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + + Self::parse(&contents) + } + + pub fn load_from_file(file: &Path) -> io::Result { + let mut file = fs::File::open(file) + .unwrap_or_else(|e| panic!("Failed to open {}, {}", file.display(), e)); let mut contents = String::new(); file.read_to_string(&mut contents)?; @@ -207,3 +217,13 @@ fn test_load() { assert_eq!(cfg.default_locale, "en"); assert_eq!(cfg.available_locales, vec!["en", "zh-CN"]); } + +#[test] +fn test_load_from_file() { + let workdir = Path::new(env!["CARGO_MANIFEST_DIR"]); + let file = workdir.join("../../examples/foo/i18n.toml"); + + let cfg = I18nConfig::load_from_file(&file).unwrap(); + assert_eq!(cfg.default_locale, "en"); + assert_eq!(cfg.available_locales, vec!["en", "zh-CN"]); +} diff --git a/examples/foo/i18n.toml b/examples/foo/i18n.toml new file mode 100644 index 0000000..11d5cb3 --- /dev/null +++ b/examples/foo/i18n.toml @@ -0,0 +1,3 @@ +[i18n] +available-locales = ["en", "zh-CN"] +default-locale = "en" From b182ef1de7af16cc40c6db7cbdb16868b79309e9 Mon Sep 17 00:00:00 2001 From: Jannis Date: Mon, 14 Apr 2025 19:43:11 +0200 Subject: [PATCH 2/3] Improve documentation --- crates/cli/src/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 292b302..c415c15 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -98,6 +98,8 @@ fn add_translations( } } +/// Reads the configuration either from the `i18n.toml` file, if present, +/// or from the `Cargo.toml` file otherwise. fn read_config(source_path: &str) -> std::io::Result { let root_dir = std::path::Path::new(&source_path); let config_file = root_dir.join(I18N_CONFIG_FILE); From 08b49f0ba6c17bd868efd14ec44fd667c964d311 Mon Sep 17 00:00:00 2001 From: Jannis Date: Mon, 14 Apr 2025 20:04:56 +0200 Subject: [PATCH 3/3] Fix reading from Cargo.toml file --- crates/cli/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index c415c15..322ad67 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -110,7 +110,7 @@ fn read_config(source_path: &str) -> std::io::Result { if config_file_exists { I18nConfig::load_from_file(&config_file) } else { - I18nConfig::load(&root_dir.join("Cargo.toml")) + I18nConfig::load(&root_dir) } }