Skip to content

Commit 41223f9

Browse files
committed
Add some docs to FluentBundle::args
1 parent 2e8457e commit 41223f9

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

fluent-bundle/src/args.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ use std::iter::FromIterator;
33

44
use crate::types::FluentValue;
55

6-
/// A map of arguments passed from the code to
7-
/// the localization to be used for message
8-
/// formatting.
6+
/// Fluent messages can use arguments in order to programmatically add values to a
7+
/// translated string. For instance, in a localized application you may wish to display
8+
/// a user's email count. This could be done with the following message.
9+
///
10+
/// `msg-key = Hello, { $user }. You have { $emailCount } messages.`
11+
///
12+
/// Here `$user` and `$emailCount` are the arguments, which can be filled with values.
13+
///
14+
/// The [`FluentArgs`] struct is the map from the argument name (for example `$user`) to
15+
/// the argument value (for example "John".) The logic to apply these to write these
16+
/// to messages is elsewhere, this struct just stores the value.
917
///
1018
/// # Example
1119
///
@@ -48,14 +56,17 @@ use crate::types::FluentValue;
4856
pub struct FluentArgs<'args>(Vec<(Cow<'args, str>, FluentValue<'args>)>);
4957

5058
impl<'args> FluentArgs<'args> {
59+
/// Creates a new empty argument map.
5160
pub fn new() -> Self {
5261
Self::default()
5362
}
5463

64+
/// Pre-allocates capacity for arguments.
5565
pub fn with_capacity(capacity: usize) -> Self {
5666
Self(Vec::with_capacity(capacity))
5767
}
5868

69+
/// Gets the [`FluentValue`] at the `key` if it exists.
5970
pub fn get<K>(&self, key: K) -> Option<&FluentValue<'args>>
6071
where
6172
K: Into<Cow<'args, str>>,
@@ -68,6 +79,7 @@ impl<'args> FluentArgs<'args> {
6879
}
6980
}
7081

82+
/// Sets the key value pair.
7183
pub fn set<K, V>(&mut self, key: K, value: V)
7284
where
7385
K: Into<Cow<'args, str>>,
@@ -80,6 +92,7 @@ impl<'args> FluentArgs<'args> {
8092
};
8193
}
8294

95+
/// Iterate over a tuple of the key an [`FluentValue`].
8396
pub fn iter(&self) -> impl Iterator<Item = (&str, &FluentValue)> {
8497
self.0.iter().map(|(k, v)| (k.as_ref(), v))
8598
}

0 commit comments

Comments
 (0)