pub struct LocaleFallbackProvider<P> { /* private fields */ }
Expand description
A data provider wrapper that performs locale fallback. This enables arbitrary locales to be handled at runtime.
§Examples
use icu_locale::langid;
use icu_provider::hello_world::*;
use icu_provider::prelude::*;
use icu_provider_adapters::fallback::LocaleFallbackProvider;
let provider = HelloWorldProvider;
let id = DataIdentifierCow::from_locale(langid!("ja-JP").into());
// The provider does not have data for "ja-JP":
DataProvider::<HelloWorldV1Marker>::load(
&provider,
DataRequest {
id: id.as_borrowed(),
..Default::default()
},
)
.expect_err("No fallback");
// But if we wrap the provider in a fallback provider...
let provider = LocaleFallbackProvider::new(
provider,
icu_locale::LocaleFallbacker::new().static_to_owned(),
);
// ...then we can load "ja-JP" based on "ja" data
let response = DataProvider::<HelloWorldV1Marker>::load(
&provider,
DataRequest {
id: id.as_borrowed(),
..Default::default()
},
)
.expect("successful with vertical fallback");
assert_eq!(response.metadata.locale.unwrap(), langid!("ja").into(),);
assert_eq!(response.payload.get().message, "こんにちは世界",);
Implementations§
source§impl<P> LocaleFallbackProvider<P>
impl<P> LocaleFallbackProvider<P>
sourcepub fn new(provider: P, fallbacker: LocaleFallbacker) -> Self
pub fn new(provider: P, fallbacker: LocaleFallbacker) -> Self
Wraps a provider with a provider performing fallback under the given fallbacker.
If the underlying provider contains deduplicated data, it is important to use the
same fallback data that ExportDriver
used.
§Examples
use icu_locale::langid;
use icu_locale::LocaleFallbacker;
use icu_provider::hello_world::*;
use icu_provider::prelude::*;
use icu_provider_adapters::fallback::LocaleFallbackProvider;
let provider = HelloWorldProvider;
let id = DataIdentifierCow::from_locale(langid!("de-CH").into());
// There is no "de-CH" data in the `HelloWorldProvider`
DataProvider::<HelloWorldV1Marker>::load(
&provider,
DataRequest {
id: id.as_borrowed(),
..Default::default()
},
)
.expect_err("No data for de-CH");
// `HelloWorldProvider` does not contain fallback data,
// but we can construct a fallbacker with `icu_locale`'s
// compiled data.
let provider = LocaleFallbackProvider::new(
provider,
LocaleFallbacker::new().static_to_owned(),
);
// Now we can load the "de-CH" data via fallback to "de".
let german_hello_world: DataResponse<HelloWorldV1Marker> = provider
.load(DataRequest {
id: id.as_borrowed(),
..Default::default()
})
.expect("Loading should succeed");
assert_eq!("Hallo Welt", german_hello_world.payload.get().message);
sourcepub fn into_inner(self) -> P
pub fn into_inner(self) -> P
Returns ownership of the inner provider to the caller.
Trait Implementations§
source§impl<P> AnyProvider for LocaleFallbackProvider<P>where
P: AnyProvider,
impl<P> AnyProvider for LocaleFallbackProvider<P>where
P: AnyProvider,
source§impl<P: Clone> Clone for LocaleFallbackProvider<P>
impl<P: Clone> Clone for LocaleFallbackProvider<P>
source§fn clone(&self) -> LocaleFallbackProvider<P>
fn clone(&self) -> LocaleFallbackProvider<P>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl<P, M> DataProvider<M> for LocaleFallbackProvider<P>where
P: DataProvider<M>,
M: DataMarker,
impl<P, M> DataProvider<M> for LocaleFallbackProvider<P>where
P: DataProvider<M>,
M: DataMarker,
source§impl<P: Debug> Debug for LocaleFallbackProvider<P>
impl<P: Debug> Debug for LocaleFallbackProvider<P>
source§impl<P, M> DryDataProvider<M> for LocaleFallbackProvider<P>where
P: DryDataProvider<M>,
M: DataMarker,
impl<P, M> DryDataProvider<M> for LocaleFallbackProvider<P>where
P: DryDataProvider<M>,
M: DataMarker,
source§impl<P, M> DynamicDataProvider<M> for LocaleFallbackProvider<P>where
P: DynamicDataProvider<M>,
M: DynamicDataMarker,
impl<P, M> DynamicDataProvider<M> for LocaleFallbackProvider<P>where
P: DynamicDataProvider<M>,
M: DynamicDataMarker,
source§impl<P, M> DynamicDryDataProvider<M> for LocaleFallbackProvider<P>where
P: DynamicDryDataProvider<M>,
M: DynamicDataMarker,
impl<P, M> DynamicDryDataProvider<M> for LocaleFallbackProvider<P>where
P: DynamicDryDataProvider<M>,
M: DynamicDataMarker,
Auto Trait Implementations§
impl<P> Freeze for LocaleFallbackProvider<P>where
P: Freeze,
impl<P> RefUnwindSafe for LocaleFallbackProvider<P>where
P: RefUnwindSafe,
impl<P> Send for LocaleFallbackProvider<P>where
P: Send,
impl<P> Sync for LocaleFallbackProvider<P>where
P: Sync,
impl<P> Unpin for LocaleFallbackProvider<P>where
P: Unpin,
impl<P> UnwindSafe for LocaleFallbackProvider<P>where
P: UnwindSafe,
Blanket Implementations§
§impl<P> AsDeserializingBufferProvider for Pwhere
P: BufferProvider + ?Sized,
impl<P> AsDeserializingBufferProvider for Pwhere
P: BufferProvider + ?Sized,
§fn as_deserializing(&self) -> DeserializingBufferProvider<'_, P>
fn as_deserializing(&self) -> DeserializingBufferProvider<'_, P>
Wrap this [BufferProvider
] in a [DeserializingBufferProvider
].
This requires enabling the deserialization Cargo feature for the expected format(s):
deserialize_json
deserialize_postcard_1
deserialize_bincode_1
§impl<P> AsDowncastingAnyProvider for Pwhere
P: AnyProvider + ?Sized,
impl<P> AsDowncastingAnyProvider for Pwhere
P: AnyProvider + ?Sized,
§fn as_downcasting(&self) -> DowncastingAnyProvider<'_, P>
fn as_downcasting(&self) -> DowncastingAnyProvider<'_, P>
Returns an object implementing
DynamicDataProvider<M>
when called on AnyProvider
§impl<P> AsDynamicDataProviderAnyMarkerWrap for Pwhere
P: DynamicDataProvider<AnyMarker> + ?Sized,
impl<P> AsDynamicDataProviderAnyMarkerWrap for Pwhere
P: DynamicDataProvider<AnyMarker> + ?Sized,
§fn as_any_provider(&self) -> DynamicDataProviderAnyMarkerWrap<'_, P>
fn as_any_provider(&self) -> DynamicDataProviderAnyMarkerWrap<'_, P>
Returns an object implementing
AnyProvider
when called on DynamicDataProvider<AnyMarker>
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more