Skip to content

Commit

Permalink
Use HelloWorldProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Aug 24, 2023
1 parent 0c95725 commit f28bb4f
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions docs/tutorials/data_provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,10 @@ However, for environments that require this behavior, such as ECMA-402, the data

```rust
use icu_provider::prelude::*;
use icu_provider::hello_world::*;
use icu::decimal::FixedDecimalFormatter;
use icu::decimal::provider::DecimalSymbolsV1Marker;
use icu_provider_adapters::fallback::LocaleFallbackProvider;
use icu_provider_adapters::fallback::LocaleFallbacker;
use icu::locid::locale;
use std::sync::RwLock;

Expand All @@ -336,32 +338,35 @@ where
{
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 `DecimalSymbolsV1Marker::KEY` will be the one
// Whichever locale gets loaded for `HelloWorldV1Marker::KEY` will be the one
// we consider the "resolved locale".
if key == DecimalSymbolsV1Marker::KEY {
if key == HelloWorldV1Marker::KEY {
let mut w = self.resolved_locale.write().unwrap();
*w = any_res.metadata.locale.clone();
}
Ok(any_res)
}
}

// Set up a HelloWorldProvider with fallback
let provider = ResolvedLocaleProvider {
inner: icu_testdata::any(),
inner: LocaleFallbackProvider::new_with_fallbacker(
HelloWorldProvider.as_any_provider(),
LocaleFallbacker::try_new_unstable(&icu_testdata::unstable()).unwrap(),
),
resolved_locale: None.into(),
};

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

// ...which loads data from sr-Latn.
// ...which loads data from ru.
assert_eq!(
*provider.resolved_locale.read().unwrap(),
Some(locale!("sr-Latn").into()),
Some(locale!("ru").into()),
);
```

0 comments on commit f28bb4f

Please sign in to comment.