@@ -18,15 +18,29 @@ Usage
1818-----
1919
2020``` rust
21- use fluent_locale :: negotiate :: NegotiationStrategy ;
21+ use std :: convert :: TryFrom
22+
2223use fluent_locale :: negotiate_languages;
24+ use fluent_locale :: NegotiationStrategy ;
25+ use fluent_locale :: convert_vec_str_to_langids;
26+ use unic_langid :: LanguageIdentifier
27+
28+ // Since langid parsing from string is fallible, we'll use a helper
29+ // function which strips any langids that failed to parse.
30+ let requested = convert_vec_str_to_langids(& ["de- DE ", "fr- FR ", "en- US "]);
31+ let available = convert_vec_str_to_langids (& [" it" , " fr" , " de-AT" , " fr-CA" , " en-US" ]);
32+ let default = LanguageIdentifier :: try_from (" en-US" ). expect (" Parsing langid failed." );
2333
2434let supported = negotiate_languages (
25- & [ " de-DE " , " fr-FR " , " en-US " ] ,
26- & [ " de-DE " , " de-AT " , " fr-CA " , " fr " , " en-GB " , " en " , " en-US " , " it " ] ,
27- Some (" en-US " ),
28- & NegotiationStrategy :: Filtering
35+ & requested ,
36+ & available ,
37+ Some (& default ),
38+ NegotiationStrategy :: Filtering
2939);
40+
41+ let expected = convert_vec_str_to_langids (& [" de-AT" , " fr" , " fr-CA" , " en-US" ]);
42+ assert_eq! (supported ,
43+ expected . iter (). map (| t | t . as_ref ()). collect :: <Vec <& LanguageIdentifier >>());
3044```
3145
3246See [ docs.rs] [ ] for more examples.
@@ -39,6 +53,11 @@ Status
3953The implementation is in early stage, but is complete according to fluent-locale
4054corpus of tests, which means that it parses, serializes and negotiates as expected.
4155
56+ The negotiation methods can operate on lists of ` LanguageIdentifier ` or ` Locale ` .
57+
58+ The ergonomics of Rust API can be improved, since the fallible nature of language identifier
59+ parsing makes operating on lists of them tedious.
60+
4261The remaining work is on the path to 1.0 is to gain in-field experience of using it,
4362add more tests and ensure that bad input is correctly handled.
4463
@@ -54,17 +73,9 @@ For most locale management and negotiation needs, the Unicode Locale Identifier
5473but in some case, like HTTP Accepted Headers, you may need the complete BCP47 Language Tag implementation which
5574this crate does not provide.
5675
57- Parsed locale identifiers are stored as ` Locale ` objects compatible with
58- ECMA402's [ Intl.Locale] [ ] and allow for operations on locale identifier subtags and
59- unicode extension keys as defined by [ RFC6067] [ ] and Unicode [ UTS35] [ ]
60-
6176Language negotiation algorithms are custom Project Fluent solutions,
6277based on [ RFC4647] [ ] .
6378
64- The current API only allows for operations on basic language subtags (language, script, region, variants)
65- and unicode extension keys. Other subtags will be parsed and serialized, but there is no
66- API access to them when operating on the ` Locale ` object.
67-
6879The language negotiation strategies aim to replicate the best-effort matches with
6980the most limited amount of data. The algorithm returns reasonable
7081results without any database, but the results can be improved with either limited
@@ -77,9 +88,10 @@ tradeoffs.
7788[ BCP47 ] : https://tools.ietf.org/html/bcp47
7889[ Intl.Locale ] : https://github.com/tc39/proposal-intl-locale
7990[ RFC6067 ] : https://www.ietf.org/rfc/rfc6067.txt
80- [ UTS35 ] : http://www.unicode.org/reports/tr35/#Locale_Extension_Key_and_Type_Data
91+ [ UTS 35 ] : http://www.unicode.org/reports/tr35/#Locale_Extension_Key_and_Type_Data
8192[ RFC4647 ] : https://tools.ietf.org/html/rfc4647
8293[ CLDR likely-subtags ] : http://www.unicode.org/cldr/charts/latest/supplemental/likely_subtags.html
94+ [ Unicode Locale Identifier ] : (http://unicode.org/reports/tr35/#Identifiers)
8395
8496Alternatives
8597------------
0 commit comments