1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
//! [Fluent](https://projectfluent.org/) helper for [Handlebars](https://docs.rs/handlebars). //! //! This crate provides a Handlebars helper that can load Fluent strings. //! //! //! # Setting up the fluent helper with handlebars //! //! The easiest way to use this is to use the [`simple_loader!()`] macro: //! //! ```rust //! use handlebars_fluent::*; //! use handlebars::*; //! use serde_json::*; //! //! simple_loader!(create_loader, "./locales/", "en-US"); //! //! fn init(handlebars: &mut Handlebars) { //! let loader = create_loader(); //! let helper = FluentHelper::new(loader); //! handlebars.register_helper("fluent", Box::new(helper)); //! } //! //! fn render_page(handlebars: &Handlebars) -> String { //! let data = json!({"lang": "zh-CN"}); //! handlebars.render_template("{{fluent \"foo-bar\"}} baz", &data).unwrap() //! } //! ``` //! //! You should have a `locales/` folder somewhere with one folder per language code, //! containing all of your FTL files. See the [`simple_loader!()`] macro for more options. //! //! Make sure the [`handlebars::Context`] has a toplevel "lang" field when rendering. //! //! //! # Using the fluent helper in your templates //! //! The main helper provided is the `{{fluent}}` helper. If you have the following Fluent //! file: //! //! ```fluent //! foo-bar = "foo bar" //! placeholder = this has a placeholder { $variable } //! ``` //! //! You can include the strings in your template with //! //! ```hbs //! {{fluent "foo-bar"}} <!-- will render "foo bar" --> //! {{fluent "placeholder" variable="baz"}} <!-- will render "this has a placeholder baz" --> //!``` //! //! You may also use the `{{fluentparam}}` helper to specify [variables], especially if you need //! them to be multiline, like so: //! //! ```hbs //! {{#fluent "placeholder"}} //! {{#fluentparam "variable"}} //! first line //! second line //! {{/fluentparam}} //! {{/fluent}} //! ``` //! //! Multiple `{{fluentparam}}`s may be specified //! //! [variables]: https://projectfluent.org/fluent/guide/variables.html //! [`simple_loader!()`]: ./macro.simple_loader.html #[doc(hidden)] pub extern crate lazy_static; #[doc(hidden)] pub extern crate fluent_bundle; pub use helper::FluentHelper; pub use loader::{Loader, SimpleLoader}; mod helper; pub mod loader;