Closed kotovalexarian closed 3 years ago
I've solved the problem with std::sync::Arc
, so I close the issue. Sorry for my poor experience with Rust.
pub fn translate(i18n: std::sync::Arc<I18n>) -> Box<dyn HelperDef> {
Box::new(move |
helper: &Helper,
_: &Handlebars,
_: &Context,
_: &mut RenderContext,
output: &mut dyn Output,
| -> HelperResult {
let locale = helper.param(0)
.ok_or(RenderError::new("expected locale param"))?
.value().as_str()
.ok_or(RenderError::new("expected locale param"))?;
let key = helper.param(1)
.ok_or(RenderError::new("expected key param"))?
.value().as_str()
.ok_or(RenderError::new("expected key param"))?;
let i18n = i18n.clone();
let l10n = i18n.l10n(locale).ok()
.ok_or(RenderError::new("unknown locale"))?;
let translated = l10n.translate(key).ok()
.ok_or(RenderError::new("translation error"))?;
output.write(&translated)?;
Ok(())
})
}
Hello. I'm trying to use Fluent in my Rocket web application. I need to access bundle in multiple places:
I can use bundle in Rocket threads with
fluent_bundle::concurrent::FluentBundle
. However, I can't use it in template helper because it doesn't implementClone
trait. Also I'm not sure how ofter will clone be performed. Maybe that's because of poorly designed architecture of Rocket and/or Handlebars.How can I solve this?
UPD. This is what I try to do: