From b4d40354950497cf7c75a29e00998e81be598315 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Thu, 5 Sep 2024 14:28:26 -0700 Subject: [PATCH] Add icu::plurals::supported_locales_of --- components/plurals/src/lib.rs | 4 ++ components/plurals/src/provider.rs | 2 +- .../plurals/src/supported_locales_of.rs | 55 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 components/plurals/src/supported_locales_of.rs diff --git a/components/plurals/src/lib.rs b/components/plurals/src/lib.rs index d432f7a61a1..94203edeb27 100644 --- a/components/plurals/src/lib.rs +++ b/components/plurals/src/lib.rs @@ -79,6 +79,8 @@ extern crate alloc; mod operands; pub mod provider; pub mod rules; +#[cfg(feature = "compiled_data")] +mod supported_locales_of; use core::cmp::{Ord, PartialOrd}; use icu_provider::prelude::*; @@ -87,6 +89,8 @@ use provider::CardinalV1Marker; use provider::ErasedPluralRulesV1Marker; use provider::OrdinalV1Marker; use rules::runtime::test_rule; +#[cfg(feature = "compiled_data")] +pub use supported_locales_of::supported_locales_of; #[cfg(feature = "experimental")] use provider::PluralRangesV1Marker; diff --git a/components/plurals/src/provider.rs b/components/plurals/src/provider.rs index 2a19b681e16..dfb2426ed26 100644 --- a/components/plurals/src/provider.rs +++ b/components/plurals/src/provider.rs @@ -40,7 +40,7 @@ const _: () = { } make_provider!(Baked); - impl_cardinal_v1_marker!(Baked); + impl_cardinal_v1_marker!(Baked, DRY); impl_ordinal_v1_marker!(Baked); #[cfg(feature = "experimental")] impl_plural_ranges_v1_marker!(Baked); diff --git a/components/plurals/src/supported_locales_of.rs b/components/plurals/src/supported_locales_of.rs new file mode 100644 index 00000000000..ab15322b10f --- /dev/null +++ b/components/plurals/src/supported_locales_of.rs @@ -0,0 +1,55 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +use icu_provider::prelude::*; + +/// Returns which locales out of the given list are supported by this crate. +/// +/// # Examples +/// +/// ``` +/// use icu::locale::locale; +/// use icu_provider::DataLocale; +/// +/// let supported_locales = icu::plurals::supported_locales_of([ +/// DataLocale::from(locale!("ru")), // Russian is supported and has its own rules +/// DataLocale::from(locale!("sr-RS")), // sr-RS inherits from sr which has its own rules +/// DataLocale::from(locale!("ja")), // Japanese is supported but inherits from root +/// DataLocale::from(locale!("tlh")), // Klingon is not supported +/// ]).unwrap(); +/// +/// assert_eq!( +/// supported_locales, +/// [ +/// DataLocale::from(locale!("ru")), +/// DataLocale::from(locale!("sr-RS")), +/// DataLocale::from(locale!("ja")), +/// ] +/// ); +/// ``` +pub fn supported_locales_of( + locales: impl IntoIterator, +) -> Result, DataError> { + let mut result = Vec::new(); + for locale in locales { + let mut metadata = DataRequestMetadata::default(); + metadata.silent = true; + let response = crate::provider::Baked.dry_load(DataRequest { + id: DataIdentifierBorrowed::for_locale(&locale), + metadata, + }); + println!("{locale:?} => {:?}", response); + match response { + Ok(DataResponseMetadata { locale: None, .. }) => { + // locale: None means exact match + result.push(locale) + } + Ok(DataResponseMetadata { locale: Some(resolved_locale), .. }) if !resolved_locale.is_default() => { + result.push(locale) + } + _ => () + } + } + Ok(result) +}