From 7497c10f4c54821ead8e47ca4447d70fe59fc7a9 Mon Sep 17 00:00:00 2001 From: Dominic Clifton Date: Tue, 14 Jan 2025 14:55:00 +0100 Subject: [PATCH] Add support for localized placeholders. --- .../localizations/en-US/validation-localized.ftl | 1 + .../localizations/es-ES/validation-localized.ftl | 1 + examples/validation-localized.rs | 2 ++ examples/validation.rs | 2 ++ src/widgets/input.rs | 12 ++++++++---- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/examples/assets/localizations/en-US/validation-localized.ftl b/examples/assets/localizations/en-US/validation-localized.ftl index ccfc94a25..ac42162eb 100644 --- a/examples/assets/localizations/en-US/validation-localized.ftl +++ b/examples/assets/localizations/en-US/validation-localized.ftl @@ -8,6 +8,7 @@ language-es-es = Spanish (Spain) # form-example-hinted-label = Hinted form-example-not-hinted-label = Not hinted +form-example-input-placeholder = Enter some text # Specific validation reason form-input-invalid-non-whitespace-required = This field must have at least one non-whitespace character diff --git a/examples/assets/localizations/es-ES/validation-localized.ftl b/examples/assets/localizations/es-ES/validation-localized.ftl index ce7845d2a..f05aa5396 100644 --- a/examples/assets/localizations/es-ES/validation-localized.ftl +++ b/examples/assets/localizations/es-ES/validation-localized.ftl @@ -8,6 +8,7 @@ language-es-es = Español (España) # form-example-hinted-label = Insinuado form-example-not-hinted-label = No insinuado +form-example-input-placeholder = Introduzca un texto # Razón de validación específica form-input-invalid-non-whitespace-required = Este campo debe tener al menos un carácter que no sea un espacio en blanco diff --git a/examples/validation-localized.rs b/examples/validation-localized.rs index d43663fbd..f24874000 100644 --- a/examples/validation-localized.rs +++ b/examples/validation-localized.rs @@ -21,12 +21,14 @@ fn form() -> impl MakeWidget { localize!("form-example-hinted-label") .and( text.to_input() + .placeholder(localize!("form-example-input-placeholder")) .validation(validations.validate(&text, validate_input)) .hint(localize!("form-hint-field-required")), ) .and(localize!("form-example-not-hinted-label")) .and( text.to_input() + .placeholder(localize!("form-example-input-placeholder")) .validation(validations.validate(&text, validate_input)), ) .and( diff --git a/examples/validation.rs b/examples/validation.rs index 58940fa68..3c7083fa1 100644 --- a/examples/validation.rs +++ b/examples/validation.rs @@ -14,12 +14,14 @@ fn main() -> cushy::Result { "Hinted" .and( text.to_input() + .placeholder("Enter some text") .validation(validations.validate(&text, validate_input)) .hint("* required"), ) .and("Not Hinted") .and( text.to_input() + .placeholder("Enter some text") .validation(validations.validate(&text, validate_input)), ) .and( diff --git a/src/widgets/input.rs b/src/widgets/input.rs index eff590db5..77ec27670 100644 --- a/src/widgets/input.rs +++ b/src/widgets/input.rs @@ -28,7 +28,7 @@ use crate::utils::ModifiersExt; use crate::value::{Destination, Dynamic, Generation, IntoDynamic, IntoValue, Source, Value}; use crate::widget::{Callback, EventHandling, Widget, HANDLED, IGNORED}; use crate::window::KeyEvent; -use crate::{ConstraintLimit, FitMeasuredSize, Lazy}; +use crate::{ConstraintLimit, FitMeasuredSize, Lazy, MaybeLocalized}; const CURSOR_BLINK_DURATION: Duration = Duration::from_millis(500); @@ -38,7 +38,7 @@ pub struct Input { /// The value of this widget. pub value: Dynamic, /// The placeholder text to display when no value is present. - pub placeholder: Value, + pub placeholder: Value, mask_symbol: Value, mask: CowString, on_key: Option>, @@ -127,7 +127,7 @@ where /// Sets the `placeholder` text, which is displayed when the field has an /// empty value. - pub fn placeholder(mut self, placeholder: impl IntoValue) -> Self { + pub fn placeholder(mut self, placeholder: impl IntoValue) -> Self { self.placeholder = placeholder.into_value(); self } @@ -605,7 +605,11 @@ where } let placeholder_color = context.theme().surface.on_color_variant; - let placeholder = self.placeholder.map(|placeholder| context.gfx.measure_text(Text::new(placeholder, placeholder_color))); + let placeholder = self.placeholder.map(|placeholder| { + let text = placeholder.localize(context).to_string(); + + context.gfx.measure_text(Text::new(&text, placeholder_color)) + }); (bytes, context.gfx.measure_text(text), placeholder) }); self.cache = Some(CachedLayout {