Giter Club home page Giter Club logo

Comments (4)

XAMPPRocky avatar XAMPPRocky commented on May 20, 2024

@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

from fluent-templates.

MrTact avatar MrTact commented on May 20, 2024

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.

from fluent-templates.

XAMPPRocky avatar XAMPPRocky commented on May 20, 2024

@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

from fluent-templates.

MrTact avatar MrTact commented on May 20, 2024

from fluent-templates.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.