fluent-datetime
International datetimes in Fluent translations
fluent-datetime uses ICU4X, in particular [icu_datetime
] and
[icu_calendar
], to format datetimes internationally within
a Fluent translation.
Example
This example uses [fluent_bundle
] directly.
You may prefer to use less verbose integrations; in which case the
bundle.add_datetime_support()
line is the only one you need.
use fluent_args;
use ;
use ;
use DateTime;
use length;
use LanguageIdentifier;
// Create a FluentBundle
let langid_en: LanguageIdentifier = "en-US".parse?;
let mut bundle = new;
// 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 = try_new
.expect;
bundle
.add_resource
.expect;
// Create an ICU DateTime
let datetime = try_new_iso_datetime
.expect;
// Convert to FluentDateTime
let mut datetime = from;
// Format some messages with date arguments
let mut errors = vec!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// 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;
assert_eq!;
assert!;