Comments (4)
@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.
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.
@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.
from fluent-templates.
Related Issues (20)
- Allow arguments to be &'static str HOT 1
- lookup attribute HOT 6
- how to type the returned value for `LOCALES.locales()`? HOT 1
- can you provide an example about integrating actix-web? HOT 4
- Example does not work HOT 8
- Does not work with handlesbar 4.0, only handlesbar 3.0 HOT 2
- Compilation hanging on 1.59 stable HOT 1
- Have Loader::lookup_complete return Option<String> HOT 3
- Better error handling semanitcs HOT 1
- Fallback chain for sibling specificity in languages
- Seems like impl for tera::Function is not brought into the scope HOT 3
- Loader cannot be made into an object HOT 1
- ArcLoader won't work without a top level directory HOT 3
- ArcLoader requires locale directory to exist to even use fallbacks HOT 2
- 0.8 breaking changes are a little bit obscure HOT 1
- Some confusion for me HOT 1
- Return Result instead of panic when looking up? HOT 3
- Feature request: configurable path to lang (in context)
- Compile time i18n macro HOT 2
- In a production environment, panic! may not be a good choice. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fluent-templates.