Skip to main content

I18n

Struct I18n 

Source
pub struct I18n<'a> { /* private fields */ }
Expand description

Small runtime localizer used by widgets and views.

Implementations§

Source§

impl<'a> I18n<'a>

Source

pub const fn new( locale: &'a str, fallback_locale: &'a str, locales: &'a [LocaleTable<'a>], ) -> Self

Creates a new localization runtime.

Examples found in repository?
examples/support/mod.rs (line 25)
24pub fn i18n() -> I18n<'static> {
25    I18n::new("en", "en", &[])
26}
More examples
Hide additional examples
examples/root_delegate.rs (line 58)
53fn main() {
54    let _ui = UiApp::<AppView, DemoDelegate, 8>::new(
55        AppView::Home,
56        DemoDelegate::new(),
57        FsTheme::default(),
58        I18n::new("en", "en", &[]),
59    );
60}
examples/uikit_root.rs (line 212)
206fn main() {
207    let bounds = Rectangle::new(Point::zero(), Size::new(320, 240));
208    let mut system = UiSystem::new(
209        NullDisplay::new(bounds),
210        RootView::new(),
211        FsTheme::default(),
212        I18n::new("en", "en", &[]),
213    );
214
215    let redraw = system.update(16).redraw;
216    let _ = system.draw_redraw(redraw);
217    let _ = system.handle_touch(TouchEvent::new(Point::new(24, 24), TouchPhase::Start, 1));
218    let _ = system.handle_touch(TouchEvent::new(Point::new(24, 24), TouchPhase::End, 2));
219    let _ = system.draw();
220    let _ = system.registration();
221    let _ = ViewRedraw::Full;
222}
Source

pub const fn locale(&self) -> &'a str

Returns the currently selected locale identifier.

Source

pub fn set_locale(&mut self, locale: &'a str)

Changes the active locale identifier.

Source

pub fn text(&self, label: Localized<'a>) -> &'a str

Resolves a Localized label into text.

Examples found in repository?
src/alert/view.rs (line 44)
43    pub fn panel(&self, bounds: Rectangle, theme: &FsTheme, i18n: &I18n<'a>) -> Rectangle {
44        alert_layout::<N>(bounds, theme, i18n.text(self.spec.message)).panel
45    }
46
47    /// Routes one touch event to the alert buttons.
48    pub fn handle_touch(&mut self, touch: TouchEvent, panel: Rectangle) -> ButtonTouchResponse<u8> {
49        let buttons = self.buttons(panel);
50        self.touch_state.handle_touch(touch, &buttons)
51    }
52
53    /// Draws the alert.
54    pub fn draw<D>(&self, display: &mut D, panel: Rectangle, theme: &FsTheme, i18n: &I18n<'a>)
55    where
56        D: embedded_graphics::draw_target::DrawTarget<Color = Rgb565>,
57    {
58        self.draw_state(display, panel, theme, i18n);
59    }
60
61    /// Draws the alert using the current touch state.
62    pub fn draw_state<D>(&self, display: &mut D, panel: Rectangle, theme: &FsTheme, i18n: &I18n<'a>)
63    where
64        D: embedded_graphics::draw_target::DrawTarget<Color = Rgb565>,
65    {
66        let layout = alert_layout_in_panel::<N>(panel, i18n.text(self.spec.message));
67        RoundedRectangle::with_equal_corners(
68            Rectangle::new(panel.top_left + Point::new(6, 8), panel.size),
69            Size::new(22, 22),
70        )
71        .into_styled(PrimitiveStyle::with_fill(theme.dim))
72        .draw(display)
73        .ok();
74
75        let panel_style = PrimitiveStyleBuilder::new()
76            .fill_color(theme.surface)
77            .stroke_color(theme.surface_alt)
78            .stroke_width(2)
79            .build();
80        RoundedRectangle::with_equal_corners(panel, Size::new(22, 22))
81            .into_styled(panel_style)
82            .draw(display)
83            .ok();
84
85        let title_style = MonoTextStyleBuilder::new()
86            .font(&FONT_9X18_BOLD)
87            .text_color(kind_color(self.spec.kind, theme))
88            .build();
89        let message_style = MonoTextStyleBuilder::new()
90            .font(&FONT_7X14)
91            .text_color(theme.text_secondary)
92            .build();
93        let text_style = TextStyleBuilder::new()
94            .alignment(Alignment::Center)
95            .baseline(Baseline::Middle)
96            .build();
97
98        Text::with_text_style(
99            i18n.text(self.spec.title),
100            panel.center() + Point::new(0, -40),
101            title_style,
102            text_style,
103        )
104        .draw(display)
105        .ok();
106        for (index, line) in layout.message_lines.iter().enumerate() {
107            Text::with_text_style(
108                line,
109                Point::new(panel.center().x, message_top(panel) + (index as i32 * 18)),
110                message_style,
111                text_style,
112            )
113            .draw(display)
114            .ok();
115        }
116
117        for button in self.buttons(panel) {
118            button.draw_state(
119                display,
120                theme,
121                i18n,
122                self.touch_state.is_highlighted(&button),
123            );
124        }
125    }
More examples
Hide additional examples
src/alert_host/presented.rs (line 67)
66    pub(super) fn panel(&self, bounds: Rectangle, theme: &FsTheme, i18n: &I18n<'a>) -> Rectangle {
67        alert_layout::<1>(bounds, theme, i18n.text(self.message)).panel
68    }
69
70    pub(super) fn handle_touch(
71        &mut self,
72        touch: TouchEvent,
73        panel: Rectangle,
74    ) -> ButtonTouchResponse<u8> {
75        let buttons = self.buttons(panel);
76        self.touch_state.handle_touch(touch, buttons.as_slice())
77    }
78
79    pub(super) fn draw<D>(
80        &self,
81        display: &mut D,
82        panel: Rectangle,
83        theme: &FsTheme,
84        i18n: &I18n<'a>,
85    ) where
86        D: UiCanvas,
87    {
88        let layout = alert_layout_in_panel::<1>(panel, i18n.text(self.message));
89        RoundedRectangle::with_equal_corners(
90            Rectangle::new(panel.top_left + Point::new(6, 8), panel.size),
91            Size::new(22, 22),
92        )
93        .into_styled(PrimitiveStyle::with_fill(theme.dim))
94        .draw(display)
95        .ok();
96        RoundedRectangle::with_equal_corners(panel, Size::new(22, 22))
97            .into_styled(
98                PrimitiveStyleBuilder::new()
99                    .fill_color(theme.surface)
100                    .stroke_color(theme.surface_alt)
101                    .stroke_width(2)
102                    .build(),
103            )
104            .draw(display)
105            .ok();
106
107        let title_style = MonoTextStyleBuilder::new()
108            .font(&FONT_9X18_BOLD)
109            .text_color(kind_color(self.kind, theme))
110            .build();
111        let message_style = MonoTextStyleBuilder::new()
112            .font(&FONT_7X14)
113            .text_color(theme.text_secondary)
114            .build();
115        let text_style = TextStyleBuilder::new()
116            .alignment(Alignment::Center)
117            .baseline(Baseline::Middle)
118            .build();
119
120        Text::with_text_style(
121            i18n.text(self.title),
122            panel.center() + Point::new(0, -40),
123            title_style,
124            text_style,
125        )
126        .draw(display)
127        .ok();
128        for (index, line) in layout.message_lines.iter().enumerate() {
129            Text::with_text_style(
130                line,
131                Point::new(panel.center().x, message_top(panel) + (index as i32 * 18)),
132                message_style,
133                text_style,
134            )
135            .draw(display)
136            .ok();
137        }
138        for button in self.buttons(panel) {
139            button.draw_state(
140                display,
141                theme,
142                i18n,
143                self.touch_state.is_highlighted(&button),
144            );
145        }
146    }
src/button/mod.rs (line 147)
101    pub fn draw_state<D>(
102        &self,
103        display: &mut D,
104        theme: &FsTheme,
105        i18n: &I18n<'a>,
106        highlighted: bool,
107    ) where
108        D: DrawTarget<Color = Rgb565>,
109    {
110        let style = PrimitiveStyleBuilder::new()
111            .fill_color(background(self.spec.kind, theme, highlighted))
112            .stroke_color(border(self.spec.kind, theme, highlighted))
113            .stroke_width(if highlighted { 3 } else { 2 })
114            .build();
115        RoundedRectangle::with_equal_corners(self.frame, Size::new(18, 18))
116            .into_styled(style)
117            .draw(display)
118            .ok();
119
120        let label_style = MonoTextStyleBuilder::new()
121            .font(&FONT_7X14)
122            .text_color(foreground(self.spec.kind, theme, highlighted))
123            .build();
124        let icon_style = MonoTextStyleBuilder::new()
125            .font(&FONT_9X18_BOLD)
126            .text_color(foreground(self.spec.kind, theme, highlighted))
127            .build();
128        let text_style = TextStyleBuilder::new()
129            .alignment(Alignment::Center)
130            .baseline(Baseline::Middle)
131            .build();
132
133        let mut label_center = self.frame.center();
134        if let Some(icon) = self.spec.icon {
135            Text::with_text_style(
136                icon,
137                self.frame.center() + Point::new(0, -10),
138                icon_style,
139                text_style,
140            )
141            .draw(display)
142            .ok();
143            label_center += Point::new(0, 14);
144        }
145
146        Text::with_text_style(
147            i18n.text(self.spec.label),
148            label_center,
149            label_style,
150            text_style,
151        )
152        .draw(display)
153        .ok();
154    }
src/tabs.rs (line 164)
111    fn draw_tab<D>(
112        &self,
113        display: &mut D,
114        index: usize,
115        bar: Rectangle,
116        theme: &FsTheme,
117        i18n: &I18n<'a>,
118    ) where
119        D: DrawTarget<Color = Rgb565>,
120    {
121        let tab = tab_frame::<N>(bar, index);
122        let spec = self.tabs[index];
123        let selected = index == self.active_index;
124        let accent = if selected {
125            theme.accent
126        } else {
127            theme.surface
128        };
129        let title_color = if selected {
130            theme.text_primary
131        } else {
132            theme.text_secondary
133        };
134
135        if selected {
136            Rectangle::new(tab.top_left, Size::new(tab.size.width, 4))
137                .into_styled(PrimitiveStyle::with_fill(accent))
138                .draw(display)
139                .ok();
140        }
141
142        let icon_style = MonoTextStyleBuilder::new()
143            .font(&FONT_7X14)
144            .text_color(title_color)
145            .build();
146        let title_style = MonoTextStyleBuilder::new()
147            .font(&FONT_6X10)
148            .text_color(title_color)
149            .build();
150        let text_style = TextStyleBuilder::new()
151            .alignment(Alignment::Center)
152            .baseline(Baseline::Middle)
153            .build();
154
155        Text::with_text_style(
156            spec.icon,
157            tab.center() + Point::new(0, -10),
158            icon_style,
159            text_style,
160        )
161        .draw(display)
162        .ok();
163        Text::with_text_style(
164            i18n.text(spec.title),
165            tab.center() + Point::new(0, 14),
166            title_style,
167            text_style,
168        )
169        .draw(display)
170        .ok();
171
172        let outline = PrimitiveStyleBuilder::new()
173            .stroke_color(theme.surface_alt)
174            .stroke_width(1)
175            .build();
176        Rectangle::new(tab.top_left, Size::new(tab.size.width, tab.size.height))
177            .into_styled(outline)
178            .draw(display)
179            .ok();
180    }
src/stack_view.rs (line 191)
120    pub fn draw_chrome<D>(
121        &self,
122        display: &mut D,
123        theme: &FsTheme,
124        i18n: &I18n<'a>,
125        touch: &ButtonTouchState<NavHeaderAction>,
126    ) where
127        D: DrawTarget<Color = Rgb565>,
128    {
129        let shell = PrimitiveStyleBuilder::new()
130            .fill_color(theme.surface_alt)
131            .stroke_color(theme.surface)
132            .stroke_width(2)
133            .build();
134        self.frame.into_styled(shell).draw(display).ok();
135
136        self.header
137            .into_styled(PrimitiveStyle::with_fill(theme.surface))
138            .draw(display)
139            .ok();
140        Line::new(
141            self.header.top_left + Point::new(0, self.header.size.height as i32),
142            self.header.bottom_right().unwrap_or(self.header.top_left),
143        )
144        .into_styled(PrimitiveStyle::with_stroke(theme.surface_alt, 2))
145        .draw(display)
146        .ok();
147
148        let title_style = MonoTextStyleBuilder::new()
149            .font(&FONT_9X18_BOLD)
150            .text_color(theme.text_primary)
151            .build();
152        let back_style = MonoTextStyleBuilder::new()
153            .font(&FONT_7X14)
154            .text_color(theme.text_primary)
155            .build();
156        let text_style = TextStyleBuilder::new()
157            .alignment(Alignment::Center)
158            .baseline(Baseline::Middle)
159            .build();
160        let settled_back_button = self.back_title.is_some() && self.secondary_title.is_none();
161
162        if settled_back_button {
163            let button = self
164                .back_button
165                .as_ref()
166                .expect("settled stacked headers include a back button");
167            button.draw_state(display, theme, i18n, touch.is_highlighted(button));
168            let arrow = MonoTextStyleBuilder::new()
169                .font(&FONT_7X14)
170                .text_color(theme.text_primary)
171                .build();
172            Text::with_text_style(
173                "<",
174                button.frame.top_left + Point::new(18, (button.frame.size.height / 2) as i32),
175                arrow,
176                TextStyleBuilder::new()
177                    .alignment(Alignment::Center)
178                    .baseline(Baseline::Middle)
179                    .build(),
180            )
181            .draw(display)
182            .ok();
183        }
184
185        let mut header = display.clipped(&self.header);
186        if settled_back_button {
187            let title = self
188                .back_title
189                .expect("settled stacked headers include a back title");
190            Text::with_text_style(
191                i18n.text(title),
192                back_title_center(self.back_button.as_ref()),
193                back_style,
194                text_style,
195            )
196            .draw(&mut header)
197            .ok();
198        }
199        draw_title(
200            &mut header,
201            i18n.text(self.active_title.title),
202            self.active_title.center,
203            title_style,
204            text_style,
205        );
206        if let Some(title) = self.secondary_title {
207            draw_title(
208                &mut header,
209                i18n.text(title.title),
210                title.center,
211                title_style,
212                text_style,
213            );
214        }
215    }

Trait Implementations§

Source§

impl<'a> Clone for I18n<'a>

Source§

fn clone(&self) -> I18n<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for I18n<'a>

Source§

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

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

impl<'a> Copy for I18n<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for I18n<'a>

§

impl<'a> RefUnwindSafe for I18n<'a>

§

impl<'a> Send for I18n<'a>

§

impl<'a> Sync for I18n<'a>

§

impl<'a> Unpin for I18n<'a>

§

impl<'a> UnsafeUnpin for I18n<'a>

§

impl<'a> UnwindSafe for I18n<'a>

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.