From 656134084be8144dc97f6c32200a935734ee975f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar=20Rubio?= Date: Mon, 26 Aug 2024 17:43:00 +0200 Subject: [PATCH 1/2] Fix variables not extracted from Fluent selectors in translations checker --- CHANGELOG.md | 8 ++ Cargo.lock | 4 +- leptos-fluent-macros/Cargo.toml | 2 +- .../translations_checker/fluent_entries.rs | 87 ++++++++++++++++++- leptos-fluent/Cargo.toml | 2 +- 5 files changed, 96 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e200f35..e1f40098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## 2024-08-26 - [0.1.20] + +### Bug fixes + +- Fix variables in Fluent selectors not being extracted as placeables when + checking translations. + ## 2024-08-19 - [0.1.19] ### Bug fixes @@ -493,6 +500,7 @@ version to `0.1` during installation. - Added all ISO-639-1 and ISO-639-2 languages. +[0.1.20]: https://github.com/mondeja/leptos-fluent/compare/v0.1.19...v0.1.20 [0.1.19]: https://github.com/mondeja/leptos-fluent/compare/v0.1.18...v0.1.19 [0.1.18]: https://github.com/mondeja/leptos-fluent/compare/v0.1.17...v0.1.18 [0.1.17]: https://github.com/mondeja/leptos-fluent/compare/v0.1.16...v0.1.17 diff --git a/Cargo.lock b/Cargo.lock index 3f1cbe8f..57d1378d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1794,7 +1794,7 @@ dependencies = [ [[package]] name = "leptos-fluent" -version = "0.1.19" +version = "0.1.20" dependencies = [ "current_locale", "directories", @@ -1857,7 +1857,7 @@ dependencies = [ [[package]] name = "leptos-fluent-macros" -version = "0.1.19" +version = "0.1.20" dependencies = [ "cfg-expr 0.15.8", "current_platform", diff --git a/leptos-fluent-macros/Cargo.toml b/leptos-fluent-macros/Cargo.toml index 97e7a09c..8ff5a242 100644 --- a/leptos-fluent-macros/Cargo.toml +++ b/leptos-fluent-macros/Cargo.toml @@ -2,7 +2,7 @@ name = "leptos-fluent-macros" description = "Macros for leptos-fluent" edition.workspace = true -version = "0.1.19" +version = "0.1.20" license = "MIT" documentation.workspace = true repository.workspace = true diff --git a/leptos-fluent-macros/src/translations_checker/fluent_entries.rs b/leptos-fluent-macros/src/translations_checker/fluent_entries.rs index 707d5846..6d031389 100644 --- a/leptos-fluent-macros/src/translations_checker/fluent_entries.rs +++ b/leptos-fluent-macros/src/translations_checker/fluent_entries.rs @@ -47,6 +47,28 @@ fn get_fluent_entries_from_resource( placeables.push(id.name.to_string()); } } + } else if let fluent_syntax::ast::Expression::Select { + selector: fluent_syntax::ast::InlineExpression::VariableReference { id }, + .. + } = expression { + placeables.push(id.name.to_string()); + } else if let fluent_syntax::ast::Expression::Select { + selector: fluent_syntax::ast::InlineExpression::FunctionReference { + arguments: fluent_syntax::ast::CallArguments { + positional, + .. + }, + .. + }, + .. + } = expression { + for arg in positional { + if let fluent_syntax::ast::InlineExpression::VariableReference { + id + } = arg { + placeables.push(id.name.to_string()); + } + } } } } @@ -201,7 +223,7 @@ mod tests { vec!["foo = Bar\nhello = Hello { $name }\n".to_string()], ), ( - Rc::new("en-US".to_string()), + Rc::new("es-ES".to_string()), vec!["foo = Bar\nhello = Hola { $name }\n".to_string()], ), ]); @@ -211,7 +233,7 @@ mod tests { vec!["./locales/en-US/foo.ftl".to_string()], ), ( - Rc::new("en-US".to_string()), + Rc::new("es-ES".to_string()), vec!["./locales/es-ES/foo.ftl".to_string()], ), ]); @@ -241,7 +263,7 @@ mod tests { ] ), ( - Rc::new("en-US".to_string()), + Rc::new("es-ES".to_string()), vec![ FluentEntry { message_name: "foo".to_string(), @@ -411,4 +433,63 @@ emails2 = Number of unread emails { NUMBER($unreadEmails) } )]) ); } + + #[test] + fn fluent_selectors() { + let fluent_resources = HashMap::from([( + Rc::new("en-US".to_string()), + vec![r#"emails = + { $unreadEmails -> + [one] You have one unread email. + *[other] You have { $unreadEmails } unread emails. + } +your-score = + { NUMBER($score, minimumFractionDigits: 1) -> + [0.0] You scored zero points. What happened? + *[other] You scored { NUMBER($score, minimumFractionDigits: 1) } points. + } +your-rank = { NUMBER($pos, type: "ordinal") -> + [1] You finished first! + [one] You finished {$pos}st + [two] You finished {$pos}nd + [few] You finished {$pos}rd + *[other] You finished {$pos}th +} +"# + .to_string()], + )]); + let fluent_file_paths = HashMap::from([( + Rc::new("en-US".to_string()), + vec!["./locales/en-US/foo.ftl".to_string()], + )]); + let workspace_path = "./"; + let (entries, errors) = build_fluent_entries( + &fluent_resources, + &fluent_file_paths, + workspace_path, + &None, + &None, + ); + assert!(errors.is_empty()); + assert_eq!( + entries, + HashMap::from([( + Rc::new("en-US".to_string()), + vec![ + FluentEntry { + message_name: "emails".to_string(), + placeables: vec!["unreadEmails".to_string()] + }, + FluentEntry { + message_name: "your-score".to_string(), + placeables: vec!["score".to_string()] + }, + FluentEntry { + message_name: "your-rank".to_string(), + placeables: vec!["pos".to_string()] + } + ] + ),]) + ); + } } diff --git a/leptos-fluent/Cargo.toml b/leptos-fluent/Cargo.toml index 1d841d30..2ff38c4b 100644 --- a/leptos-fluent/Cargo.toml +++ b/leptos-fluent/Cargo.toml @@ -2,7 +2,7 @@ name = "leptos-fluent" description = "Fluent framework for internationalization of Leptos applications" edition.workspace = true -version = "0.1.19" +version = "0.1.20" license = "MIT" documentation.workspace = true repository.workspace = true From ab34ccb86fde76c8e9c1cf9df6cc950cfeed5de5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar=20Rubio?= Date: Mon, 26 Aug 2024 17:45:59 +0200 Subject: [PATCH 2/2] Run `cargo update` --- Cargo.lock | 130 ++++++++++++++++++++++++++++------------------------- 1 file changed, 70 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 57d1378d..a2cc1afa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -88,7 +88,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" dependencies = [ "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -205,7 +205,7 @@ dependencies = [ "actix-router", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -286,7 +286,7 @@ checksum = "3b43422f69d8ff38f95f1b2bb76517c91589a924d1559a0e935d7c8ce0274c11" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -297,7 +297,7 @@ checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -311,7 +311,7 @@ dependencies = [ "manyhow", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -323,11 +323,11 @@ dependencies = [ "collection_literals", "interpolator", "manyhow", - "proc-macro-utils", + "proc-macro-utils 0.8.0", "proc-macro2", "quote", "quote-use", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -555,9 +555,9 @@ checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3" [[package]] name = "cc" -version = "1.1.13" +version = "1.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72db2f7947ecee9b03b510377e8bb9077afa27176fdbff55c51027e976fdcc48" +checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6" dependencies = [ "jobserver", "libc", @@ -843,7 +843,7 @@ checksum = "62d671cc41a825ebabc75757b62d3d168c577f9149b2d49ece1dad1f72119d25" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -856,7 +856,7 @@ dependencies = [ "proc-macro2", "quote", "rustc_version", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -898,7 +898,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -940,9 +940,9 @@ dependencies = [ [[package]] name = "flate2" -version = "1.0.32" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c0596c1eac1f9e04ed902702e9878208b336edc9d6fddc8a48387349bab3666" +checksum = "324a1be68054ef05ad64b861cc9eaf1d623d2d8cb25b4bf2cb9cdd902b4bf253" dependencies = [ "crc32fast", "miniz_oxide 0.8.0", @@ -991,7 +991,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", "unic-langid", "walkdir", ] @@ -1085,7 +1085,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -1265,7 +1265,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -1437,7 +1437,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -1873,7 +1873,7 @@ dependencies = [ "serde", "serde_json", "serde_yaml", - "syn 2.0.75", + "syn 2.0.76", "tracing", "trybuild", "walkdir", @@ -2018,7 +2018,7 @@ dependencies = [ "quote", "rstml", "serde", - "syn 2.0.75", + "syn 2.0.76", "walkdir", ] @@ -2054,7 +2054,7 @@ dependencies = [ "quote", "rstml", "server_fn_macro", - "syn 2.0.75", + "syn 2.0.76", "tracing", "uuid", ] @@ -2235,7 +2235,7 @@ dependencies = [ "manyhow-macros", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -2244,7 +2244,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c64621e2c08f2576e4194ea8be11daf24ac01249a4f53cd8befcbb7077120ead" dependencies = [ - "proc-macro-utils", + "proc-macro-utils 0.8.0", "proc-macro2", "quote", ] @@ -2553,7 +2553,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -2584,7 +2584,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -2622,12 +2622,12 @@ dependencies = [ [[package]] name = "prettyplease" -version = "0.2.20" +version = "0.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e" +checksum = "479cf940fbbb3426c32c5d5176f62ad57549a0bb84773423ba8be9d089f5faba" dependencies = [ "proc-macro2", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -2679,6 +2679,17 @@ dependencies = [ "smallvec", ] +[[package]] +name = "proc-macro-utils" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eeaf08a13de400bc215877b5bdc088f241b12eb42f0a548d3390dc1c56bb7071" +dependencies = [ + "proc-macro2", + "quote", + "smallvec", +] + [[package]] name = "proc-macro2" version = "1.0.86" @@ -2696,25 +2707,25 @@ checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", "version_check", "yansi", ] [[package]] name = "quote" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] [[package]] name = "quote-use" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48e96ac59974192a2fa6ee55a41211cf1385c5b2a8636a4c3068b3b3dd599ece" +checksum = "9619db1197b497a36178cfc736dc96b271fe918875fbf1344c436a7e93d0321e" dependencies = [ "quote", "quote-use-macros", @@ -2722,15 +2733,14 @@ dependencies = [ [[package]] name = "quote-use-macros" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4c57308e9dde4d7be9af804f6deeaa9951e1de1d5ffce6142eb964750109f7e" +checksum = "82ebfb7faafadc06a7ab141a6f67bcfb24cb8beb158c6fe933f2f035afa99f35" dependencies = [ - "derive-where", - "proc-macro-utils", + "proc-macro-utils 0.10.0", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -2842,7 +2852,7 @@ dependencies = [ "proc-macro2", "proc-macro2-diagnostics", "quote", - "syn 2.0.75", + "syn 2.0.76", "syn_derive", "thiserror", ] @@ -2933,9 +2943,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.208" +version = "1.0.209" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cff085d2cb684faa248efb494c39b68e522822ac0de72ccf08109abde717cfb2" +checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09" dependencies = [ "serde_derive", ] @@ -2953,20 +2963,20 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.208" +version = "1.0.209" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf" +checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] name = "serde_json" -version = "1.0.125" +version = "1.0.127" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83c8e735a073ccf5be70aa8066aa984eaf2fa000db6c8d0100ae605b366d31ed" +checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad" dependencies = [ "itoa", "memchr", @@ -3095,7 +3105,7 @@ dependencies = [ "convert_case 0.6.0", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", "xxhash-rust", ] @@ -3106,7 +3116,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "556e4fd51eb9ee3e7d9fb0febec6cef486dcbc8f7f427591dfcfebee1abe1ad4" dependencies = [ "server_fn_macro", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -3215,9 +3225,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.75" +version = "2.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6af063034fc1935ede7be0122941bafa9bacb949334d090b77ca98b5817c7d9" +checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525" dependencies = [ "proc-macro2", "quote", @@ -3233,7 +3243,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -3304,7 +3314,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -3398,7 +3408,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -3546,7 +3556,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -3614,7 +3624,7 @@ checksum = "1f718dfaf347dcb5b983bfc87608144b0bad87970aebcbea5ce44d2a30c08e63" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -3668,7 +3678,7 @@ checksum = "1ed7f4237ba393424195053097c1516bd4590dc82b84f2f97c5c69e12704555b" dependencies = [ "proc-macro-hack", "quote", - "syn 2.0.75", + "syn 2.0.76", "unic-langid-impl", ] @@ -3802,7 +3812,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", "wasm-bindgen-shared", ] @@ -3836,7 +3846,7 @@ checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3870,7 +3880,7 @@ checksum = "4b8220be1fa9e4c889b30fd207d4906657e7e90b12e0e6b0c8b8d8709f5de021" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]] @@ -4123,7 +4133,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.76", ] [[package]]