@@ -3,9 +3,17 @@ use std::iter::FromIterator;
3
3
4
4
use crate :: types:: FluentValue ;
5
5
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.
9
17
///
10
18
/// # Example
11
19
///
@@ -48,14 +56,17 @@ use crate::types::FluentValue;
48
56
pub struct FluentArgs < ' args > ( Vec < ( Cow < ' args , str > , FluentValue < ' args > ) > ) ;
49
57
50
58
impl < ' args > FluentArgs < ' args > {
59
+ /// Creates a new empty argument map.
51
60
pub fn new ( ) -> Self {
52
61
Self :: default ( )
53
62
}
54
63
64
+ /// Pre-allocates capacity for arguments.
55
65
pub fn with_capacity ( capacity : usize ) -> Self {
56
66
Self ( Vec :: with_capacity ( capacity) )
57
67
}
58
68
69
+ /// Gets the [`FluentValue`] at the `key` if it exists.
59
70
pub fn get < K > ( & self , key : K ) -> Option < & FluentValue < ' args > >
60
71
where
61
72
K : Into < Cow < ' args , str > > ,
@@ -68,6 +79,7 @@ impl<'args> FluentArgs<'args> {
68
79
}
69
80
}
70
81
82
+ /// Sets the key value pair.
71
83
pub fn set < K , V > ( & mut self , key : K , value : V )
72
84
where
73
85
K : Into < Cow < ' args , str > > ,
@@ -80,6 +92,7 @@ impl<'args> FluentArgs<'args> {
80
92
} ;
81
93
}
82
94
95
+ /// Iterate over a tuple of the key an [`FluentValue`].
83
96
pub fn iter ( & self ) -> impl Iterator < Item = ( & str , & FluentValue ) > {
84
97
self . 0 . iter ( ) . map ( |( k, v) | ( k. as_ref ( ) , v) )
85
98
}
0 commit comments