Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to use serde_yml #86

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,27 @@ exclude = ["crates", "tests"]
keywords = [
"gettext",
"i18n",
"l10n",
"intl",
"internationalization",
"localization",
"tr",
"translation",
"yml",
]
license = "MIT"
name = "rust-i18n"
readme = "README.md"
repository = "https://github.com/longbridgeapp/rust-i18n"
version = "3.0.2-alpha.0"
version = "3.1.1"

[dependencies]
once_cell = "1.10.0"
rust-i18n-support = { path = "./crates/support", version = "3.0.1" }
rust-i18n-macro = { path = "./crates/macro", version = "3.0.0" }
rust-i18n-support = { path = "./crates/support", version = "3.1.1" }
rust-i18n-macro = { path = "./crates/macro", version = "3.1.1" }
smallvec = "1.12.0"

[dev-dependencies]
foo = { path = "examples/foo" }
criterion = "0.5"
lazy_static = "1"
serde_yaml = "0.8"
serde_yml = "0.0.11"

[build-dependencies]
globwalk = "0.8.1"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl RemoteI18n {
fn new() -> Self {
// fetch translations from remote URL
let response = reqwest::blocking::get("https://your-host.com/assets/locales.yml").unwrap();
let trs = serde_yaml::from_str::<HashMap<String, HashMap<String, String>>>(&response.text().unwrap()).unwrap();
let trs = serde_yml::from_str::<HashMap<String, HashMap<String, String>>>(&response.text().unwrap()).unwrap();

return Self {
trs
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = "MIT"
name = "rust-i18n-cli"
readme = "../../README.md"
repository = "https://github.com/longbridgeapp/rust-i18n"
version = "3.0.0"
version = "3.1.1"

[dependencies]
anyhow = "1"
Expand Down
4 changes: 2 additions & 2 deletions crates/extract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = "MIT"
name = "rust-i18n-extract"
readme = "../../README.md"
repository = "https://github.com/longbridgeapp/rust-i18n"
version = "3.0.0"
version = "3.1.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -18,7 +18,7 @@ regex = "1"
rust-i18n-support = { path = "../support", version = "3.0.0" }
serde = "1"
serde_json = "1"
serde_yaml = "0.8"
serde_yml = "0.0.11"
syn = { version = "2.0.18", features = ["full"] }
toml = "0.7.4"

Expand Down
2 changes: 1 addition & 1 deletion crates/extract/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn convert_text(trs: &Translations, format: &str) -> String {
match format {
"json" => serde_json::to_string_pretty(&value).unwrap(),
"yaml" | "yml" => {
let text = serde_yaml::to_string(&value).unwrap();
let text = serde_yml::to_string(&value).unwrap();
// Remove leading `---`
text.trim_start_matches("---").trim_start().to_string()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = "MIT"
name = "rust-i18n-macro"
readme = "../../README.md"
repository = "https://github.com/longbridgeapp/rust-i18n"
version = "3.0.0"
version = "3.1.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -17,7 +17,7 @@ quote = "1.0.2"
rust-i18n-support = { path = "../support", version = "3.0.0" }
serde = "1"
serde_json = "1"
serde_yaml = "0.8"
serde_yml = "0.0.11"
syn = { version = "2.0.18", features = ["full", "extra-traits"] }

[dev-dependencies]
Expand Down
5 changes: 3 additions & 2 deletions crates/macro/src/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,9 @@ impl syn::parse::Parse for Arguments {

#[derive(Default)]
pub struct Messsage {
pub key: proc_macro2::TokenStream,
pub val: Value,
#[allow(dead_code)]
key: proc_macro2::TokenStream,
val: Value,
}

impl Messsage {
Expand Down
4 changes: 2 additions & 2 deletions crates/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = "MIT"
name = "rust-i18n-support"
readme = "../../README.md"
repository = "https://github.com/longbridgeapp/rust-i18n"
version = "3.0.1"
version = "3.1.1"

[dependencies]
arc-swap = "1.6.0"
Expand All @@ -16,7 +16,7 @@ once_cell = "1.10.0"
proc-macro2 = "1.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_yaml = "0.8"
serde_yml = "0.0.11"
siphasher = "1.0"
toml = "0.7.4"
normpath = "1.1.1"
Expand Down
13 changes: 6 additions & 7 deletions crates/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub fn load_locales<F: Fn(&str) -> bool>(
// Parse Translations from file to support multiple formats
fn parse_file(content: &str, ext: &str, locale: &str) -> Result<Translations, String> {
let result = match ext {
"yml" | "yaml" => serde_yaml::from_str::<serde_json::Value>(content)
"yml" | "yaml" => serde_yml::from_str::<serde_json::Value>(content)
.map_err(|err| format!("Invalid YAML format, {}", err)),
"json" => serde_json::from_str::<serde_json::Value>(content)
.map_err(|err| format!("Invalid JSON format, {}", err)),
Expand Down Expand Up @@ -199,7 +199,7 @@ fn parse_file_v2(key_prefix: &str, data: &serde_json::Value) -> Option<Translati
// en: Welcome
// zh-CN: 欢迎
if text.is_string() {
let key = format_keys(&[&key_prefix, &key]);
let key = format_keys(&[key_prefix, key]);
let sub_trs = HashMap::from([(key, text.clone())]);
let sub_value = serde_json::to_value(&sub_trs).unwrap();

Expand All @@ -212,9 +212,8 @@ fn parse_file_v2(key_prefix: &str, data: &serde_json::Value) -> Option<Translati
if text.is_object() {
// Parse the nested keys
// If the value is object (Map<locale, string>), iter them and convert them and insert into trs
let key = format_keys(&[&key_prefix, &key]);
let key = format_keys(&[key_prefix, key]);
if let Some(sub_trs) = parse_file_v2(&key, value) {
// println!("--------------- sub_trs:\n{:?}", sub_trs);
// Merge the sub_trs into trs
for (locale, sub_value) in sub_trs {
trs.entry(locale)
Expand Down Expand Up @@ -356,14 +355,14 @@ mod tests {

#[test]
fn test_get_version() {
let json = serde_yaml::from_str::<serde_json::Value>("_version: 2").unwrap();
let json = serde_yml::from_str::<serde_json::Value>("_version: 2").unwrap();
assert_eq!(super::get_version(&json), 2);

let json = serde_yaml::from_str::<serde_json::Value>("_version: 1").unwrap();
let json = serde_yml::from_str::<serde_json::Value>("_version: 1").unwrap();
assert_eq!(super::get_version(&json), 1);

// Default fallback to 1
let json = serde_yaml::from_str::<serde_json::Value>("foo: Foo").unwrap();
let json = serde_yml::from_str::<serde_json::Value>("foo: Foo").unwrap();
assert_eq!(super::get_version(&json), 1);
}

Expand Down
4 changes: 1 addition & 3 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ mod tests {

#[test]
fn test_load() {
assert!(load_locales("./tests/locales", |_| false)
.get("en")
.is_some());
assert!(load_locales("./tests/locales", |_| false).contains_key("en"));
}

#[test]
Expand Down
Loading