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

Question: Is there a way to pass through a variable to be resolved by Handlebars? #12

Closed
MrTact opened this issue Jun 28, 2020 · 4 comments

Comments

@MrTact
Copy link
Contributor

MrTact commented Jun 28, 2020

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?

@XAMPPRocky
Copy link
Owner

@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

@MrTact
Copy link
Contributor Author

MrTact commented Jun 29, 2020

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.

@XAMPPRocky
Copy link
Owner

@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

@MrTact
Copy link
Contributor Author

MrTact commented Jun 30, 2020 via email

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

No branches or pull requests

2 participants