Repository: kellpossible/cargo-i18n (language: rust)

Issue:
Add custom functions to the bundles in FluentLanguageLoader?
I use `fluent_language_loader!()` to get a `FluentLanguageLoader`, which is used by the `fl!(...)` macro from the `i18n-embed-fl` crate.  Now I'd like to add a custom function to the bundles, but there seems to be now way to access them.  As the bundles are kept inside private field `language_config` there seems to be no way of doing this?

There is a method `set_use_isolating` that loops over the associated bundles and sets the value. Maybe an `add_function` method could be added in a similar way?  Or maybe a more generic function that does a callback for each associated bundle?

(If this sounds reasonable, I could try to provide implementation.  If adding functions is already possible some other way I'd be happy to hear that.)


Discussion / hints:
@kaj thanks for getting in contact!

>  Now I'd like to add a custom function to the bundles

Can you explain a bit more what you mean by this? Do you want a way to modify what is in the bundles?
Sorry for being unclear.  I meant ["Functions in FTL" as described here](https://projectfluent.org/fluent/guide/functions.html).  I want to be able to write things like this in my `app.ftl` bundles:

```ftl
label = Message with { MYFUNCTION($value) } in it.
```

@kaj Thanks I understand now! Your suggestion to provide a generic function where we pass a callback with mutable access to each bundle seems like a good one, and in hindsight perhaps that's what I should have done with `set_use_isolating` too, we could deprecate it in favour of this. I'm happy to do this, but if you'd like to contribute let me know, it seems like a good first contribution to the project.
Perhaps a new method for `FluentLanguageLoader` something like:

```rust
pub fn with_bundles_mut<F>(&self, f: F) 
    where
        F: Fn(&mut FluentBundle)
{
    ...
}
```

Fix patch:
diff --git a/i18n-embed/src/fluent.rs b/i18n-embed/src/fluent.rs
index 293c807..f9773d5 100644
--- a/i18n-embed/src/fluent.rs
+++ b/i18n-embed/src/fluent.rs
@@ -279,8 +279,19 @@ impl FluentLanguageLoader {
     ///
     /// Default: `true`.
     pub fn set_use_isolating(&self, value: bool) {
+        self.with_bundles_mut(|bundle| bundle.set_use_isolating(value));
+    }
+
+    /// Apply some configuration to each budle in this loader.
+    ///
+    /// **Note:** This function will have no effect if
+    /// [`LanguageLoader::load_languages`] has not been called first.
+    pub fn with_bundles_mut<F>(&self, f: F)
+    where
+        F: Fn(&mut FluentBundle<Arc<FluentResource>, IntlLangMemoizer>),
+    {
         for bundle in self.language_config.write().language_bundles.as_mut_slice() {
-            bundle.bundle.set_use_isolating(value);
+            f(&mut bundle.bundle);
         }
     }
 }


Test patch: