Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zh: add special plurlized words #1059

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions arrow/locales.py
Expand Up @@ -976,6 +976,15 @@ class ChineseCNLocale(Locale):
"years": "{0}年",
}

special_dayframes = {
-2: "前天",
-1: "昨天",
1: "明天",
2: "后天",
}

special_yearframes = {-2: "前年", -1: "去年", 1: "明年", 2: "后年"}

month_names = [
"",
"一月",
Expand Down Expand Up @@ -1010,6 +1019,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):

Expand Down
37 changes: 37 additions & 0 deletions tests/test_locales.py
Expand Up @@ -2058,6 +2058,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:
Expand Down