Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add icu::plurals::supported_locales_of #5504

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions components/plurals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion components/plurals/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
55 changes: 55 additions & 0 deletions components/plurals/src/supported_locales_of.rs
Original file line number Diff line number Diff line change
@@ -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<Item = DataLocale>,
) -> Result<Vec<DataLocale>, 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)
}
Loading