Closed Almost-Senseless-Coder closed 1 year ago
You don't need to create a new FluentResource and FluentBundle for this, you can access the existing Bundles /w FluentLanguageLoader#with_bundles_mut
.
But yes, a proper way to do this would be nice to have.
@m00nwtchr Doesn't suffice, since you cannot return anything worthwhile from with_bundles_mut
, i.e. you can gain access to the attribute, but not return it to your application.
Unlike with_message
, which returns an Option
of whatever the closure returns, with_bundles_mut
returns ()
.
You can, this is the solution that I've been using in my app:
pub fn fl(message_id: &str, attribute: Option<&str>) -> Option<String> {
let mut message = OnceCell::new();
LANGUAGE_LOADER.with_bundles_mut(|bundle| {
if message.get().is_none() {
if let Some(msg) = bundle.get_message(message_id) {
if let Some(pattern) = if let Some(attribute) = attribute {
msg.get_attribute(attribute).map(|v| v.value())
} else {
msg.value()
} {
message.set(bundle.format_pattern(pattern, None, &mut vec![]).to_string()).unwrap();
}
}
}
});
message.take()
}
Thanks @m00nwtchr for the suggested workaround.
Fixed in #98
Fluent offers a powerful means of grouping related translations: attributes. They can get added to a message to indicate that they belong to that message somehow, even if they do not show up in the message text. Example:
The
fluent-bundle
crate gives access to these attributes using theFluentMessage::get_attribute()
method and one can format an attribute usingFluentBundle::format_pattern()
.Currently
i18n-embed
doesn't offer access to this functionality, which is, in my opinion, a serious limitation. I worked around this using the following code:( Note: The
LanguageLoaderState
is just an application-specific wrapper type that enables global shared state on my application.)I will attempt implementing
fl!
-variant of the formfl!(language_loader, message_id, attribute_id, args)
get_attribute
method on the language loader to do what above methods achieve.PR incoming.