diff --git a/examples/localized.rs b/examples/localized.rs index 95fd53a23..6d3fbbf17 100644 --- a/examples/localized.rs +++ b/examples/localized.rs @@ -46,7 +46,8 @@ fn localized() -> impl MakeWidget { *counter.lock() -= 1; } }))) - .into_columns(); + .into_columns() + .localized(dynamic_locale.map_each(LanguageChoices::to_locale)); element_in_default_locale .and(elements_in_specific_locale) diff --git a/src/context.rs b/src/context.rs index 26c617996..88da6821c 100644 --- a/src/context.rs +++ b/src/context.rs @@ -20,7 +20,7 @@ use crate::styles::components::{ }; use crate::styles::{ComponentDefinition, FontFamilyList, Styles, Theme, ThemePair}; use crate::tree::Tree; -use crate::value::{IntoValue, Source, Value}; +use crate::value::{Dynamic, IntoValue, Source, Value}; use crate::widget::{EventHandling, MountedWidget, RootBehavior, WidgetId, WidgetInstance}; use crate::window::{ CursorState, DeviceId, KeyEvent, PlatformWindow, ThemeMode, WidgetCursorState, @@ -933,7 +933,7 @@ pub struct WidgetContext<'context> { font_state: &'context mut FontState, effective_styles: Styles, cache: WidgetCacheKey, - locale: LanguageIdentifier, + locale: Value, translations: &'context mut TranslationState, } @@ -978,7 +978,7 @@ impl<'context> WidgetContext<'context> { font_state, theme: Cow::Borrowed(theme), window, - locale: translations.fallback_locale.clone(), + locale: Dynamic::new(translations.fallback_locale.clone()).into_value(), translations }; @@ -989,8 +989,10 @@ impl<'context> WidgetContext<'context> { context.cache.theme_mode = mode.get_tracking_redraw(&context); } if let Some(locale) = overridden_locale { - context.locale = locale.get_tracking_redraw(&context); + context.locale = locale } + context.locale.invalidate_when_changed(&context); + context.locale.redraw_when_changed(&context); context } @@ -1035,7 +1037,7 @@ impl<'context> WidgetContext<'context> { }; let locale = current_node.overridden_locale(); let locale = if let Some(locale) = locale { - locale.get_tracking_redraw(self) + locale } else { self.locale.clone() }; @@ -1282,7 +1284,7 @@ impl<'context> WidgetContext<'context> { /// Returns the locale for this widget. #[must_use] - pub fn locale(&self) -> &LanguageIdentifier { + pub fn locale(&self) -> &Value { &self.locale } @@ -1290,7 +1292,7 @@ impl<'context> WidgetContext<'context> { pub fn translation( &self ) -> &FluentBundle { - if let Some(bundle) = self.translations.loaded_translations.get(&self.locale) { + if let Some(bundle) = self.translations.loaded_translations.get(&self.locale.get()) { bundle } else { self.translations.loaded_translations.get(&self.translations.fallback_locale).unwrap() diff --git a/src/localization.rs b/src/localization.rs index ee2d4f426..18310678c 100644 --- a/src/localization.rs +++ b/src/localization.rs @@ -6,7 +6,7 @@ use fluent_bundle::{FluentArgs, FluentBundle, FluentResource, FluentValue}; use unic_langid::LanguageIdentifier; use cushy::widgets::Label; use crate::context::WidgetContext; -use crate::value::{Dynamic, IntoValue}; +use crate::value::{Dynamic, Generation, IntoValue}; use crate::widgets::label::{DynamicDisplay}; pub struct Localize<'args> { @@ -53,8 +53,12 @@ impl Localize<'static> { } impl DynamicDisplay for Localize<'static> { - fn fmt(&self, context: &WidgetContext<'_>, f: &mut Formatter<'_>) -> std::fmt::Result { - let locale = context.locale(); + fn generation(&self, context: &WidgetContext<'_>) -> Option { + context.locale().generation() + } + + fn fmt(&self, context: &WidgetContext<'_>, f: &mut Formatter<'_>) -> fmt::Result { + let locale = context.locale().get(); println!("{:?}", locale); let bundle = context.translation();