Skip to content

Commit

Permalink
Update I18n config in Cargo.toml for use package.metadata.i18n, for a…
Browse files Browse the repository at this point in the history
…void Cargo warning.
  • Loading branch information
huacnlee committed Dec 10, 2021
1 parent 4247b42 commit 07fa92c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Add crate dependencies in your Cargo.toml and setup I18n config:
lazy_static = "1.4.0"
rust-i18n = "0"

[i18n]
[package.metadata.i18n]
# The available locales for your application, default: ["en"].
# available-locales = ["en", "zh-CN"]

Expand Down
2 changes: 1 addition & 1 deletion examples/foo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ version = "0.1.0"
lazy_static = "1.4.0"
rust-i18n = {path = "../../../rust-i18n"}

[i18n]
[package.metadata.i18n]
available-locales = ["en", "zh-CN"]
default-locale = "en"
20 changes: 17 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::fs;
use std::io;
use std::io::Read;
use std::path::Path;
use toml;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
Expand Down Expand Up @@ -60,10 +59,10 @@ pub fn load(cargo_root: &Path) -> io::Result<I18nConfig> {
}

pub fn parse(contents: &str) -> io::Result<I18nConfig> {
if !contents.contains("[i18n]") {
if !contents.contains("[i18n]") && !contents.contains("[package.metadata.i18n]") {
return Ok(I18nConfig::default());
}

let contents = contents.replace("[package.metadata.i18n]", "[i18n]");
let mut config: MainConfig = toml::from_str(&contents)?;

// Push default_locale
Expand Down Expand Up @@ -109,6 +108,21 @@ fn test_parse() {
assert_eq!(cfg.load_path, "./locales");
}

#[test]
fn test_parse_with_metadata() {
let contents = r#"
[package.metadata.i18n]
default-locale = "en"
available-locales = ["zh-CN"]
load-path = "./my-locales"
"#;

let cfg = parse(contents).unwrap();
assert_eq!(cfg.default_locale, "en");
assert_eq!(cfg.available_locales, vec!["en", "zh-CN"]);
assert_eq!(cfg.load_path, "./my-locales");
}

#[test]
fn test_load_default() {
let workdir = Path::new(env!["CARGO_MANIFEST_DIR"]);
Expand Down

0 comments on commit 07fa92c

Please sign in to comment.