diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/fluent-langneg/src/accepted_languages.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/fluent-langneg/src/accepted_languages.rs')
-rw-r--r-- | third_party/rust/fluent-langneg/src/accepted_languages.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/third_party/rust/fluent-langneg/src/accepted_languages.rs b/third_party/rust/fluent-langneg/src/accepted_languages.rs new file mode 100644 index 0000000000..58cf277703 --- /dev/null +++ b/third_party/rust/fluent-langneg/src/accepted_languages.rs @@ -0,0 +1,41 @@ +//! This function parses Accept-Language string into a list of language tags that +//! can be later passed to language negotiation functions. +//! +//! # Example: +//! +//! ``` +//! use fluent_langneg::negotiate_languages; +//! use fluent_langneg::NegotiationStrategy; +//! use fluent_langneg::parse_accepted_languages; +//! use fluent_langneg::convert_vec_str_to_langids_lossy; +//! use unic_langid::LanguageIdentifier; +//! +//! let requested = parse_accepted_languages("de-AT;0.9,de-DE;0.8,de;0.7;en-US;0.5"); +//! let available = convert_vec_str_to_langids_lossy(&["fr", "pl", "de", "en-US"]); +//! let default: LanguageIdentifier = "en-US".parse().expect("Failed to parse a langid."); +//! +//! let supported = negotiate_languages( +//! &requested, +//! &available, +//! Some(&default), +//! NegotiationStrategy::Filtering +//! ); +//! +//! let expected = convert_vec_str_to_langids_lossy(&["de", "en-US"]); +//! assert_eq!(supported, +//! expected.iter().map(|t| t.as_ref()).collect::<Vec<&LanguageIdentifier>>()); +//! ``` +//! +//! This function ignores the weights associated with the locales, since Fluent Locale +//! language negotiation only uses the order of locales, not the weights. +//! + +use unic_langid::LanguageIdentifier; + +pub fn parse(s: &str) -> Vec<LanguageIdentifier> { + s.split(',') + .map(|t| t.trim().split(';').nth(0).unwrap()) + .filter(|t| !t.is_empty()) + .filter_map(|t| t.parse().ok()) + .collect() +} |