Skip to main content

Localizations

Struct Localizations 

Source
pub struct Localizations { /* private fields */ }
Expand description

Defines the Locale for its child and the localized resources that the child depends on.

§Defining localized resources

This class is effectively an InheritedWidget. If it’s rebuilt with a new locale or a different list of delegates or any of its delegates’ LocalizationsDelegate::should_reload methods returns true, then widgets that have created a dependency on this widget will be rebuilt after the resources for the new locale have been loaded.

The Localizations widget also instantiates Directionality in order to support the appropriate Directionality::of for its localized child.

Each LocalizationsDelegate in delegates produces a collection of localized resources, an object of type T, one of which is retrieved with Localizations::of.

WidgetsApp creates a Localizations widget so most apps will not need to create one. The widget app’s Localizations delegates can be initialized with WidgetsApp.localizationsDelegates. The MaterialApp class also provides a localizationsDelegates parameter that’s just passed along to the WidgetsApp.

§Loading localized resources

Localized resources are loaded by the list of LocalizationsDelegate delegates. Each delegate is essentially a factory for a collection of localized resources. There are multiple delegates because there are multiple sources for localizations within an app.

If Localizations were to be rebuilt with a new locale then the widget subtree that corresponds to BuildContext context would be rebuilt after the corresponding resources had been loaded.

§Localizations widgets

The delegates are the primary parameter.

Implementations§

Source§

impl Localizations

Source

pub fn new( locale: Locale, delegates: Vec<LocalizationsDelegateRef>, ) -> Localizations

Create a widget from which localizations (like translated strings) can be obtained.

The delegates must include one for WidgetsLocalizations.

Source

pub fn override(app: &mut App, context: BuildContext) -> LocalizationsOverride

Overrides the inherited Locale or LocalizationsDelegates for child.

This factory constructor is used for the (rare) situation where part of an app should be localized for a different locale than the one defined for the device, or if its localizations should come from a different list of LocalizationsDelegates than the list defined by WidgetsApp.localizationsDelegates.

For example you could specify that my_widget was only to be localized for the US English locale:

Localizations::r#override(app, context)
    .locale(Locale::new("en").country_code("US"))
    .child(my_widget)

The locale and delegates parameters default to the Localizations::locale_of and Localizations delegates values from the enclosing Localizations widget. The given delegates are consulted before the inherited ones.

Source

pub fn key(self, key: KeyRef) -> Localizations

Dart Localizations(key:).

Source

pub fn child<K>(self, child: impl IntoWidget<K>) -> Localizations

The widget below this widget in the tree.

Source

pub fn is_application_level(self, is_application_level: bool) -> Localizations

Whether this widget is at the application level.

WidgetsApp sets this to true.

Source

pub fn locale_of(app: &mut App, context: BuildContext) -> Locale

The locale of the Localizations widget for the widget tree that corresponds to BuildContext context.

If no Localizations widget is in scope then the Localizations::locale_of method will throw an exception.

Source

pub fn maybe_locale_of(app: &mut App, context: BuildContext) -> Option<Locale>

The locale of the Localizations widget for the widget tree that corresponds to BuildContext context.

If no Localizations widget is in scope then this function will return null.

Source

pub fn of<T: ?Sized + 'static>( app: &mut App, context: BuildContext, ) -> Option<Rc<T>>

Returns the localized resources object of the given type for the widget tree that corresponds to the given context.

Returns null if no resources object of the given type exists within the given context.

This method is typically used by a static factory method on the type class. For example Flutter’s MaterialLocalizations class looks up Material resources with a method defined like this:

pub fn of(app: &mut App, context: BuildContext) -> Rc<dyn MaterialLocalizations> {
    Localizations::of::<dyn MaterialLocalizations>(app, context).expect(..)
}

Trait Implementations§

Source§

impl Debug for Localizations

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl StatefulWidget for Localizations

Source§

type State = LocalizationsState

The state this widget creates.
Source§

fn debug_fill_properties( &self, _app: &App, properties: &mut DiagnosticPropertiesBuilder, )

Dart’s debugFillProperties: the properties a dump shows for this widget, in Dart’s order. Dart’s Widget base only sets the dense tree style, which the framework does.
Source§

fn key(&self) -> Option<&KeyRef>

Source§

fn create_state(&self) -> LocalizationsState

Creates the mutable state for this widget at a given location in the tree. Read more
Source§

fn debug_check_constructor(&self)

Source§

fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>

Source§

fn type_name(&self) -> &'static str

See Widget::type_name. A stateful form another widget kind becomes (an animated or status transition widget) names the widget it wraps.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<W> IntoWidget<StatefulKind> for W
where W: StatefulWidget,

Source§

fn into_widget(self) -> Rc<dyn Widget>

The widget as a tree node, made where this is called: its creation_location.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>