Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make format methods localizable #680

Open
tguichaoua opened this issue May 7, 2024 · 6 comments
Open

Make format methods localizable #680

tguichaoua opened this issue May 7, 2024 · 6 comments
Labels
A-formatting Area: formatting A-parsing Area: parsing C-feature-request Category: a new feature (not already implemented)

Comments

@tguichaoua
Copy link

tguichaoua commented May 7, 2024

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)
    }
}
@jhpratt
Copy link
Member

jhpratt commented May 9, 2024

This is something I previously considered when I took over time in 2019. I was talked out of it at the time, though having basic support for localization is something I was recently reconsidering. The API wouldn't quite be what you have, but it wouldn't be that far off.

@jhpratt jhpratt added C-feature-request Category: a new feature (not already implemented) A-formatting Area: formatting A-parsing Area: parsing labels May 9, 2024
@Mastermindaxe
Copy link

@jhpratt Could you make a suggestion for the API? Currently on vacation and willing to take a crack at it if you can provide the general direction, which (as you said) isn't the above suggestion

@Mastermindaxe
Copy link

I could also just make a suggestion via a PR if that's preferred :D

@jhpratt
Copy link
Member

jhpratt commented May 22, 2024

For this specifically, I'd prefer not having a PR as it'll involve some code generation that will most likely end up in a separate repository.

@Mastermindaxe
Copy link

Gotcha! Any way I can help move this forward or support you with this?

@jhpratt
Copy link
Member

jhpratt commented May 22, 2024

Not really at this point. I have other priorities, so it's not going to happen in the immediate future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-formatting Area: formatting A-parsing Area: parsing C-feature-request Category: a new feature (not already implemented)
Projects
None yet
Development

No branches or pull requests

3 participants