1 个不稳定版本
0.1.0 | 2023年10月29日 |
---|
#342 在 日期和时间
21KB
193 行
fluent-datetime
Fluent 翻译中的国际日期时间
fluent-datetime 使用 ICU4X,特别是 icu_datetime
和 icu_calendar
,在 Fluent 翻译中格式化国际日期时间。
示例
此示例直接使用 fluent_bundle
。
您可能更喜欢使用更简洁的集成;在这种情况下,只需一行 bundle.add_datetime_support)
即可。
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());
依赖项
~6.5MB
~125K SLoC