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 an example for accessing the resolved locale #3928

Merged
merged 13 commits into from
Aug 29, 2023
59 changes: 59 additions & 0 deletions docs/tutorials/data_provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,62 @@ let formatter = FixedDecimalFormatter::try_new_with_any_provider(

assert_eq!(formatter.format_to_string(&100007i64.into()), "100🐮007");
```

## Accessing the Resolved Locale

ICU4X objects to not store their "resolved locale" because that is not a well-defined concept. Components can load data from many sources, and fallbacks to parent locales or root does not necesarily mean that a locale is not supported.
sffc marked this conversation as resolved.
Show resolved Hide resolved

However, for environments that require this behavior, such as ECMA-402, the data provider can be instrumented to access the resolved locale from `DataResponseMetadata`, as shown in the following example.

```rust
use icu_provider::prelude::*;
use icu_provider::hello_world::*;
use icu::decimal::FixedDecimalFormatter;
sffc marked this conversation as resolved.
Show resolved Hide resolved
use icu_provider_adapters::fallback::LocaleFallbackProvider;
use icu_provider_adapters::fallback::LocaleFallbacker;
use icu::locid::locale;
use std::sync::RwLock;

pub struct ResolvedLocaleProvider<P> {
inner: P,
resolved_locale: RwLock<Option<DataLocale>>,
}

impl<P> AnyProvider for ResolvedLocaleProvider<P>
where
P: AnyProvider
{
fn load_any(&self, key: DataKey, req: DataRequest) -> Result<AnyResponse, DataError> {
let mut any_res = self.inner.load_any(key, req)?;
// Whichever locale gets loaded for `HelloWorldV1Marker::KEY` will be the one
// we consider the "resolved locale".
if key == HelloWorldV1Marker::KEY {
robertbastian marked this conversation as resolved.
Show resolved Hide resolved
let mut w = self.resolved_locale.write().unwrap();
sffc marked this conversation as resolved.
Show resolved Hide resolved
*w = any_res.metadata.locale.clone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this could be take instead of clone? The FixedDecimalFormatter should not need the locale.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really prefer if this would take. This would demonstrate that the locale is not actually used later, and it's only there to allow writing this specific wrapper.

}
Ok(any_res)
}
}

// Set up a HelloWorldProvider with fallback
let provider = ResolvedLocaleProvider {
inner: LocaleFallbackProvider::new_with_fallbacker(
HelloWorldProvider.as_any_provider(),
LocaleFallbacker::try_new_unstable(&icu_testdata::unstable()).unwrap(),
),
resolved_locale: None.into(),
sffc marked this conversation as resolved.
Show resolved Hide resolved
};

// Request data for ru-RU...
HelloWorldFormatter::try_new_with_any_provider(
&provider,
&locale!("ru-RU").into(),
)
.unwrap();

// ...which loads data from ru.
assert_eq!(
*provider.resolved_locale.read().unwrap(),
sffc marked this conversation as resolved.
Show resolved Hide resolved
Some(locale!("ru").into()),
);
```