7
7
use std:: borrow:: Borrow ;
8
8
use std:: borrow:: Cow ;
9
9
use std:: collections:: hash_map:: { Entry as HashEntry , HashMap } ;
10
+ use std:: convert:: TryFrom ;
11
+ use std:: default:: Default ;
10
12
11
13
use fluent_locale:: { negotiate_languages, NegotiationStrategy } ;
12
14
use fluent_syntax:: ast;
13
15
use intl_pluralrules:: { IntlPluralRules , PluralRuleType } ;
16
+ use unic_langid:: LanguageIdentifier ;
14
17
15
18
use crate :: entry:: Entry ;
16
19
use crate :: entry:: GetEntry ;
@@ -33,12 +36,15 @@ pub struct Message<'m> {
33
36
/// ```
34
37
/// use fluent_bundle::{FluentBundle, FluentResource, FluentValue};
35
38
/// use std::collections::HashMap;
39
+ /// use std::convert::TryFrom;
40
+ /// use unic_langid::LanguageIdentifier;
36
41
///
37
42
/// let ftl_string = String::from("intro = Welcome, { $name }.");
38
43
/// let resource = FluentResource::try_new(ftl_string)
39
44
/// .expect("Could not parse an FTL string.");
40
45
///
41
- /// let mut bundle = FluentBundle::new(&["en-US"]);
46
+ /// let langid_en = LanguageIdentifier::try_from("en-US").expect("Parsing failed.");
47
+ /// let mut bundle = FluentBundle::new(&[langid_en]);
42
48
/// bundle.add_resource(&resource)
43
49
/// .expect("Failed to add FTL resources to the bundle.");
44
50
///
@@ -86,7 +92,7 @@ pub struct Message<'m> {
86
92
/// [`add_resource`]: ./struct.FluentBundle.html#method.add_resource
87
93
/// [`Cow<str>`]: http://doc.rust-lang.org/std/borrow/enum.Cow.html
88
94
pub struct FluentBundle < R > {
89
- pub locales : Vec < String > ,
95
+ pub locales : Vec < LanguageIdentifier > ,
90
96
pub ( crate ) resources : Vec < R > ,
91
97
pub ( crate ) entries : HashMap < String , Entry > ,
92
98
pub ( crate ) plural_rules : IntlPluralRules ,
@@ -103,27 +109,33 @@ impl<R> FluentBundle<R> {
103
109
/// ```
104
110
/// use fluent_bundle::FluentBundle;
105
111
/// use fluent_bundle::FluentResource;
112
+ /// use std::convert::TryFrom;
113
+ /// use unic_langid::LanguageIdentifier;
106
114
///
107
- /// let mut bundle: FluentBundle<FluentResource> = FluentBundle::new(&["en-US"]);
115
+ /// let langid_en = LanguageIdentifier::try_from("en-US").expect("Parsing failed.");
116
+ /// let mut bundle: FluentBundle<FluentResource> = FluentBundle::new(&[langid_en]);
108
117
/// ```
109
118
///
110
119
/// # Errors
111
120
///
112
121
/// This will panic if no formatters can be found for the locales.
113
- pub fn new < S : ToString > ( locales : & [ S ] ) -> Self {
122
+ pub fn new < ' a , L : ' a + Into < LanguageIdentifier > + PartialEq + Clone > (
123
+ locales : impl IntoIterator < Item = & ' a L > ,
124
+ ) -> Self {
114
125
let locales = locales
115
- . iter ( )
116
- . map ( std :: string :: ToString :: to_string )
126
+ . into_iter ( )
127
+ . map ( |s| s . clone ( ) . into ( ) )
117
128
. collect :: < Vec < _ > > ( ) ;
129
+ let default_langid = LanguageIdentifier :: try_from ( "en" ) . expect ( "Parsing failed." ) ;
118
130
let pr_locale = negotiate_languages (
119
131
& locales,
120
- IntlPluralRules :: get_locales ( PluralRuleType :: CARDINAL ) ,
121
- Some ( "en" ) ,
122
- & NegotiationStrategy :: Lookup ,
132
+ & IntlPluralRules :: get_locales ( PluralRuleType :: CARDINAL ) ,
133
+ Some ( & default_langid ) ,
134
+ NegotiationStrategy :: Lookup ,
123
135
) [ 0 ]
124
- . to_owned ( ) ;
136
+ . clone ( ) ;
125
137
126
- let pr = IntlPluralRules :: create ( & pr_locale, PluralRuleType :: CARDINAL )
138
+ let pr = IntlPluralRules :: create ( pr_locale, PluralRuleType :: CARDINAL )
127
139
. expect ( "Failed to initialize PluralRules." ) ;
128
140
FluentBundle {
129
141
locales,
@@ -149,14 +161,17 @@ impl<R> FluentBundle<R> {
149
161
///
150
162
/// ```
151
163
/// use fluent_bundle::{FluentBundle, FluentResource};
164
+ /// use std::convert::TryFrom;
165
+ /// use unic_langid::LanguageIdentifier;
152
166
///
153
167
/// let ftl_string = String::from("
154
168
/// hello = Hi!
155
169
/// goodbye = Bye!
156
170
/// ");
157
171
/// let resource = FluentResource::try_new(ftl_string)
158
172
/// .expect("Could not parse an FTL string.");
159
- /// let mut bundle = FluentBundle::new(&["en-US"]);
173
+ /// let langid_en = LanguageIdentifier::try_from("en-US").expect("Parsing failed.");
174
+ /// let mut bundle = FluentBundle::new(&[langid_en]);
160
175
/// bundle.add_resource(resource)
161
176
/// .expect("Failed to add FTL resources to the bundle.");
162
177
/// assert_eq!(true, bundle.has_message("hello"));
@@ -230,11 +245,14 @@ impl<R> FluentBundle<R> {
230
245
///
231
246
/// ```
232
247
/// use fluent_bundle::{FluentBundle, FluentResource};
248
+ /// use std::convert::TryFrom;
249
+ /// use unic_langid::LanguageIdentifier;
233
250
///
234
251
/// let ftl_string = String::from("hello = Hi!");
235
252
/// let resource = FluentResource::try_new(ftl_string)
236
253
/// .expect("Failed to parse an FTL string.");
237
- /// let mut bundle = FluentBundle::new(&["en-US"]);
254
+ /// let langid_en = LanguageIdentifier::try_from("en-US").expect("Parsing failed.");
255
+ /// let mut bundle = FluentBundle::new(&[langid_en]);
238
256
/// bundle.add_resource(&resource)
239
257
/// .expect("Failed to add FTL resources to the bundle.");
240
258
/// assert_eq!(true, bundle.has_message("hello"));
@@ -262,7 +280,7 @@ impl<R> FluentBundle<R> {
262
280
for attr in message. attributes . iter ( ) {
263
281
attributes. insert ( attr. id . name , & attr. value ) ;
264
282
}
265
- return Some ( Message { value, attributes } ) ;
283
+ Some ( Message { value, attributes } )
266
284
}
267
285
268
286
pub fn format_pattern < ' bundle > (
@@ -295,11 +313,14 @@ impl<R> FluentBundle<R> {
295
313
///
296
314
/// ```
297
315
/// use fluent_bundle::{FluentBundle, FluentResource, FluentValue};
316
+ /// use std::convert::TryFrom;
317
+ /// use unic_langid::LanguageIdentifier;
298
318
///
299
319
/// let ftl_string = String::from("length = { STRLEN(\"12345\") }");
300
320
/// let resource = FluentResource::try_new(ftl_string)
301
321
/// .expect("Could not parse an FTL string.");
302
- /// let mut bundle = FluentBundle::new(&["en-US"]);
322
+ /// let langid_en = LanguageIdentifier::try_from("en-US").expect("Parsing failed.");
323
+ /// let mut bundle = FluentBundle::new(&[langid_en]);
303
324
/// bundle.add_resource(&resource)
304
325
/// .expect("Failed to add FTL resources to the bundle.");
305
326
///
@@ -335,3 +356,20 @@ impl<R> FluentBundle<R> {
335
356
}
336
357
}
337
358
}
359
+
360
+ impl < R > Default for FluentBundle < R > {
361
+ fn default ( ) -> Self {
362
+ let pr_langid = LanguageIdentifier :: try_from ( "en" ) . expect ( "Parsing failed." ) ;
363
+ let langid = LanguageIdentifier :: new ( ) ;
364
+
365
+ let pr = IntlPluralRules :: create ( pr_langid, PluralRuleType :: CARDINAL )
366
+ . expect ( "Failed to initialize PluralRules." ) ;
367
+ FluentBundle {
368
+ plural_rules : pr,
369
+ locales : vec ! [ langid] ,
370
+ resources : vec ! [ ] ,
371
+ entries : Default :: default ( ) ,
372
+ use_isolating : false ,
373
+ }
374
+ }
375
+ }
0 commit comments