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

Extract function positional arguments as Fluent entries placeables #202

Merged
merged 2 commits into from
Aug 4, 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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased - [0.1.11]

### Bug fixes

- Fix translations checker not extracting Fluent function positional arguments
as placeables.

### Enhancements

- Accept multiple configuration conditional checks for the same parameters in
Expand Down Expand Up @@ -423,7 +428,7 @@ version to `0.1` during installation.

- Added all ISO-639-1 and ISO-639-2 languages.

[0.1.11]: https://github.com/mondeja/leptos-fluent/compare/v0.1.10...master
[0.1.11]: https://github.com/mondeja/leptos-fluent/compare/v0.1.10...v0.1.11
[0.1.10]: https://github.com/mondeja/leptos-fluent/compare/v0.1.9...v0.1.10
[0.1.9]: https://github.com/mondeja/leptos-fluent/compare/v0.1.8...v0.1.9
[0.1.8]: https://github.com/mondeja/leptos-fluent/compare/v0.1.7...v0.1.8
Expand Down
73 changes: 69 additions & 4 deletions leptos-fluent-macros/src/translations_checker/fluent_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,32 @@ fn get_fluent_entries_from_resource(
let mut placeables = Vec::new();
for element in &value.elements {
if let fluent_syntax::ast::PatternElement::Placeable {
expression: fluent_syntax::ast::Expression::Inline(
expression,
} = element
{
if let fluent_syntax::ast::Expression::Inline(
fluent_syntax::ast::InlineExpression::VariableReference {
id
}
)
} = element {
placeables.push(id.name.to_string());
) = expression {
placeables.push(id.name.to_string());
} else if let fluent_syntax::ast::Expression::Inline(
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());
}
}
}
}
}
entries.push(FluentEntry {
Expand Down Expand Up @@ -322,4 +341,50 @@ mod tests {
)])
);
}

#[test]
fn test_fluent_functions() {
let fluent_resources = HashMap::from([(
"en-US".to_string(),
vec![
r#"locale-date-format = { DATETIME($date, month: "long", year: "numeric", day: "numeric") }
log-time2 = Entry time: { DATETIME($date) }
emails2 = Number of unread emails { NUMBER($unreadEmails) }
"#.to_string()
],
)]);
let fluent_file_paths = HashMap::from([(
"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([(
"en-US".to_string(),
vec![
FluentEntry {
message_name: "locale-date-format".to_string(),
placeables: vec!["date".to_string()]
},
FluentEntry {
message_name: "log-time2".to_string(),
placeables: vec!["date".to_string()]
},
FluentEntry {
message_name: "emails2".to_string(),
placeables: vec!["unreadEmails".to_string()]
}
]
)])
);
}
}
Loading