Skip to content

Commit 7497c10

Browse files
committed
Add support for localized placeholders.
1 parent dface87 commit 7497c10

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

examples/assets/localizations/en-US/validation-localized.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ language-es-es = Spanish (Spain)
88
#
99
form-example-hinted-label = Hinted
1010
form-example-not-hinted-label = Not hinted
11+
form-example-input-placeholder = Enter some text
1112
1213
# Specific validation reason
1314
form-input-invalid-non-whitespace-required = This field must have at least one non-whitespace character

examples/assets/localizations/es-ES/validation-localized.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ language-es-es = Español (España)
88
#
99
form-example-hinted-label = Insinuado
1010
form-example-not-hinted-label = No insinuado
11+
form-example-input-placeholder = Introduzca un texto
1112
1213
# Razón de validación específica
1314
form-input-invalid-non-whitespace-required = Este campo debe tener al menos un carácter que no sea un espacio en blanco

examples/validation-localized.rs

+2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ fn form() -> impl MakeWidget {
2121
localize!("form-example-hinted-label")
2222
.and(
2323
text.to_input()
24+
.placeholder(localize!("form-example-input-placeholder"))
2425
.validation(validations.validate(&text, validate_input))
2526
.hint(localize!("form-hint-field-required")),
2627
)
2728
.and(localize!("form-example-not-hinted-label"))
2829
.and(
2930
text.to_input()
31+
.placeholder(localize!("form-example-input-placeholder"))
3032
.validation(validations.validate(&text, validate_input)),
3133
)
3234
.and(

examples/validation.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ fn main() -> cushy::Result {
1414
"Hinted"
1515
.and(
1616
text.to_input()
17+
.placeholder("Enter some text")
1718
.validation(validations.validate(&text, validate_input))
1819
.hint("* required"),
1920
)
2021
.and("Not Hinted")
2122
.and(
2223
text.to_input()
24+
.placeholder("Enter some text")
2325
.validation(validations.validate(&text, validate_input)),
2426
)
2527
.and(

src/widgets/input.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::utils::ModifiersExt;
2828
use crate::value::{Destination, Dynamic, Generation, IntoDynamic, IntoValue, Source, Value};
2929
use crate::widget::{Callback, EventHandling, Widget, HANDLED, IGNORED};
3030
use crate::window::KeyEvent;
31-
use crate::{ConstraintLimit, FitMeasuredSize, Lazy};
31+
use crate::{ConstraintLimit, FitMeasuredSize, Lazy, MaybeLocalized};
3232

3333
const CURSOR_BLINK_DURATION: Duration = Duration::from_millis(500);
3434

@@ -38,7 +38,7 @@ pub struct Input<Storage = String> {
3838
/// The value of this widget.
3939
pub value: Dynamic<Storage>,
4040
/// The placeholder text to display when no value is present.
41-
pub placeholder: Value<String>,
41+
pub placeholder: Value<MaybeLocalized>,
4242
mask_symbol: Value<CowString>,
4343
mask: CowString,
4444
on_key: Option<Callback<KeyEvent, EventHandling>>,
@@ -127,7 +127,7 @@ where
127127

128128
/// Sets the `placeholder` text, which is displayed when the field has an
129129
/// empty value.
130-
pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> Self {
130+
pub fn placeholder(mut self, placeholder: impl IntoValue<MaybeLocalized>) -> Self {
131131
self.placeholder = placeholder.into_value();
132132
self
133133
}
@@ -605,7 +605,11 @@ where
605605
}
606606

607607
let placeholder_color = context.theme().surface.on_color_variant;
608-
let placeholder = self.placeholder.map(|placeholder| context.gfx.measure_text(Text::new(placeholder, placeholder_color)));
608+
let placeholder = self.placeholder.map(|placeholder| {
609+
let text = placeholder.localize(context).to_string();
610+
611+
context.gfx.measure_text(Text::new(&text, placeholder_color))
612+
});
609613
(bytes, context.gfx.measure_text(text), placeholder)
610614
});
611615
self.cache = Some(CachedLayout {

0 commit comments

Comments
 (0)