1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
//! # International datetimes in Fluent translations
//!
//! fluent-datetime uses [ICU4X], in particular [`icu_datetime`] and
//! [`icu_calendar`], to format datetimes internationally within
//! a [Fluent] translation.
//!
//! [Fluent]: https://projectfluent.org/
//! [ICU4X]: https://github.com/unicode-org/icu4x
//!
//! # Example
//!
//! This example uses [`fluent_bundle`] directly.
//!
//! You may prefer to use less verbose integrations; in which case the
//! [`bundle.add_datetime_support()`](BundleExt::add_datetime_support)
//! line is the only one you need.
//!
//! ```rust
//! use fluent::fluent_args;
//! use fluent_bundle::{FluentBundle, FluentResource};
//! use fluent_datetime::{BundleExt, FluentDateTime};
//! use icu_calendar::DateTime;
//! use icu_datetime::options::length;
//! use unic_langid::LanguageIdentifier;
//!
//! // Create a FluentBundle
//! let langid_en: LanguageIdentifier = "en-US".parse()?;
//! let mut bundle = FluentBundle::new(vec![langid_en]);
//!
//! // Register the DATETIME function
//! bundle.add_datetime_support();
//!
//! // Add a FluentResource to the bundle
//! let ftl_string = r#"
//! today-is = Today is {$date}
//! today-is-fulldate = Today is {DATETIME($date, dateStyle: "full")}
//! now-is-time = Now is {DATETIME($date, timeStyle: "medium")}
//! now-is-datetime = Now is {DATETIME($date, dateStyle: "full", timeStyle: "short")}
//! "#
//! .to_string();
//!
//! let res = FluentResource::try_new(ftl_string)
//! .expect("Failed to parse an FTL string.");
//! bundle
//! .add_resource(res)
//! .expect("Failed to add FTL resources to the bundle.");
//!
//! // Create an ICU DateTime
//! let datetime = DateTime::try_new_iso_datetime(1989, 11, 9, 23, 30, 0)
//! .expect("Failed to create ICU DateTime");
//!
//! // Convert to FluentDateTime
//! let mut datetime = FluentDateTime::from(datetime);
//!
//! // Format some messages with date arguments
//! let mut errors = vec![];
//!
//! assert_eq!(
//! bundle.format_pattern(
//! &bundle.get_message("today-is").unwrap().value().unwrap(),
//! Some(&fluent_args!("date" => datetime.clone())), &mut errors),
//! "Today is \u{2068}11/9/89\u{2069}"
//! );
//!
//! assert_eq!(
//! bundle.format_pattern(
//! &bundle.get_message("today-is-fulldate").unwrap().value().unwrap(),
//! Some(&fluent_args!("date" => datetime.clone())), &mut errors),
//! "Today is \u{2068}Thursday, November 9, 1989\u{2069}"
//! );
//!
//! assert_eq!(
//! bundle.format_pattern(
//! &bundle.get_message("now-is-time").unwrap().value().unwrap(),
//! Some(&fluent_args!("date" => datetime.clone())), &mut errors),
//! "Now is \u{2068}11:30:00\u{202f}PM\u{2069}"
//! );
//!
//! assert_eq!(
//! bundle.format_pattern(
//! &bundle.get_message("now-is-datetime").unwrap().value().unwrap(),
//! Some(&fluent_args!("date" => datetime.clone())), &mut errors),
//! "Now is \u{2068}Thursday, November 9, 1989, 11:30\u{202f}PM\u{2069}"
//! );
//!
//! // Set FluentDateTime.options in code rather than in translation data
//! // This is useful because it sets presentation options that are
//! // shared between all locales
//! datetime.options.set_date_style(Some(length::Date::Full));
//! assert_eq!(
//! bundle.format_pattern(
//! &bundle.get_message("today-is").unwrap().value().unwrap(),
//! Some(&fluent_args!("date" => datetime)), &mut errors),
//! "Today is \u{2068}Thursday, November 9, 1989\u{2069}"
//! );
//!
//! assert!(errors.is_empty());
//!
//! # // I would like to use the ? operator, but Fluent and ICU error types don't implement the std Error trait…
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
#![forbid(unsafe_code)]
#![warn(missing_docs)]
use std::borrow::Cow;
use std::mem::discriminant;
use fluent_bundle::bundle::FluentBundle;
use fluent_bundle::types::FluentType;
use fluent_bundle::{FluentArgs, FluentError, FluentValue};
use icu_calendar::{Gregorian, Iso};
use icu_datetime::options::length;
fn val_as_str<'a>(val: &'a FluentValue) -> Option<&'a str> {
if let FluentValue::String(str) = val {
Some(str)
} else {
None
}
}
/// Options for formatting a DateTime
#[derive(Debug, Clone, PartialEq)]
pub struct FluentDateTimeOptions {
// This calendar arg makes loading provider data and memoizing formatters harder
// In particular, the AnyCalendarKind logic (in
// AnyCalendarKind::from_data_locale_with_fallback) that defaults to
// Gregorian for most calendars, except for the thai locale (Buddhist),
// isn't exposed. So we would have to build the formatter and then decide
// if it is the correct one for the calendar we want.
//calendar: Option<icu_calendar::AnyCalendarKind>,
// We don't handle icu_datetime per-component settings atm, it is experimental
// and length is expressive enough so far
length: length::Bag,
}
impl Default for FluentDateTimeOptions {
/// Defaults to showing a short date
///
/// The intent is to emulate [Intl.DateTimeFormat] behavior:
/// > The default value for each date-time component option is undefined,
/// > but if all component properties are undefined, then year, month, and day default
/// > to "numeric". If any of the date-time component options is specified, then
/// > dateStyle and timeStyle must be undefined.
///
/// In terms of the current Rust implementation:
///
/// The default value for each date-time style option is None, but if both
/// are unset, we display the date only, using the `length::Date::Short`
/// style.
///
/// [Intl.DateTimeFormat]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
fn default() -> Self {
Self {
length: length::Bag::empty(),
}
}
}
impl FluentDateTimeOptions {
/// Set a date style, from verbose to compact
///
/// See [`icu_datetime::options::length::Date`].
pub fn set_date_style(&mut self, style: Option<length::Date>) {
self.length.date = style;
}
/// Set a time style, from verbose to compact
///
/// See [`icu_datetime::options::length::Time`].
pub fn set_time_style(&mut self, style: Option<length::Time>) {
self.length.time = style;
}
fn make_formatter(
&self,
locale: &icu_provider::DataLocale,
) -> Result<DateTimeFormatter, icu_datetime::DateTimeError> {
let mut length = self.length;
if length == length::Bag::empty() {
length = length::Bag::from_date_style(length::Date::Short);
}
Ok(DateTimeFormatter(icu_datetime::DateTimeFormatter::try_new(
locale,
length.into(),
)?))
}
fn merge_args(&mut self, other: &FluentArgs) -> Result<(), ()> {
// TODO set an err state on self to match fluent-js behaviour
for (k, v) in other.iter() {
match k {
"dateStyle" => {
self.length.date = Some(match val_as_str(v).ok_or(())? {
"full" => length::Date::Full,
"long" => length::Date::Long,
"medium" => length::Date::Medium,
"short" => length::Date::Short,
_ => return Err(()),
});
}
"timeStyle" => {
self.length.time = Some(match val_as_str(v).ok_or(())? {
"full" => length::Time::Full,
"long" => length::Time::Long,
"medium" => length::Time::Medium,
"short" => length::Time::Short,
_ => return Err(()),
});
}
_ => (), // Ignore with no warning
}
}
Ok(())
}
}
impl std::hash::Hash for FluentDateTimeOptions {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
// We could also use serde… or send a simple PR to have derive(Hash) upstream
//self.calendar.hash(state);
self.length.date.map(|e| discriminant(&e)).hash(state);
self.length.time.map(|e| discriminant(&e)).hash(state);
}
}
impl Eq for FluentDateTimeOptions {}
/// An ICU [`DateTime`](icu_calendar::DateTime) with attached formatting options
///
/// Construct from an [`icu_calendar::DateTime`] using From / Into.
///
/// Convert to a [`FluentValue`] with From / Into.
///
/// See [`FluentDateTimeOptions`] and [`FluentDateTimeOptions::default`].
///
///```
/// use icu_calendar::DateTime;
/// use fluent_datetime::FluentDateTime;
///
/// let datetime = DateTime::try_new_iso_datetime(1989, 11, 9, 23, 30, 0)
/// .expect("Failed to create ICU DateTime");
///
/// let datetime = FluentDateTime::from(datetime);
// ```
#[derive(Debug, Clone, PartialEq)]
pub struct FluentDateTime {
// Iso seemed like a natural default, but [AnyCalendarKind::from_data_locale_with_fallback]
// loads Gregorian in almost all cases. Differences have to do with eras:
// proleptic Gregorian has BCE / CE and no year zero, iso has just the one era and a year zero
value: icu_calendar::DateTime<Gregorian>,
/// Options for rendering
pub options: FluentDateTimeOptions,
}
impl FluentType for FluentDateTime {
fn duplicate(&self) -> Box<dyn FluentType + Send> {
// Basically Clone
Box::new(self.clone())
}
fn as_string(&self, intls: &intl_memoizer::IntlLangMemoizer) -> Cow<'static, str> {
intls
.with_try_get::<DateTimeFormatter, _, _>(self.options.clone(), |dtf| {
dtf.0
.format_to_string(&self.value.to_any())
.unwrap_or_default()
})
.unwrap_or_default()
.into()
}
fn as_string_threadsafe(
&self,
intls: &intl_memoizer::concurrent::IntlLangMemoizer,
) -> Cow<'static, str> {
// Maybe don't try to cache formatters in this case, the traits don't work out
let lang = intls
.with_try_get::<GimmeTheLocale, _, _>((), |gimme| gimme.0.clone())
.expect("Infallible");
let Some(langid): Option<icu_locid::LanguageIdentifier> = lang.to_string().parse().ok()
else {
return "".into();
};
let Ok(dtf) = self.options.make_formatter(&langid.into()) else {
return "".into();
};
dtf.0
.format_to_string(&self.value.to_any())
.unwrap_or_default()
.into()
}
}
impl From<icu_calendar::DateTime<Gregorian>> for FluentDateTime {
fn from(value: icu_calendar::DateTime<Gregorian>) -> Self {
Self {
value,
options: Default::default(),
}
}
}
impl From<icu_calendar::DateTime<Iso>> for FluentDateTime {
fn from(value: icu_calendar::DateTime<Iso>) -> Self {
Self {
value: value.to_calendar(Gregorian),
options: Default::default(),
}
}
}
impl From<FluentDateTime> for FluentValue<'static> {
fn from(value: FluentDateTime) -> Self {
Self::Custom(Box::new(value))
}
}
struct DateTimeFormatter(icu_datetime::DateTimeFormatter);
impl intl_memoizer::Memoizable for DateTimeFormatter {
type Args = FluentDateTimeOptions;
type Error = ();
fn construct(
lang: unic_langid::LanguageIdentifier,
args: Self::Args,
) -> Result<Self, Self::Error>
where
Self: std::marker::Sized,
{
// Convert LanguageIdentifier from unic_langid to icu_locid
let langid: icu_locid::LanguageIdentifier = lang.to_string().parse().map_err(|_| ())?;
args.make_formatter(&langid.into()).map_err(|_| ())
}
}
/// Working around that intl_memoizer API, because IntlLangMemoizer doesn't
/// expose the language it is caching
///
/// This would be a trivial addition but it isn't maintained these days.
struct GimmeTheLocale(unic_langid::LanguageIdentifier);
impl intl_memoizer::Memoizable for GimmeTheLocale {
type Args = ();
type Error = std::convert::Infallible;
fn construct(lang: unic_langid::LanguageIdentifier, _args: ()) -> Result<Self, Self::Error>
where
Self: std::marker::Sized,
{
Ok(Self(lang))
}
}
/// A Fluent function for formatted datetimes
///
/// Normally you would register this using
/// [`BundleExt::add_datetime_support`]; you would not use it directly.
///
/// However, some frameworks like [l10n](https://lib.rs/crates/l10n)
/// require functions to be set up like this:
///
/// ```ignore
/// l10n::init!({
/// functions: { "DATETIME": fluent_datetime::DATETIME }
/// });
/// ```
///
/// # Usage
///
/// ```fluent
/// today-is = Today is {$date}
/// today-is-fulldate = Today is {DATETIME($date, dateStyle: "full")}
/// now-is-time = Now is {DATETIME($date, timeStyle: "medium")}
/// now-is-datetime = Now is {DATETIME($date, dateStyle: "full", timeStyle: "short")}
/// ````
///
/// See [`DATETIME` in the Fluent guide][datetime-fluent]
/// and [the `Intl.DateTimeFormat` constructor][Intl.DateTimeFormat]
/// from [ECMA 402] for how to use this inside a Fluent document.
///
/// We currently implement only a subset of the formatting options:
/// * `dateStyle`
/// * `timeStyle`
///
/// Unknown options and extra positional arguments are ignored, unknown values
/// of known options cause the date to be returned as-is.
///
/// [datetime-fluent]: https://projectfluent.org/fluent/guide/functions.html#datetime
/// [Intl.DateTimeFormat]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
/// [ECMA 402]: https://tc39.es/ecma402/#sec-createdatetimeformat
#[allow(non_snake_case)]
pub fn DATETIME<'a>(positional: &[FluentValue<'a>], named: &FluentArgs) -> FluentValue<'a> {
match positional.get(0) {
Some(FluentValue::Custom(cus)) => {
if let Some(dt) = cus.as_any().downcast_ref::<FluentDateTime>() {
let mut dt = dt.clone();
let Ok(()) = dt.options.merge_args(named) else {
return FluentValue::Error;
};
FluentValue::Custom(Box::new(dt))
} else {
FluentValue::Error
}
}
// https://github.com/projectfluent/fluent/wiki/Error-Handling
// argues for graceful recovery (think lingering trauma from XUL DTD
// errors)
_ => FluentValue::Error,
}
}
/// Extension trait to register DateTime support on [`FluentBundle`]
///
/// [`FluentDateTime`] values are rendered automatically, but you need to call
/// [`BundleExt::add_datetime_support`] at bundle creation time when using
/// the [`DATETIME`] function inside FTL resources.
pub trait BundleExt {
/// Registers the [`DATETIME`] function
///
/// Call this on a [`FluentBundle`].
///
fn add_datetime_support(&mut self) -> Result<(), FluentError>;
}
impl<R, M> BundleExt for FluentBundle<R, M> {
fn add_datetime_support(&mut self) -> Result<(), FluentError> {
self.add_function("DATETIME", DATETIME)?;
//self.set_formatter(Some(datetime_formatter));
Ok(())
}
}