-
Hi ! In my mind, the function I want to do has a very simple signature: fn get_abbr_month_names(locale: Locale) -> Result<Vec<String>>; I can't figure out how to do this properly. The furthest I was able to go right now is making a let mut fmt: FixedCalendarDateTimeNames<Gregorian, fieldsets::M> =
FixedCalendarDateTimeNames::try_new(locale.into()).unwrap(); Any advice on this matter ? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 15 replies
-
The problem is that there isn't such a thing as a "list of month names in the current locale" in general. For example, in multiple calendar systems (including Hebrew, Chinese, and Dangi), the list of month names depends on the year, not just the locale. The code you wrote above will allow you to fetch the Gregorian month names. |
Beta Was this translation helpful? Give feedback.
-
Since it sounds like you are trying to use this data for parsing, you may want to read my blog post on the subject: https://blog.sffc.xyz/post/190943794505/why-you-should-not-parse-localized-strings Basically, the way to solve the problem is to avoid an architecture that requires you to parse month names, which can be done much of the time. I acknowledge that there are still use cases that need to do this, and I encourage use-case-specific solutions for them. |
Beta Was this translation helpful? Give feedback.
-
Here's how I eventually did it pub fn get_abbr_month_names(loc: Locale) -> Result<Vec<String>, DataError> {
if loc == DEFAULT_LOCALE {
const DEFAULT_ABBR_MONTHS: &[&str] = &[
"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC",
];
return Ok(DEFAULT_ABBR_MONTHS
.iter()
.map(ToString::to_string)
.collect());
}
let data_locale = DataLocale::from(loc);
let request = DataRequest {
id: DataIdentifierBorrowed::for_marker_attributes_and_locale(ABBR_STANDALONE, &data_locale),
metadata: DataRequestMetadata::default(),
};
let response: DataResponse<DatetimeNamesMonthGregorianV1> =
icu_datetime::provider::Baked.load(request)?;
match response.payload.get() {
MonthNames::Linear(months) => Ok(months.iter().map(ToString::to_string).collect()),
_ => todo!("unsupported"),
}
} |
Beta Was this translation helpful? Give feedback.
Here's how I eventually did it