Closed MrTact closed 4 years ago
@MrTact Thank you for your issue! All of this is already available in this library. You can pass placeables by simply naming them in the fluent call (E.g. {{fluent "hello" var="foo"}}
. For resolving that you want to use Handlebars::render_template
or an equivalent method. If you have any other questions feel free to ask!
See also for more examples of the syntax https://github.com/XAMPPRocky/fluent-templates#handlebars-helper-syntax
I do have more questions 😀. Either I'm not communicating the use case effectively, or I'm not grokking the docs well enough to apply what's there. Let me elaborate and hopefully you can help me figure out how to bridge the gap in my understanding.
Let's take the example from the docs. Here's the relevant part of our .ftl file for locale en-US:
placeholder = this has a placeholder { $variable }
We register the fluent loader with Handlebars, and then we do:
let data = serde_json::json!({"lang": "en-US"});
println!("{}", handlebars.render_template(r#{{fluent "placeholder" variable="baz"}}#, &data).unwrap());
Which produces
this has a placeholder baz
So far so good.
What I'm trying to achieve is as follows -- use the same localization file, and then the following code:
let data = serde_json::json!({
"lang": "en-US",
"variable": "baz"
});
println!("{}", handlebars.render_template(r#{{fluent "placeholder"}}#, &data).unwrap());
And get the same result. In other words, not have to bake the placeable into the handlebars template, but have it looked up in the Handlebars render context instead. Reason being that's all dynamic data, so I would essentially have to generate the templates at runtime. All the approaches I've tried to make this happen panic on failure to resolve the placeables.
@MrTact Well you can pass the data by saying variable=variable
. If you wanted it to be implicitly passed like that is not supported out of the box, and I don't think I would support this in the library itself. However it would not be hard to build your own newtype wrapper that does the same. I'd recommend creating your own FluentHelper
type that accepts a Loader
and implement the HelperDef
yourself so that it passes the context to the loader. You can look at the implementation in the library as an example.
https://github.com/XAMPPRocky/fluent-templates/blob/master/templates/src/loader/handlebars.rs#L25
Thanks! XAMPPRocky notifications@github.com wrote: “@MrTact Well you can pass the data by saying variable=variable. If you wanted it to be implicitly passed like that is not supported out of the box, and I don't think I would support this in the library itself. However it would not be hard to build your own newtype wrapper that does the same. I'd recommend creating your own FluentHelper type that accepts a Loader and implement the HelperDef yourself so that it passes the context to the loader. You can look at the implementation in the library as an example.
https://github.com/XAMPPRocky/fluent-templates/blob/master/templates/src/loader/handlebars.rs#L25”
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.
It seems from looking at the Project Fluent syntax guide, placeables should be what I want. I would expect that the fluent helper would allow placeables in the message to get resolved from the data passed to the Handlebars template renderer. However, based on the example from the docs (and my extensive experience in failing to make it work 😁) it looks like under the hood, the helperdef is likely calling lookup_with_args, passing the args that are baked into the helper call, and all variable resolution happens at the Fluent level.
Is this functionality that exists in the library today, and I'm just not grokking how to make it work, or would this be something I have to add?