diff options
author | Colin Reeder <colin@vpzom.click> | 2020-07-21 20:13:24 -0600 |
---|---|---|
committer | Colin Reeder <colin@vpzom.click> | 2020-07-21 20:13:56 -0600 |
commit | 9a5854edf5e08d2155d3dcd332f1530243a628c7 (patch) | |
tree | 0e0f82c9579cccb9d0f786028ec71dd5612542e2 | |
parent | 207d06ae4118bf42828db2300cee74c0a42237ba (diff) |
Replace timeago with custom implementation with language files
-rw-r--r-- | Cargo.lock | 247 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | res/lang/en.flt | 37 | ||||
-rw-r--r-- | res/lang/eo.flt | 37 | ||||
-rw-r--r-- | src/components/mod.rs | 15 | ||||
-rw-r--r-- | src/components/timeago.rs | 70 | ||||
-rw-r--r-- | src/main.rs | 26 | ||||
-rw-r--r-- | src/routes/posts.rs | 2 |
8 files changed, 189 insertions, 246 deletions
@@ -17,12 +17,6 @@ dependencies = [ [[package]] name = "autocfg" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" - -[[package]] -name = "autocfg" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" @@ -69,15 +63,6 @@ dependencies = [ ] [[package]] -name = "cloudabi" -version = "0.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" -dependencies = [ - "bitflags", -] - -[[package]] name = "core-foundation" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -167,12 +152,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" [[package]] -name = "fuchsia-cprng" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" - -[[package]] name = "fuchsia-zircon" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -304,7 +283,6 @@ dependencies = [ "serde_derive", "serde_json", "serde_urlencoded", - "timeago", "tokio", "trout", "unic-langid", @@ -406,7 +384,7 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c398b2b113b55809ceb9ee3e753fcbac793f1956663f3c36549c1346015c2afe" dependencies = [ - "autocfg 1.0.0", + "autocfg", ] [[package]] @@ -439,16 +417,6 @@ dependencies = [ ] [[package]] -name = "isolang" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "265ef164908329e47e753c769b14cbb27434abf0c41984dca201484022f09ce5" -dependencies = [ - "phf 0.7.24", - "phf_codegen 0.7.24", -] - -[[package]] name = "itoa" version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -504,8 +472,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aae38d669396ca9b707bfc3db254bc382ddb94f57cc5c235f34623a669a01dab" dependencies = [ "log", - "phf 0.8.0", - "phf_codegen 0.8.0", + "phf", + "phf_codegen", "serde", "serde_derive", "serde_json", @@ -610,7 +578,7 @@ version = "0.1.43" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" dependencies = [ - "autocfg 1.0.0", + "autocfg", "num-traits", ] @@ -620,7 +588,7 @@ version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" dependencies = [ - "autocfg 1.0.0", + "autocfg", ] [[package]] @@ -649,7 +617,7 @@ version = "0.9.58" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a842db4709b604f0fe5d1170ae3565899be2ad3d9cbc72dedc789ac0511f78de" dependencies = [ - "autocfg 1.0.0", + "autocfg", "cc", "libc", "pkg-config", @@ -664,30 +632,11 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" [[package]] name = "phf" -version = "0.7.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3da44b85f8e8dfaec21adae67f95d93244b2ecf6ad2a692320598dcc8e6dd18" -dependencies = [ - "phf_shared 0.7.24", -] - -[[package]] -name = "phf" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12" dependencies = [ - "phf_shared 0.8.0", -] - -[[package]] -name = "phf_codegen" -version = "0.7.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b03e85129e324ad4166b06b2c7491ae27fe3ec353af72e72cd1654c7225d517e" -dependencies = [ - "phf_generator 0.7.24", - "phf_shared 0.7.24", + "phf_shared", ] [[package]] @@ -696,18 +645,8 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cbffee61585b0411840d3ece935cce9cb6321f01c45477d30066498cd5e1a815" dependencies = [ - "phf_generator 0.8.0", - "phf_shared 0.8.0", -] - -[[package]] -name = "phf_generator" -version = "0.7.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09364cc93c159b8b06b1f4dd8a4398984503483891b0c26b867cf431fb132662" -dependencies = [ - "phf_shared 0.7.24", - "rand 0.6.5", + "phf_generator", + "phf_shared", ] [[package]] @@ -716,17 +655,8 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17367f0cc86f2d25802b2c26ee58a7b23faeccf78a396094c13dced0d0182526" dependencies = [ - "phf_shared 0.8.0", - "rand 0.7.3", -] - -[[package]] -name = "phf_shared" -version = "0.7.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0" -dependencies = [ - "siphasher 0.2.3", + "phf_shared", + "rand", ] [[package]] @@ -735,7 +665,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7" dependencies = [ - "siphasher 0.3.3", + "siphasher", ] [[package]] @@ -840,45 +770,16 @@ dependencies = [ [[package]] name = "rand" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" -dependencies = [ - "autocfg 0.1.7", - "libc", - "rand_chacha 0.1.1", - "rand_core 0.4.2", - "rand_hc 0.1.0", - "rand_isaac", - "rand_jitter", - "rand_os", - "rand_pcg 0.1.2", - "rand_xorshift", - "winapi 0.3.8", -] - -[[package]] -name = "rand" version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" dependencies = [ "getrandom", "libc", - "rand_chacha 0.2.2", - "rand_core 0.5.1", - "rand_hc 0.2.0", - "rand_pcg 0.2.1", -] - -[[package]] -name = "rand_chacha" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" -dependencies = [ - "autocfg 0.1.7", - "rand_core 0.3.1", + "rand_chacha", + "rand_core", + "rand_hc", + "rand_pcg", ] [[package]] @@ -888,26 +789,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" dependencies = [ "ppv-lite86", - "rand_core 0.5.1", -] - -[[package]] -name = "rand_core" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" -dependencies = [ - "rand_core 0.4.2", + "rand_core", ] [[package]] name = "rand_core" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" - -[[package]] -name = "rand_core" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" @@ -917,64 +803,11 @@ dependencies = [ [[package]] name = "rand_hc" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" -dependencies = [ - "rand_core 0.3.1", -] - -[[package]] -name = "rand_hc" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" dependencies = [ - "rand_core 0.5.1", -] - -[[package]] -name = "rand_isaac" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" -dependencies = [ - "rand_core 0.3.1", -] - -[[package]] -name = "rand_jitter" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" -dependencies = [ - "libc", - "rand_core 0.4.2", - "winapi 0.3.8", -] - -[[package]] -name = "rand_os" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" -dependencies = [ - "cloudabi", - "fuchsia-cprng", - "libc", - "rand_core 0.4.2", - "rdrand", - "winapi 0.3.8", -] - -[[package]] -name = "rand_pcg" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" -dependencies = [ - "autocfg 0.1.7", - "rand_core 0.4.2", + "rand_core", ] [[package]] @@ -983,25 +816,7 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429" dependencies = [ - "rand_core 0.5.1", -] - -[[package]] -name = "rand_xorshift" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" -dependencies = [ - "rand_core 0.3.1", -] - -[[package]] -name = "rdrand" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" -dependencies = [ - "rand_core 0.3.1", + "rand_core", ] [[package]] @@ -1140,12 +955,6 @@ dependencies = [ [[package]] name = "siphasher" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" - -[[package]] -name = "siphasher" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fa8f3741c7372e75519bd9346068370c9cdaabcc1f9599cbcf2a2719352286b7" @@ -1188,7 +997,7 @@ checksum = "2940c75beb4e3bf3a494cef919a747a2cb81e52571e212bfbd185074add7208a" dependencies = [ "lazy_static", "new_debug_unreachable", - "phf_shared 0.8.0", + "phf_shared", "precomputed-hash", "serde", ] @@ -1199,8 +1008,8 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f24c8e5e19d22a726626f1a5e16fe15b132dcf21d10177fa5a45ce7962996b97" dependencies = [ - "phf_generator 0.8.0", - "phf_shared 0.8.0", + "phf_generator", + "phf_shared", "proc-macro2", "quote", ] @@ -1235,7 +1044,7 @@ checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" dependencies = [ "cfg-if", "libc", - "rand 0.7.3", + "rand", "redox_syscall", "remove_dir_all", "winapi 0.3.8", @@ -1283,16 +1092,6 @@ dependencies = [ ] [[package]] -name = "timeago" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aff2f3f1ac92d664adfdea85496dceb8c044f66d62e7d953a059023385967cfc" -dependencies = [ - "chrono", - "isolang", -] - -[[package]] name = "tinystr" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -23,7 +23,6 @@ ammonia = "3.1.0" urlencoding = "1.1.1" http = "0.2.1" chrono = "0.4.13" -timeago = "0.2.1" fluent-langneg = "0.13.0" fluent = "0.12.0" lazy_static = "1.4.0" diff --git a/res/lang/en.flt b/res/lang/en.flt index 51b9d78..34430d4 100644 --- a/res/lang/en.flt +++ b/res/lang/en.flt @@ -53,6 +53,43 @@ reply_submit = Reply submit = Submit submitted = Submitted text_with_markdown = Text (markdown supported) +timeago_years = + { $years } { $years -> + [one] year + *[other] years + } ago +timeago_months = + { $months } { $months -> + [one] month + *[other] months + } ago +timeago_weeks = + { $weeks } { $weeks -> + [one] week + *[other] weeks + } ago +timeago_days = + { $days } { $days -> + [one] day + *[other] days + } ago +timeago_hours = + { $hours } { $hours -> + [one] hour + *[other] hours + } ago +timeago_minutes = + { $minutes } { $minutes -> + [one] minute + *[other] minutes + } ago +timeago_seconds = + { $seconds } { $seconds -> + [one] second + *[other] seconds + } ago +timeago_now = now +timeago_future = in the future title = Title to = to url = URL diff --git a/res/lang/eo.flt b/res/lang/eo.flt index fe01c53..cd526e9 100644 --- a/res/lang/eo.flt +++ b/res/lang/eo.flt @@ -53,6 +53,43 @@ reply_submit = Respondi submit = Sendi submitted = Afiŝita text_with_markdown = Teksto (markdown estas permesita) +timeago_years = + antaŭ { $years } { $years -> + [one] jaro + *[other] jaroj + } +timeago_months = + antaŭ { $months } { $months -> + [one] monato + *[other] monatoj + } +timeago_weeks = + antaŭ{ $weeks } { $weeks -> + [one] semajno + *[other] semajnoj + } +timeago_days = + antaŭ { $days } { $days -> + [one] tago + *[other] tagoj + } +timeago_hours = + antaŭ { $hours } { $hours -> + [one] horo + *[other] horoj + } +timeago_minutes = + antaŭ { $minutes } { $minutes -> + [one] minuto + *[other] minutoj + } +timeago_seconds = + antaŭ { $seconds } { $seconds -> + [one] sekundo + *[other] sekundoj + } +timeago_now = nune +timeago_future = estontece title = Titolo to = al url = URL diff --git a/src/components/mod.rs b/src/components/mod.rs index b43b43b..b7a3b39 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -1,3 +1,5 @@ +pub mod timeago; + use std::borrow::{Borrow, Cow}; use std::collections::HashMap; @@ -8,6 +10,8 @@ use crate::resp_types::{ use crate::util::{abbreviate_link, author_is_me}; use crate::PageBaseData; +pub use timeago::TimeAgo; + #[render::component] pub fn Comment<'a>( comment: &'a RespPostCommentInfo<'a>, @@ -19,7 +23,7 @@ pub fn Comment<'a>( <small> <cite><UserLink user={comment.author.as_ref()} /></cite> {" "} - <TimeAgo since={chrono::DateTime::parse_from_rfc3339(&comment.created).unwrap()} /> + <TimeAgo since={chrono::DateTime::parse_from_rfc3339(&comment.created).unwrap()} lang /> </small> <Content src={comment} /> <div class={"actionList"}> @@ -409,12 +413,3 @@ pub fn BoolSubmitButton<'a>(value: bool, do_text: &'a str, done_text: &'a str) { } } } - -#[render::component] -pub fn TimeAgo(since: chrono::DateTime<chrono::offset::FixedOffset>) { - let since_str = since.to_rfc3339(); - let text = timeago::Formatter::new().convert_chrono(since, chrono::offset::Utc::now()); - render::rsx! { - <span title={since_str}>{text}</span> - } -} diff --git a/src/components/timeago.rs b/src/components/timeago.rs new file mode 100644 index 0000000..54dcb45 --- /dev/null +++ b/src/components/timeago.rs @@ -0,0 +1,70 @@ +#[render::component] +pub fn TimeAgo<'a>( + since: chrono::DateTime<chrono::offset::FixedOffset>, + lang: &'a crate::Translator, +) { + let since_str = since.to_rfc3339(); + + let duration = chrono::offset::Utc::now().signed_duration_since(since); + + let (key, args) = { + let weeks = duration.num_weeks(); + if weeks > 52 { + let years = ((weeks as f32) / 52.18).floor() as u32; + ( + "timeago_years", + Some(fluent::fluent_args!["years" => years]), + ) + } else if weeks > 5 { + let months = (f32::from(weeks as i8) / 4.35).floor() as u8; + ( + "timeago_months", + Some(fluent::fluent_args!["months" => months]), + ) + } else if weeks > 0 { + ( + "timeago_weeks", + Some(fluent::fluent_args!["weeks" => weeks]), + ) + } else { + let days = duration.num_days(); + if days > 0 { + ("timeago_days", Some(fluent::fluent_args!["days" => days])) + } else { + let hours = duration.num_hours(); + if hours > 0 { + ( + "timeago_hours", + Some(fluent::fluent_args!["hours" => hours]), + ) + } else { + let minutes = duration.num_minutes(); + if minutes > 0 { + ( + "timeago_minutes", + Some(fluent::fluent_args!["minutes" => minutes]), + ) + } else { + let seconds = duration.num_seconds(); + + if seconds > 0 { + ( + "timeago_seconds", + Some(fluent::fluent_args!["seconds" => seconds]), + ) + } else if seconds < 0 { + ("timeago_future", None) + } else { + ("timeago_now", None) + } + } + } + } + } + }; + let text = lang.tr(key, args.as_ref()).into_owned(); + + render::rsx! { + <span title={since_str}>{text}</span> + } +} diff --git a/src/main.rs b/src/main.rs index d513f20..91fc487 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,17 +75,23 @@ pub struct Translator { bundle: fluent::concurrent::FluentBundle<&'static fluent::FluentResource>, } impl Translator { - pub fn tr<'a>(&'a self, key: &str, args: Option<&'a fluent::FluentArgs>) -> Cow<'a, str> { + pub fn tr<'a>( + &'a self, + key: &'static str, + args: Option<&'a fluent::FluentArgs>, + ) -> Cow<'a, str> { + let pattern = self.bundle.get_message(key).and_then(|msg| msg.value); + + let pattern = match pattern { + Some(pattern) => pattern, + None => { + eprintln!("Missing message in translation: {}", key); + return Cow::Borrowed(key); + } + }; + let mut errors = Vec::with_capacity(0); - let out = self.bundle.format_pattern( - self.bundle - .get_message(key) - .expect("Missing message in translation") - .value - .expect("Missing value for translation key"), - args, - &mut errors, - ); + let out = self.bundle.format_pattern(pattern, args, &mut errors); if !errors.is_empty() { eprintln!("Errors in translation: {:?}", errors); } diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 0f47720..feaf0da 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -74,7 +74,7 @@ async fn page_post( </p> <p> {lang.tr("submitted", None)} - {" "}<TimeAgo since={chrono::DateTime::parse_from_rfc3339(&post.as_ref().created)?} /> + {" "}<TimeAgo since={chrono::DateTime::parse_from_rfc3339(&post.as_ref().created)?} lang={&lang} /> {" "}{lang.tr("by", None)}{" "}<UserLink user={post.as_ref().author.as_ref()} /> {" "}{lang.tr("to", None)}{" "}<CommunityLink community={&post.as_ref().community} /> </p> |