diff --git a/leptos-fluent-macros/src/fluent_resources.rs b/leptos-fluent-macros/src/fluent_resources.rs index 7046a9ab..ee5d4a55 100644 --- a/leptos-fluent-macros/src/fluent_resources.rs +++ b/leptos-fluent-macros/src/fluent_resources.rs @@ -1,11 +1,12 @@ use std::collections::HashMap; use std::path::Path; +use std::rc::Rc; -pub(crate) type FluentResources = HashMap>; -pub(crate) type FluentFilePaths = HashMap>; +pub(crate) type FluentResources = HashMap, Vec>; +pub(crate) type FluentFilePaths = HashMap, Vec>; pub(crate) fn build_fluent_resources_and_file_paths( - dir: impl AsRef, + dir: impl AsRef, ) -> ((FluentResources, FluentFilePaths), Vec) { let mut resources = HashMap::new(); let mut paths = HashMap::new(); @@ -19,10 +20,11 @@ pub(crate) fn build_fluent_resources_and_file_paths( if let Some(lang) = entry.file_name().into_string().ok().filter(|l| { l.parse::().is_ok() }) { + let l = Rc::new(lang); let ((file_paths, file_contents), read_errors) = read_from_dir(entry.path()); - resources.insert(lang.clone(), file_contents); - paths.insert(lang, file_paths); + resources.insert(Rc::clone(&l), file_contents); + paths.insert(l, file_paths); errors.extend(read_errors); } else { errors.push(format!( @@ -34,8 +36,8 @@ pub(crate) fn build_fluent_resources_and_file_paths( ((resources, paths), errors) } -fn read_from_dir>( - path: P, +fn read_from_dir( + path: impl AsRef, ) -> ((Vec, Vec), Vec) { let mut paths = Vec::new(); let mut contents = Vec::new(); diff --git a/leptos-fluent-macros/src/lib.rs b/leptos-fluent-macros/src/lib.rs index 5b4c3c3c..5ae8d8eb 100644 --- a/leptos-fluent-macros/src/lib.rs +++ b/leptos-fluent-macros/src/lib.rs @@ -215,7 +215,7 @@ pub fn leptos_fluent( #effect_quote } }, - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -223,14 +223,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, } } @@ -256,7 +256,7 @@ pub fn leptos_fluent( #effect_quote } }, - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -264,14 +264,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -327,7 +327,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => effect_quote, - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -335,14 +335,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -392,7 +392,7 @@ pub fn leptos_fluent( #effect_quote } }, - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -400,14 +400,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -438,7 +438,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => effect_quote.clone(), - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -446,14 +446,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -476,7 +476,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => effect_quote.clone(), - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -484,14 +484,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -534,11 +534,11 @@ pub fn leptos_fluent( } }; match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, } } - None => quote! {}, + None => quote!(), } }) .collect() @@ -560,7 +560,7 @@ pub fn leptos_fluent( Some(ref path) => quote! { #path{#effect_quote} }, None => effect_quote, }, - None => quote! {}, + None => quote!(), } }).collect(); @@ -574,11 +574,11 @@ pub fn leptos_fluent( }); }; match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, } }, - None => quote! {}, + None => quote!(), } }).collect(); @@ -604,7 +604,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => effect_quote, - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -612,14 +612,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -637,7 +637,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => effect_quote, - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -645,14 +645,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -758,7 +758,7 @@ pub fn leptos_fluent( match quote.is_empty() { true => quote! { false }, false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -823,7 +823,7 @@ pub fn leptos_fluent( match quote.is_empty() { true => quote! { false }, false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -877,7 +877,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => sync_html_tag_lang_effect_quote.clone(), - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -885,14 +885,14 @@ pub fn leptos_fluent( #sync_html_tag_lang_effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -906,7 +906,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => sync_html_tag_dir_effect_quote.clone(), - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -914,14 +914,14 @@ pub fn leptos_fluent( #sync_html_tag_dir_effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -958,7 +958,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => effect_quote.clone(), - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -966,14 +966,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1009,7 +1009,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => effect_quote.clone(), - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -1017,14 +1017,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1048,7 +1048,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => effect_quote.clone(), - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -1056,14 +1056,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1088,11 +1088,11 @@ pub fn leptos_fluent( }); }; match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, } } - None => quote! {}, + None => quote!(), }) .collect(); @@ -1160,12 +1160,12 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => parse_language_quote.clone(), - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => { match parse_language_quote.is_empty() { - true => quote! {}, + true => quote!(), false => quote! { if #expr { #parse_language_quote @@ -1173,14 +1173,14 @@ pub fn leptos_fluent( }, } } - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1203,7 +1203,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => set_cookie_quote.clone(), - false => quote! {}, + false => quote!(), }, None => match param.expr { @@ -1212,14 +1212,14 @@ pub fn leptos_fluent( #set_cookie_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, } } @@ -1235,11 +1235,11 @@ pub fn leptos_fluent( }); }; match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, } }, - None => quote! {}, + None => quote!(), } }).collect(); @@ -1264,7 +1264,7 @@ pub fn leptos_fluent( #localstorage_get_quote } }, - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -1272,14 +1272,14 @@ pub fn leptos_fluent( #localstorage_get_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1310,7 +1310,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => effect_quote.clone(), - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -1318,14 +1318,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1347,7 +1347,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => effect_quote.clone(), - false => quote! {}, + false => quote!(), }, None => { match param.expr { @@ -1356,15 +1356,15 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), } } }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1384,7 +1384,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => effect_quote.clone(), - false => quote! {}, + false => quote!(), }, None => { match param.expr { @@ -1393,15 +1393,15 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), } } }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1418,11 +1418,11 @@ pub fn leptos_fluent( }); }; match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, } }, - None => quote! {}, + None => quote!(), } }).collect(); @@ -1453,7 +1453,7 @@ pub fn leptos_fluent( #window_navigator_languages_quote; } }, - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -1461,14 +1461,14 @@ pub fn leptos_fluent( #window_navigator_languages_quote; } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1515,7 +1515,7 @@ pub fn leptos_fluent( #effect_quote } }, - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -1523,14 +1523,14 @@ pub fn leptos_fluent( #effect_quote; } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1568,7 +1568,7 @@ pub fn leptos_fluent( #effect_quote } }, - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -1576,14 +1576,14 @@ pub fn leptos_fluent( #effect_quote; } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1611,11 +1611,11 @@ pub fn leptos_fluent( }); }; match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, } }, - None => quote! {}, + None => quote!(), } }).collect(); @@ -1633,7 +1633,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => effect_quote.clone(), - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -1641,14 +1641,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1675,7 +1675,7 @@ pub fn leptos_fluent( #parse_client_cookie_quote; } }, - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -1683,14 +1683,14 @@ pub fn leptos_fluent( #parse_client_cookie_quote; } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1716,7 +1716,7 @@ pub fn leptos_fluent( let quote = match param.lit { Some(ref lit) => match lit.value { true => effect_quote.clone(), - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -1724,14 +1724,14 @@ pub fn leptos_fluent( #effect_quote } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1775,7 +1775,7 @@ pub fn leptos_fluent( #effect_quote; } }, - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -1783,14 +1783,14 @@ pub fn leptos_fluent( #effect_quote; } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1835,7 +1835,7 @@ pub fn leptos_fluent( #effect_quote; } }, - false => quote! {}, + false => quote!(), }, None => match param.expr { Some(ref expr) => quote! { @@ -1843,14 +1843,14 @@ pub fn leptos_fluent( #effect_quote; } }, - None => quote! {}, + None => quote!(), }, }; match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, }, } @@ -1900,18 +1900,20 @@ pub fn leptos_fluent( } }; - let translations = { + let translations_quote = { let loader::Translations { simple, compound } = translations; - quote! {{ - let mut all_loaders = Vec::new(); - all_loaders.extend([#(& #simple),*]); - #( - all_loaders.extend(#compound.iter()); - );* + quote! { + { + let mut all_loaders = Vec::new(); + all_loaders.extend([#(& #simple),*]); + #( + all_loaders.extend(#compound.iter()); + );* - all_loaders - }} + all_loaders + } + } }; let leptos_fluent_provide_meta_context_quote: proc_macro2::TokenStream = { @@ -1938,10 +1940,10 @@ pub fn leptos_fluent( .map(|param| { let quote = bool_param(¶m.lit, ¶m.expr); match quote.is_empty() { - true => quote! {}, + true => quote!(), false => match param.exprpath { Some(ref path) => { - quote! { #path{#quote} } + quote!(#path{#quote}) } None => quote, }, @@ -1994,7 +1996,7 @@ pub fn leptos_fluent( }; match param.exprpath { - Some(ref path) => quote! { #path{#quote} }, + Some(ref path) => quote!(#path{#quote}), None => quote, } }) @@ -2196,7 +2198,7 @@ pub fn leptos_fluent( None => quote, } } - false => quote! {}, + false => quote!(), } }).collect() }; @@ -2225,10 +2227,11 @@ pub fn leptos_fluent( LANGUAGES[0] }; + let translations = ::std::rc::Rc::new(#translations_quote); let mut i18n = ::leptos_fluent::I18n { language: ::leptos::create_rw_signal(initial_lang), languages: &LANGUAGES, - translations: ::leptos::Signal::derive(|| #translations), + translations: ::leptos::Signal::derive(move || ::std::rc::Rc::clone(&translations)), }; ::leptos::provide_context::<::leptos_fluent::I18n>(i18n); }; diff --git a/leptos-fluent-macros/src/translations_checker/fluent_entries.rs b/leptos-fluent-macros/src/translations_checker/fluent_entries.rs index ad9cd175..717f2338 100644 --- a/leptos-fluent-macros/src/translations_checker/fluent_entries.rs +++ b/leptos-fluent-macros/src/translations_checker/fluent_entries.rs @@ -1,9 +1,13 @@ use crate::{FluentFilePaths, FluentResources}; use std::collections::HashMap; +use std::rc::Rc; + +pub(in crate::translations_checker) type FluentEntries = + HashMap, Vec>; #[cfg_attr(test, derive(PartialEq))] #[cfg_attr(any(debug_assertions, feature = "tracing"), derive(Debug))] -pub(crate) struct FluentEntry { +pub(in crate::translations_checker) struct FluentEntry { pub(crate) message_name: String, pub(crate) placeables: Vec, } @@ -62,13 +66,13 @@ pub(crate) fn build_fluent_entries( workspace_path: &str, core_locales_path: &Option, core_locales_content: &Option, -) -> (HashMap>, Vec) { - let mut fluent_entries: HashMap> = HashMap::new(); +) -> (FluentEntries, Vec) { + let mut fluent_entries: FluentEntries = HashMap::new(); let mut errors: Vec = Vec::new(); for (lang, resources) in fluent_resources { - fluent_entries.insert(lang.to_owned(), vec![]); - for (r, resource_str) in resources.iter().enumerate() { + fluent_entries.insert(Rc::clone(lang), vec![]); + for resource_str in resources { match fluent_templates::fluent_bundle::FluentResource::try_new( resource_str.to_owned(), ) { @@ -79,9 +83,13 @@ pub(crate) fn build_fluent_entries( .extend(get_fluent_entries_from_resource(&resource)); } Err((resource, errs)) => { + let index = resources + .iter() + .position(|r| r == resource_str) + .unwrap(); let file_path = fluent_file_paths .get(lang) - .and_then(|paths| paths.get(r)) + .and_then(|paths| paths.get(index)) .unwrap(); let rel_file_path = pathdiff::diff_paths(file_path, workspace_path) @@ -118,12 +126,8 @@ pub(crate) fn build_fluent_entries( resource_str.to_owned(), ) { Ok(resource) => { - let langs = - fluent_entries.keys().cloned().collect::>(); - for lang in langs { - fluent_entries - .get_mut(&lang) - .unwrap() + for resources in fluent_entries.values_mut() { + resources .extend(get_fluent_entries_from_resource(&resource)); } } @@ -152,12 +156,8 @@ pub(crate) fn build_fluent_entries( .collect::>() .join("\n +") )); - let langs = - fluent_entries.keys().cloned().collect::>(); - for lang in langs { - fluent_entries - .get_mut(&lang) - .unwrap() + for resources in fluent_entries.values_mut() { + resources .extend(get_fluent_entries_from_resource(&resource)); } } @@ -186,21 +186,21 @@ mod tests { fn test_valid() { let fluent_resources = HashMap::from([ ( - "en-US".to_string(), + Rc::new("en-US".to_string()), vec!["foo = Bar\nhello = Hello { $name }\n".to_string()], ), ( - "es-ES".to_string(), + Rc::new("en-US".to_string()), vec!["foo = Bar\nhello = Hola { $name }\n".to_string()], ), ]); let fluent_file_paths = HashMap::from([ ( - "en-US".to_string(), + Rc::new("en-US".to_string()), vec!["./locales/en-US/foo.ftl".to_string()], ), ( - "es-ES".to_string(), + Rc::new("en-US".to_string()), vec!["./locales/es-ES/foo.ftl".to_string()], ), ]); @@ -217,7 +217,7 @@ mod tests { entries, HashMap::from([ ( - "en-US".to_string(), + Rc::new("en-US".to_string()), vec![ FluentEntry { message_name: "foo".to_string(), @@ -230,7 +230,7 @@ mod tests { ] ), ( - "es-ES".to_string(), + Rc::new("en-US".to_string()), vec![ FluentEntry { message_name: "foo".to_string(), @@ -248,10 +248,12 @@ mod tests { #[test] fn test_empty_resource() { - let fluent_resources = - HashMap::from([("en-US".to_string(), vec!["".to_string()])]); + let fluent_resources = HashMap::from([( + Rc::new("en-US".to_string()), + vec!["".to_string()], + )]); let fluent_file_paths = HashMap::from([( - "en-US".to_string(), + Rc::new("en-US".to_string()), vec!["./locales/en-US/foo.ftl".to_string()], )]); let workspace_path = "./"; @@ -263,17 +265,20 @@ mod tests { &None, ); assert!(errors.is_empty()); - assert_eq!(entries, HashMap::from([("en-US".to_string(), vec![])])); + assert_eq!( + entries, + HashMap::from([(Rc::new("en-US".to_string()), vec![])]) + ); } #[test] fn test_empty_message_name() { let fluent_resources = HashMap::from([( - "en-US".to_string(), + Rc::new("en-US".to_string()), vec!["foo =\nbar = Baz".to_string()], )]); let fluent_file_paths = HashMap::from([( - "en-US".to_string(), + Rc::new("en-US".to_string()), vec!["./locales/en-US/foo.ftl".to_string()], )]); let workspace_path = "./"; @@ -295,7 +300,7 @@ mod tests { assert_eq!( entries, HashMap::from([( - "en-US".to_string(), + Rc::new("en-US".to_string()), vec![FluentEntry { message_name: "bar".to_string(), placeables: vec![] @@ -307,11 +312,11 @@ mod tests { #[test] fn test_empty_variable_name() { let fluent_resources = HashMap::from([( - "en-US".to_string(), + Rc::new("en-US".to_string()), vec!["foo = Bar\nhello = Hello { $ }\n".to_string()], )]); let fluent_file_paths = HashMap::from([( - "en-US".to_string(), + Rc::new("en-US".to_string()), vec!["./locales/en-US/foo.ftl".to_string()], )]); let workspace_path = "./"; @@ -333,7 +338,7 @@ mod tests { assert_eq!( entries, HashMap::from([( - "en-US".to_string(), + Rc::new("en-US".to_string()), vec![FluentEntry { message_name: "foo".to_string(), placeables: vec![] @@ -345,7 +350,7 @@ mod tests { #[test] fn test_fluent_functions() { let fluent_resources = HashMap::from([( - "en-US".to_string(), + Rc::new("en-US".to_string()), vec![ r#"locale-date-format = { DATETIME($date, month: "long", year: "numeric", day: "numeric") } log-time2 = Entry time: { DATETIME($date) } @@ -354,7 +359,7 @@ emails2 = Number of unread emails { NUMBER($unreadEmails) } ], )]); let fluent_file_paths = HashMap::from([( - "en-US".to_string(), + Rc::new("en-US".to_string()), vec!["./locales/en-US/foo.ftl".to_string()], )]); let workspace_path = "./"; @@ -369,7 +374,7 @@ emails2 = Number of unread emails { NUMBER($unreadEmails) } assert_eq!( entries, HashMap::from([( - "en-US".to_string(), + Rc::new("en-US".to_string()), vec![ FluentEntry { message_name: "locale-date-format".to_string(), diff --git a/leptos-fluent-macros/src/translations_checker/mod.rs b/leptos-fluent-macros/src/translations_checker/mod.rs index 4bb36ce6..d1932656 100644 --- a/leptos-fluent-macros/src/translations_checker/mod.rs +++ b/leptos-fluent-macros/src/translations_checker/mod.rs @@ -2,8 +2,8 @@ mod fluent_entries; mod tr_macros; use crate::{FluentFilePaths, FluentResources}; -use fluent_entries::{build_fluent_entries, FluentEntry}; -use std::collections::HashMap; +use fluent_entries::build_fluent_entries; +use fluent_entries::FluentEntries; use std::path::Path; use tr_macros::{gather_tr_macro_defs_from_rs_files, TranslationMacro}; @@ -23,10 +23,6 @@ pub(crate) fn run( #[cfg(not(test))] workspace_path, ); - #[cfg(feature = "tracing")] - errors.extend(tr_macros_errors.clone()); - #[cfg(not(feature = "tracing"))] - errors.extend(tr_macros_errors); #[cfg(feature = "tracing")] if !tr_macros_errors.is_empty() { @@ -38,6 +34,8 @@ pub(crate) fn run( tracing::trace!("Gathered tr macros: {:#?}", tr_macros); } + errors.extend(tr_macros_errors); + let (fluent_entries, fluent_syntax_errors) = build_fluent_entries( fluent_resources, fluent_file_paths, @@ -45,21 +43,19 @@ pub(crate) fn run( core_locales_path, core_locales_content, ); - #[cfg(feature = "tracing")] - errors.extend(fluent_syntax_errors.clone()); - #[cfg(not(feature = "tracing"))] - errors.extend(fluent_syntax_errors); #[cfg(feature = "tracing")] - if !fluent_syntax_errors.is_empty() { + if !&fluent_syntax_errors.is_empty() { tracing::warn!( "Errors while building fluent entries: {:#?}", - fluent_syntax_errors + &fluent_syntax_errors ); } else { tracing::trace!("Built fluent entries: {:#?}", fluent_entries); } + errors.extend(fluent_syntax_errors); + let mut check_messages = check_tr_macros_against_fluent_entries(&tr_macros, &fluent_entries); check_messages.extend(check_fluent_entries_against_tr_macros( @@ -75,7 +71,7 @@ pub(crate) fn run( if !check_messages.is_empty() { tracing::warn!( "Errors while checking translations: {:#?}", - check_messages + &check_messages ); } @@ -116,7 +112,7 @@ fn macro_location(tr_macro: &TranslationMacro) -> String { fn check_tr_macros_against_fluent_entries( tr_macros: &Vec, - fluent_entries: &HashMap>, + fluent_entries: &FluentEntries, ) -> Vec { let mut error_messages: Vec = Vec::new(); @@ -189,7 +185,7 @@ fn check_tr_macros_against_fluent_entries( fn check_fluent_entries_against_tr_macros( tr_macros: &Vec, - fluent_entries: &HashMap>, + fluent_entries: &FluentEntries, ) -> Vec { let mut error_messages: Vec = Vec::new(); diff --git a/leptos-fluent-macros/src/translations_checker/tr_macros.rs b/leptos-fluent-macros/src/translations_checker/tr_macros.rs index 3c456382..f3fddc45 100644 --- a/leptos-fluent-macros/src/translations_checker/tr_macros.rs +++ b/leptos-fluent-macros/src/translations_checker/tr_macros.rs @@ -82,7 +82,7 @@ pub(crate) struct TranslationMacro { // On tests is easier to not use file paths #[cfg(not(test))] - pub(crate) file_path: String, + pub(crate) file_path: std::rc::Rc, } impl PartialEq for TranslationMacro { @@ -111,7 +111,7 @@ pub(crate) struct TranslationsMacrosVisitor { current_tr_macro_start: Option, #[cfg(not(test))] - file_path: String, + file_path: std::rc::Rc, } impl TranslationsMacrosVisitor { @@ -132,7 +132,7 @@ impl TranslationsMacrosVisitor { current_message_name: None, current_placeables: Vec::new(), #[cfg(not(test))] - file_path: rel_path, + file_path: std::rc::Rc::new(rel_path), #[cfg(feature = "nightly")] current_tr_macro_start: None, } @@ -263,7 +263,7 @@ impl<'ast> TranslationsMacrosVisitor { message_name: current_message_name.to_owned(), placeables: self.current_placeables.to_owned(), #[cfg(not(test))] - file_path: self.file_path.clone(), + file_path: std::rc::Rc::clone(&self.file_path), #[cfg(feature = "nightly")] start: self.current_tr_macro_start.unwrap(), }; diff --git a/leptos-fluent/src/data_file.rs b/leptos-fluent/src/data_file.rs index b12972c6..d6db9e93 100644 --- a/leptos-fluent/src/data_file.rs +++ b/leptos-fluent/src/data_file.rs @@ -38,7 +38,7 @@ pub fn get(data_file_key: &str) -> Option { #[cfg(feature = "tracing")] tracing::trace!( "Data directory \"{}\" does not exist, created. Language not found", - data_dir.display() + &data_dir.display() ); return None; } @@ -46,14 +46,11 @@ pub fn get(data_file_key: &str) -> Option { #[cfg(feature = "tracing")] tracing::trace!( "Data file \"{}\" does not exist. Language not found", - data_file.display() + &data_file.display() ); return None; } - #[cfg(feature = "tracing")] - let mut file = File::open(data_file.clone()).unwrap(); - #[cfg(not(feature = "tracing"))] - let mut file = File::open(data_file).unwrap(); + let mut file = File::open(&data_file).unwrap(); let mut contents = String::new(); _ = file.read_to_string(&mut contents); if contents.is_empty() { diff --git a/leptos-fluent/src/lib.rs b/leptos-fluent/src/lib.rs index 8de02f88..2ce315cb 100644 --- a/leptos-fluent/src/lib.rs +++ b/leptos-fluent/src/lib.rs @@ -292,6 +292,7 @@ use leptos::{view, SignalGetUntracked}; pub use leptos_fluent_macros::leptos_fluent; #[cfg(feature = "ssr")] use leptos_meta::Html; +use std::rc::Rc; /// Direction of the text #[derive(Debug)] @@ -429,7 +430,7 @@ pub struct I18n { /// Available languages for the application. pub languages: &'static [&'static Language], /// Signal with a vector of fluent-templates static loaders. - pub translations: Signal>>, + pub translations: Signal>>>, } impl I18n {