unicode-org / icu4x

Solving i18n for client-side and resource-constrained environments.
https://icu4x.unicode.org
Other
1.38k stars 178 forks source link

How to get month names from specific locale #5801

Closed keepitupkitty closed 1 week ago

keepitupkitty commented 2 weeks ago

Hello, I am working on my project that involves date handling, I want to extract names of months from specific locale I use rust and I can't figure out if there's something like getShortMonths like it's in C++ icu

sffc commented 1 week ago

In the upcoming ICU4X 2.0, use the semantic skeleton API

https://unicode-org.github.io/icu4x/rustdoc/icu/datetime/fieldset/struct.M.html

use icu::calendar::Date;
use icu::calendar::Gregorian;
use icu::datetime::FixedCalendarDateTimeFormatter;
use icu::datetime::fieldset::M;
use icu::locale::locale;
use writeable::assert_try_writeable_eq;

let fmt = FixedCalendarDateTimeFormatter::<Gregorian, M>::try_new(
    &locale!("en").into(),
   M::long(),
)
.unwrap();
let dt = Date::try_new_gregorian(2024, 5, 17).unwrap();

assert_try_writeable_eq!(
    fmt.format(&dt),
   "May"
);

It looks like it didn't quite make it into the 1.5 snapshot, but 2.0-beta should be released this month.

If you need it in 1.5, use the old experimental skeleton API (removed in 2.0).

https://docs.rs/icu/latest/icu/datetime/options/components/index.html

keepitupkitty commented 1 week ago

Thank you!!

On Tue, Nov 12, 2024, 9:35 AM Shane F. Carr @.***> wrote:

In the upcoming ICU4X 2.0, use the semantic skeleton API

https://unicode-org.github.io/icu4x/rustdoc/icu/datetime/fieldset/struct.M.html

use icu::calendar::Date;use icu::calendar::Gregorian;use icu::datetime::FixedCalendarDateTimeFormatter;use icu::datetime::fieldset::M;use icu::locale::locale;use writeable::assert_try_writeable_eq; let fmt = FixedCalendarDateTimeFormatter::<Gregorian, M>::try_new( &locale!("en").into(), M::long(),).unwrap();let dt = Date::try_new_gregorian(2024, 5, 17).unwrap(); assert_try_writeable_eq!( fmt.format(&dt), "May");

It looks like it didn't quite make it into the 1.5 snapshot, but 2.0-beta should be released this month.

If you need it in 1.5, use the old experimental skeleton API (removed in 2.0).

https://docs.rs/icu/latest/icu/datetime/options/components/index.html

— Reply to this email directly, view it on GitHub https://github.com/unicode-org/icu4x/issues/5801#issuecomment-2469714720, or unsubscribe https://github.com/notifications/unsubscribe-auth/BAN7OVBVPML2LUYDFZ7AOIT2AGORTAVCNFSM6AAAAABRORAYDKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINRZG4YTINZSGA . You are receiving this because you authored the thread.Message ID: @.***>