From d0691028223e8c4a93e554b9670cdd3b160613a7 Mon Sep 17 00:00:00 2001 From: Toru Nayuki Date: Thu, 2 Jul 2026 19:31:38 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20drop=20word=20spacing=20in=20Japanese=20?= =?UTF-8?q?("5=20=E5=88=86=20=E5=89=8D"=20->=20"5=E5=88=86=E5=89=8D")?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/languages/japanese.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/languages/japanese.rs b/src/languages/japanese.rs index 3f68e46..3f49869 100644 --- a/src/languages/japanese.rs +++ b/src/languages/japanese.rs @@ -30,4 +30,43 @@ impl Language for Japanese { Years => "年", } } + // Japanese doesn't separate words with spaces, so drop the default " " + // between the value, the unit, and the trailing "前": "5 分 前" -> "5分前". + fn between_value_and_word(&self) -> &str { + "" + } + fn between_chunks(&self) -> &str { + "" + } + fn override_space_near_ago(&self) -> &str { + "" + } +} + +#[test] +fn test() { + use super::super::Formatter; + use std::time::Duration; + + fn test_with_formatter(mut f: Formatter) { + f.min_unit(TimeUnit::Seconds); + assert_eq!(f.convert(Duration::from_secs(0)), "今"); + assert_eq!(f.convert(Duration::from_nanos(42)), "今"); + assert_eq!(f.convert(Duration::from_micros(42)), "今"); + assert_eq!(f.convert(Duration::from_millis(42)), "今"); + assert_eq!(f.convert(Duration::from_secs(42)), "42秒前"); + assert_eq!(f.convert(Duration::from_mins(42)), "42分前"); + assert_eq!(f.convert(Duration::from_hours(2)), "2時間前"); + assert_eq!(f.convert(Duration::from_hours(24)), "1日前"); + assert_eq!(f.convert(Duration::from_hours(7 * 24)), "1週間前"); + + f.num_items(2); + assert_eq!(f.convert(Duration::from_secs(3600 + 42)), "1時間42秒前"); + + f.min_unit(TimeUnit::Nanoseconds); + assert_eq!(f.convert(Duration::from_nanos(42)), "42ナノ秒前"); + } + + test_with_formatter(Formatter::with_language(Japanese)); + test_with_formatter(Formatter::with_language(Japanese.clone_boxed())); }