From 665d2965b7e1b67c0fe189b37e7145a7f1541ac2 Mon Sep 17 00:00:00 2001 From: yiyiyimu Date: Wed, 27 Oct 2021 16:23:13 -0400 Subject: [PATCH] zh: add special plurlized words Signed-off-by: yiyiyimu --- arrow/locales.py | 23 +++++++++++++++++++++++ tests/test_locales.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/arrow/locales.py b/arrow/locales.py index d6d5c486..f6cfc49a 100644 --- a/arrow/locales.py +++ b/arrow/locales.py @@ -967,6 +967,15 @@ class ChineseCNLocale(Locale): "years": "{0}年", } + special_dayframes = { + -2: "前天", + -1: "昨天", + 1: "明天", + 2: "后天", + } + + special_yearframes = {-2: "前年", -1: "去年", 1: "明年", 2: "后年"} + month_names = [ "", "一月", @@ -1001,6 +1010,20 @@ class ChineseCNLocale(Locale): day_names = ["", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"] day_abbreviations = ["", "一", "二", "三", "四", "五", "六", "日"] + def _format_relative( + self, humanized: str, timeframe: TimeFrameLiteral, delta: Union[float, int] + ) -> str: + if timeframe in ("day", "days"): + special = self.special_dayframes.get(int(delta)) + if special: + return special + elif timeframe in ("year", "years"): + special = self.special_yearframes.get(int(delta)) + if special: + return special + + return super()._format_relative(humanized, timeframe, delta) + class ChineseTWLocale(Locale): diff --git a/tests/test_locales.py b/tests/test_locales.py index 04a9a452..d72c90c3 100644 --- a/tests/test_locales.py +++ b/tests/test_locales.py @@ -1689,6 +1689,43 @@ def test_format_timeframe(self): assert self.locale._format_timeframe("year", 1) == "1年" assert self.locale._format_timeframe("years", 12) == "12年" + def test_format_relative(self): + assert self.locale._format_relative("刚才", "now", 0) == "刚才" + + assert self.locale._format_relative("1秒", "second", 1) == "1秒后" + assert self.locale._format_relative("2秒", "seconds", 2) == "2秒后" + assert self.locale._format_relative("1分钟", "minute", 1) == "1分钟后" + assert self.locale._format_relative("2分钟", "minutes", 2) == "2分钟后" + assert self.locale._format_relative("1小时", "hour", 1) == "1小时后" + assert self.locale._format_relative("2小时", "hours", 2) == "2小时后" + assert self.locale._format_relative("1天", "day", 1) == "明天" + assert self.locale._format_relative("2天", "days", 2) == "后天" + assert self.locale._format_relative("3天", "days", 3) == "3天后" + assert self.locale._format_relative("1周", "week", 1) == "1周后" + assert self.locale._format_relative("2周", "weeks", 2) == "2周后" + assert self.locale._format_relative("1个月", "month", 1) == "1个月后" + assert self.locale._format_relative("2个月", "months", 2) == "2个月后" + assert self.locale._format_relative("1年", "year", 1) == "明年" + assert self.locale._format_relative("2年", "years", 2) == "后年" + assert self.locale._format_relative("3年", "years", 3) == "3年后" + + assert self.locale._format_relative("1秒", "second", -1) == "1秒前" + assert self.locale._format_relative("2秒", "seconds", -2) == "2秒前" + assert self.locale._format_relative("1分钟", "minute", -1) == "1分钟前" + assert self.locale._format_relative("2分钟", "minutes", -2) == "2分钟前" + assert self.locale._format_relative("1小时", "hour", -1) == "1小时前" + assert self.locale._format_relative("2小时", "hours", -2) == "2小时前" + assert self.locale._format_relative("1天", "day", -1) == "昨天" + assert self.locale._format_relative("2天", "days", -2) == "前天" + assert self.locale._format_relative("3天", "days", -3) == "3天前" + assert self.locale._format_relative("1周", "week", -1) == "1周前" + assert self.locale._format_relative("2周", "weeks", -2) == "2周前" + assert self.locale._format_relative("1个月", "month", -1) == "1个月前" + assert self.locale._format_relative("2个月", "months", -2) == "2个月前" + assert self.locale._format_relative("1年", "year", -1) == "去年" + assert self.locale._format_relative("2年", "years", -2) == "前年" + assert self.locale._format_relative("3年", "years", -3) == "3年前" + @pytest.mark.usefixtures("lang_locale") class TestSwahiliLocale: