Skip to content

Commit

Permalink
Add FluentBundle::add_builtins()
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiretza committed Apr 26, 2024
1 parent c2bde7a commit a3a63c0
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions fluent-bundle/src/bundle.rs
Expand Up @@ -547,6 +547,61 @@ impl<R, M> FluentBundle<R, M> {
}),
}
}

/// Adds the builtin functions described in the [FTL syntax guide] to the bundle, making them
/// available in messages.
///
/// # Examples
///
/// ```
/// use fluent_bundle::{FluentArgs, FluentBundle, FluentResource, FluentValue};
/// use unic_langid::langid;
///
/// let ftl_string = String::from(r#"rank = { NUMBER($n, type: "ordinal") ->
/// [1] first
/// [2] second
/// [3] third
/// [one] {$n}st
/// [two] {$n}nd
/// [few] {$n}rd
/// *[other] {$n}th
/// }"#);
/// let resource = FluentResource::try_new(ftl_string)
/// .expect("Could not parse an FTL string.");
/// let langid_en = langid!("en-US");
/// let mut bundle = FluentBundle::new(vec![langid_en]);
/// bundle.add_resource(&resource)
/// .expect("Failed to add FTL resources to the bundle.");
///
/// // Register the builtin functions (including NUMBER())
/// bundle.add_builtins().expect("Failed to add builtins to the bundle.");
///
/// let msg = bundle.get_message("rank").expect("Message doesn't exist.");
/// let mut errors = vec![];
/// let pattern = msg.value().expect("Message has no value.");
///
/// let mut args = FluentArgs::new();
///
/// args.set("n", 5);
/// let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);
/// assert_eq!(&value, "\u{2068}5\u{2069}th");
///
/// args.set("n", 12);
/// let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);
/// assert_eq!(&value, "\u{2068}12\u{2069}th");
///
/// args.set("n", 22);
/// let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);
/// assert_eq!(&value, "\u{2068}22\u{2069}nd");
/// ```
///
/// [FTL syntax guide]: https://projectfluent.org/fluent/guide/functions.html
pub fn add_builtins(&mut self) -> Result<(), FluentError> {
self.add_function("NUMBER", crate::builtins::NUMBER)?;
// TODO: DATETIME()

Ok(())
}
}

impl<R> Default for FluentBundle<R, IntlLangMemoizer> {
Expand Down

0 comments on commit a3a63c0

Please sign in to comment.