Skip to content

Commit 34f63ce

Browse files
committed
Add support for localized placeholders.
1 parent 92a6daa commit 34f63ce

File tree

6 files changed

+15
-5
lines changed

6 files changed

+15
-5
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/reactive/value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::widget::{
3333
use crate::widgets::checkbox::CheckboxState;
3434
use crate::widgets::{Checkbox, Radio, Select, Space, Switcher};
3535
use crate::window::WindowHandle;
36-
use crate::{Lazy, MaybeLocalized};
36+
use crate::MaybeLocalized;
3737

3838
/// A source of one or more `T` values.
3939
pub trait Source<T> {

src/widgets/input.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::styles::components::{HighlightColor, IntrinsicPadding, OutlineColor,
3030
use crate::utils::ModifiersExt;
3131
use crate::widget::{Callback, EventHandling, Widget, HANDLED, IGNORED};
3232
use crate::window::KeyEvent;
33-
use crate::{ConstraintLimit, FitMeasuredSize, Lazy};
33+
use crate::{ConstraintLimit, FitMeasuredSize, Lazy, MaybeLocalized};
3434

3535
const CURSOR_BLINK_DURATION: Duration = Duration::from_millis(500);
3636

@@ -40,7 +40,7 @@ pub struct Input<Storage = String> {
4040
/// The value of this widget.
4141
pub value: Dynamic<Storage>,
4242
/// The placeholder text to display when no value is present.
43-
pub placeholder: Value<String>,
43+
pub placeholder: Value<MaybeLocalized>,
4444
mask_symbol: Value<CowString>,
4545
mask: CowString,
4646
on_key: Option<Callback<KeyEvent, EventHandling>>,
@@ -129,7 +129,7 @@ where
129129

130130
/// Sets the `placeholder` text, which is displayed when the field has an
131131
/// empty value.
132-
pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> Self {
132+
pub fn placeholder(mut self, placeholder: impl IntoValue<MaybeLocalized>) -> Self {
133133
self.placeholder = placeholder.into_value();
134134
self
135135
}
@@ -607,7 +607,11 @@ where
607607
}
608608

609609
let placeholder_color = context.theme().surface.on_color_variant;
610-
let placeholder = self.placeholder.map(|placeholder| context.gfx.measure_text(Text::new(placeholder, placeholder_color)));
610+
let placeholder = self.placeholder.map(|placeholder| {
611+
let text = placeholder.localize(context).to_string();
612+
613+
context.gfx.measure_text(Text::new(&text, placeholder_color))
614+
});
611615
(bytes, context.gfx.measure_text(text), placeholder)
612616
});
613617
self.cache = Some(CachedLayout {

0 commit comments

Comments
 (0)