Skip to content

Conversation

@cbs228
Copy link
Contributor

@cbs228 cbs228 commented Apr 5, 2025

The fl!() macro checks that all assigned arguments exist on the message in question. But fluent messages can also refer to other messages, and those messages can also have arguments.

hello = Hello { to-you }
to-you = to you, {$name}!

Previous versions of fl!() would not permit the name argument to be bound.

Resolving references to other messages requires access to the owning FluentBundle. A variety of alternatives for granting this access are discussed in #144. This PR adds a new public API function, FluentLanguageLoader::with_fluent_message_and_bundle(), which passes both:

  1. the resolved message; and
  2. its owning bundle

to a closure.

With this new capability, fl! macro is updated to properly support MessageReference.

This is an API-enhancing change for i18n-embed and a bugfix for i18n-embed-fl.

Closes #144.

cbs228 added 2 commits April 4, 2025 23:11
Add `FluentLanguageLoader::with_fluent_message_and_bundle()`. This
method functions like `with_fluent_message()`, but it also returns
the `FluentBundle` which owns the message.

This addition permits the closure to resolve `MessageReference`
and to retrieve other messages on the same bundle.
The `fl!()` macro checks that all assigned arguments exist on
the message in question. But fluent messages can also refer to
other messages, and those messages can also have arguments.

```ftl
hello = Hello { to-you }
to-you = to you, {$name}!
```

Previous versions of the macro could not check arguments on
`MessageReference`s since it had no way to resolve them.

Bring the owning `FluentBundle` into scope so that message
references can be resolved. Some unnecessary generics on the
recursive functions are made concrete.

This patch requires an update to i18-embed for new API.
.map(|m| m.value())
.map(|p| args_from_pattern(p, bundle, args));
}
_ => {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we go ahead and match this enum exhaustively? The source enum is not marked #[non_exhaustive], which means that any additions on fluent's part can be considered breaking. Making our match exhaustive will detect any future additions to the AST with breakage here.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @cbs228 that's a great observation! Let's do this in a future PR perhaps? I've created an issue for it #148

@cbs228
Copy link
Contributor Author

cbs228 commented Apr 11, 2025

Hello @kellpossible, can you assign a reviewer for this PR?

@kellpossible
Copy link
Owner

Hi @cbs228 sorry I didn't see this until now! (Email notifications never seem to work for me, until someone comments), I'd be happy to review this, this coming week.

@kellpossible
Copy link
Owner

Thanks @cbs228 this is a significant contribution!

@kellpossible kellpossible merged commit 3274a55 into kellpossible:master Apr 14, 2025
3 checks passed
@kellpossible
Copy link
Owner

@cbs228 I published a new release with this change

@cbs228
Copy link
Contributor Author

cbs228 commented Apr 14, 2025

Thanks, I appreciate the new release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

i18n-embed-fl: follow MessageReference when validating arguments

2 participants