Skip to content

Make format methods localizable #680

@tguichaoua

Description

@tguichaoua

It should be possible to make format methods localizable by passing a struct Localization with the month and weekday names.
This struct is passed down to fmt_month and fmt_weekday methods that used it instead of the hardcoded MONTH_NAMES and WEEKDAY_NAMES.

pub struct Localization {
    pub month_names: [&[u8]; 12],
    pub weekday_names: [&[u8]; 7],
}

impl Localization {
    pub const EN: Self = Self { /* ... */ }
    pub const FR: Self = Self { /* ... */ }
    pub const DE: Self = Self { /* ... */ }
    pub const ES: Self = Self { /* ... */ }
     /* ... */
}

pub trait Sealed {
    fn format_into(
        &self,
        output: &mut impl Write,
        localization: &Localization,    // <-- new argument
        date: Option<Date>,
        time: Option<Time>,
        offset: Option<UtcOffset>
    ) -> Result<usize, Format>;

    fn format(
        &self,
        localization: &Localization,    // <-- new argument
        date: Option<Date>,
        time: Option<Time>,
        offset: Option<UtcOffset>
    ) -> Result<String, Format> { ... }
}

impl { Date, Time, ... } {
    pub fn format(self, format: &(impl Formattable + ?Sized)) -> Result<String, error::Format> {
        // Preserve the current behaviour
        self.format_localized(format, &Localization::EN)
    }

    pub fn format_localized(self, format: &(impl Formattable + ?Sized), localization: &Localization) -> Result<String, error::Format> {
        format.format(localization, Some(self), None, None)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-formattingArea: formattingA-parsingArea: parsingC-feature-requestCategory: a new feature (not already implemented)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions