Skip to main content

es_fluent/traits/
fluent_display.rs

1use std::fmt;
2
3/// This trait is similar to `std::fmt::Display`, but it is used for formatting
4/// types that can be displayed in a Fluent message.
5pub trait FluentDisplay {
6    /// Formats the value using the given formatter.
7    fn fluent_fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
8}
9
10/// This trait is automatically implemented for any type that implements
11/// `FluentDisplay`.
12pub trait ToFluentString {
13    /// Converts the type into a Fluent (i18n translated) string.
14    fn to_fluent_string(&self) -> String;
15}
16
17impl<T: FluentDisplay + ?Sized> ToFluentString for T {
18    fn to_fluent_string(&self) -> String {
19        struct FluentDisplayWrapper<'a, T: ?Sized> {
20            inner: &'a T,
21        }
22
23        impl<'a, T: FluentDisplay + ?Sized> fmt::Display for FluentDisplayWrapper<'a, T> {
24            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25                self.inner.fluent_fmt(f)
26            }
27        }
28
29        FluentDisplayWrapper { inner: self }.to_string()
30    }
31}