Skip to content

Commit

Permalink
Merge pull request openvanilla#98 from xatier/calendar-jp
Browse files Browse the repository at this point in the history
feat(macro): add Japanese calendar macro support
  • Loading branch information
lukhnos authored Dec 17, 2023
2 parents 2bc1b0a + 1cdd12b commit aae09c4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 24 deletions.
3 changes: 3 additions & 0 deletions data/data.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16771,6 +16771,7 @@ _punctuation_~ ~ 0.0
ㄇㄧㄥˊ-ㄊㄧㄢ-ㄖˋ-ㄑㄧˊ MACRO@DATE_TOMORROW_MEDIUM -8
ㄇㄧㄥˊ-ㄊㄧㄢ-ㄖˋ-ㄑㄧˊ MACRO@DATE_TOMORROW_MEDIUM_ROC -8
ㄇㄧㄥˊ-ㄊㄧㄢ-ㄖˋ-ㄑㄧˊ MACRO@DATE_TOMORROW_MEDIUM_CHINESE -8
ㄇㄧㄥˊ-ㄊㄧㄢ-ㄖˋ-ㄑㄧˊ MACRO@DATE_TOMORROW_MEDIUM_JAPANESE -8
ㄇㄧㄥˊ-ㄊㄨㄛˊ 明駝 -6.78951856
ㄇㄧㄥˊ-ㄋㄧˊ-ㄙㄨ-ㄉㄚˊ 明尼蘇達 -5.67803398
ㄇㄧㄥˊ-ㄋㄧˊ-ㄙㄨ-ㄉㄚˊ-ㄉㄚˋ-ㄒㄩㄝˊ 明尼蘇達大學 -6.99251813
Expand Down Expand Up @@ -77405,6 +77406,7 @@ _punctuation_~ ~ 0.0
ㄐㄧㄣ-ㄊㄧㄢ-ㄖˋ-ㄑㄧˊ MACRO@DATE_TODAY_MEDIUM -8
ㄐㄧㄣ-ㄊㄧㄢ-ㄖˋ-ㄑㄧˊ MACRO@DATE_TODAY_MEDIUM_ROC -8
ㄐㄧㄣ-ㄊㄧㄢ-ㄖˋ-ㄑㄧˊ MACRO@DATE_TODAY_MEDIUM_CHINESE -8
ㄐㄧㄣ-ㄊㄧㄢ-ㄖˋ-ㄑㄧˊ MACRO@DATE_TODAY_MEDIUM_JAPANESE -8
ㄐㄧㄣ-ㄊㄧㄢ-ㄗㄞˋ 今天在 -4.63713047
ㄐㄧㄣ-ㄊㄧㄢ-ㄧㄡˇ 今天有 -4.97056355
ㄐㄧㄣ-ㄊㄧㄢˊ-ㄑㄧˇ-ㄧˋ 金田起義 -7.28009397
Expand Down Expand Up @@ -125761,6 +125763,7 @@ _punctuation_~ ~ 0.0
ㄗㄨㄛˊ-ㄊㄧㄢ-ㄖˋ-ㄑㄧˊ MACRO@DATE_YESTERDAY_MEDIUM -8
ㄗㄨㄛˊ-ㄊㄧㄢ-ㄖˋ-ㄑㄧˊ MACRO@DATE_YESTERDAY_MEDIUM_ROC -8
ㄗㄨㄛˊ-ㄊㄧㄢ-ㄖˋ-ㄑㄧˊ MACRO@DATE_YESTERDAY_MEDIUM_CHINESE -8
ㄗㄨㄛˊ-ㄊㄧㄢ-ㄖˋ-ㄑㄧˊ MACRO@DATE_YESTERDAY_MEDIUM_JAPANESE -8
ㄗㄨㄛˊ-ㄊㄧㄢ-ㄗㄠˇ-ㄕㄤˋ 昨天早上 -6.20091272
ㄗㄨㄛˊ-ㄊㄧㄢ-ㄧㄝˋ-ㄌㄧˇ 昨天夜裡 -6.97906398
ㄗㄨㄛˊ-ㄊㄧㄢ-ㄨㄢˇ-ㄕㄤˋ 昨天晚上 -5.16615062
Expand Down
84 changes: 60 additions & 24 deletions src/InputMacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,139 +4,158 @@
#include <unicode/smpdtfmt.h>

#include <iostream>
#include <utility>
#include <vector>

namespace McBopomofo {

class InputMacroDateTodayShort : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_TODAY_SHORT"; };
std::string name() const { return "MACRO@DATE_TODAY_SHORT"; }
std::string replacement() const;
};

class InputMacroDateTodayMedium : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_TODAY_MEDIUM"; };
std::string name() const { return "MACRO@DATE_TODAY_MEDIUM"; }
std::string replacement() const;
};

class InputMacroDateTodayMediumRoc : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_TODAY_MEDIUM_ROC"; };
std::string name() const { return "MACRO@DATE_TODAY_MEDIUM_ROC"; }
std::string replacement() const;
};

class InputMacroDateTodayMediumChinese : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_TODAY_MEDIUM_CHINESE"; };
std::string name() const { return "MACRO@DATE_TODAY_MEDIUM_CHINESE"; }
std::string replacement() const;
};

class InputMacroDateTodayMediumJapanese : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_TODAY_MEDIUM_JAPANESE"; }
std::string replacement() const;
};

class InputMacroDateYesterdayShort : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_YESTERDAY_SHORT"; };
std::string name() const { return "MACRO@DATE_YESTERDAY_SHORT"; }
std::string replacement() const;
};

class InputMacroDateYesterdayMedium : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_YESTERDAY_MEDIUM"; };
std::string name() const { return "MACRO@DATE_YESTERDAY_MEDIUM"; }
std::string replacement() const;
};

class InputMacroDateYesterdayMediumRoc : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_YESTERDAY_MEDIUM_ROC"; };
std::string name() const { return "MACRO@DATE_YESTERDAY_MEDIUM_ROC"; }
std::string replacement() const;
};

class InputMacroDateYesterdayMediumChinese : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_YESTERDAY_MEDIUM_CHINESE"; };
std::string name() const { return "MACRO@DATE_YESTERDAY_MEDIUM_CHINESE"; }
std::string replacement() const;
};

class InputMacroDateYesterdayMediumJapanese : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_YESTERDAY_MEDIUM_JAPANESE"; }
std::string replacement() const;
};

class InputMacroDateTomorrowShort : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_TOMORROW_SHORT"; };
std::string name() const { return "MACRO@DATE_TOMORROW_SHORT"; }
std::string replacement() const;
};

class InputMacroDateTomorrowMedium : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_TOMORROW_MEDIUM"; };
std::string name() const { return "MACRO@DATE_TOMORROW_MEDIUM"; }
std::string replacement() const;
};

class InputMacroDateTomorrowMediumRoc : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_TOMORROW_MEDIUM_ROC"; };
std::string name() const { return "MACRO@DATE_TOMORROW_MEDIUM_ROC"; }
std::string replacement() const;
};

class InputMacroDateTomorrowMediumChinese : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_TOMORROW_MEDIUM_CHINESE"; };
std::string name() const { return "MACRO@DATE_TOMORROW_MEDIUM_CHINESE"; }
std::string replacement() const;
};

class InputMacroDateTomorrowMediumJapanese : public InputMacro {
public:
std::string name() const { return "MACRO@DATE_TOMORROW_MEDIUM_JAPANESE"; }
std::string replacement() const;
};

class InputMacroDateTimeNowShort : public InputMacro {
public:
std::string name() const { return "MACRO@TIME_NOW_SHORT"; };
std::string name() const { return "MACRO@TIME_NOW_SHORT"; }
std::string replacement() const;
};

class InputMacroDateTimeNowMedium : public InputMacro {
public:
std::string name() const { return "MACRO@TIME_NOW_MEDIUM"; };
std::string name() const { return "MACRO@TIME_NOW_MEDIUM"; }
std::string replacement() const;
};

class InputMacroTimeZoneStandard : public InputMacro {
public:
std::string name() const { return "MACRO@TIMEZONE_STANDARD"; };
std::string name() const { return "MACRO@TIMEZONE_STANDARD"; }
std::string replacement() const;
};

class InputMacroTimeZoneShortGeneric : public InputMacro {
public:
std::string name() const { return "MACRO@TIMEZONE_GENERIC_SHORT"; };
std::string name() const { return "MACRO@TIMEZONE_GENERIC_SHORT"; }
std::string replacement() const;
};

class InputMacroThisYearGanZhi : public InputMacro {
public:
std::string name() const { return "MACRO@THIS_YEAR_GANZHI"; };
std::string name() const { return "MACRO@THIS_YEAR_GANZHI"; }
std::string replacement() const;
};

class InputMacroLastYearGanZhi : public InputMacro {
public:
std::string name() const { return "MACRO@LAST_YEAR_GANZHI"; };
std::string name() const { return "MACRO@LAST_YEAR_GANZHI"; }
std::string replacement() const;
};

class InputMacroNextYearGanZhi : public InputMacro {
public:
std::string name() const { return "MACRO@NEXT_YEAR_GANZHI"; };
std::string name() const { return "MACRO@NEXT_YEAR_GANZHI"; }
std::string replacement() const;
};

class InputMacroThisYearChineseZodiac : public InputMacro {
public:
std::string name() const { return "MACRO@THIS_YEAR_CHINESE_ZODIAC"; };
std::string name() const { return "MACRO@THIS_YEAR_CHINESE_ZODIAC"; }
std::string replacement() const;
};

class InputMacroLastYearChineseZodiac : public InputMacro {
public:
std::string name() const { return "MACRO@LAST_YEAR_CHINESE_ZODIAC"; };
std::string name() const { return "MACRO@LAST_YEAR_CHINESE_ZODIAC"; }
std::string replacement() const;
};

class InputMacroNextYearChineseZodiac : public InputMacro {
public:
std::string name() const { return "MACRO@NEXT_YEAR_CHINESE_ZODIAC"; };
std::string name() const { return "MACRO@NEXT_YEAR_CHINESE_ZODIAC"; }
std::string replacement() const;
};

Expand All @@ -151,14 +170,17 @@ InputMacroController::InputMacroController() {
AddMacro(macros_, std::make_unique<InputMacroDateTodayMedium>());
AddMacro(macros_, std::make_unique<InputMacroDateTodayMediumRoc>());
AddMacro(macros_, std::make_unique<InputMacroDateTodayMediumChinese>());
AddMacro(macros_, std::make_unique<InputMacroDateTodayMediumJapanese>());
AddMacro(macros_, std::make_unique<InputMacroDateYesterdayShort>());
AddMacro(macros_, std::make_unique<InputMacroDateYesterdayMedium>());
AddMacro(macros_, std::make_unique<InputMacroDateYesterdayMediumRoc>());
AddMacro(macros_, std::make_unique<InputMacroDateYesterdayMediumChinese>());
AddMacro(macros_, std::make_unique<InputMacroDateYesterdayMediumJapanese>());
AddMacro(macros_, std::make_unique<InputMacroDateTomorrowShort>());
AddMacro(macros_, std::make_unique<InputMacroDateTomorrowMedium>());
AddMacro(macros_, std::make_unique<InputMacroDateTomorrowMediumRoc>());
AddMacro(macros_, std::make_unique<InputMacroDateTomorrowMediumChinese>());
AddMacro(macros_, std::make_unique<InputMacroDateTomorrowMediumJapanese>());
AddMacro(macros_, std::make_unique<InputMacroDateTimeNowShort>());
AddMacro(macros_, std::make_unique<InputMacroDateTimeNowMedium>());
AddMacro(macros_, std::make_unique<InputMacroTimeZoneStandard>());
Expand All @@ -185,8 +207,8 @@ std::string formatDate(std::string calendarName, int DayOffset,
icu::DateFormat::EStyle dateStyle) {
UErrorCode status = U_ZERO_ERROR;
icu::TimeZone* timezone = icu::TimeZone::createDefault();
std::string calendarNameBase = "zh_Hant_TW";
if (calendarName.empty() == false) {
std::string calendarNameBase = calendarName == "japanese" ? "ja_JP" : "zh_Hant_TW";
if (!calendarName.empty()) {
calendarNameBase += "@calendar=" + calendarName;
}

Expand Down Expand Up @@ -224,6 +246,10 @@ std::string InputMacroDateTodayMediumChinese::replacement() const {
return formatDate("chinese", 0, icu::DateFormat::EStyle::kMedium);
}

std::string InputMacroDateTodayMediumJapanese::replacement() const {
return formatDate("japanese", 0, icu::DateFormat::EStyle::kMedium);
}

std::string InputMacroDateYesterdayShort::replacement() const {
return formatDate("", -1, icu::DateFormat::EStyle::kShort);
}
Expand All @@ -240,6 +266,10 @@ std::string InputMacroDateYesterdayMediumChinese::replacement() const {
return formatDate("chinese", -1, icu::DateFormat::EStyle::kMedium);
}

std::string InputMacroDateYesterdayMediumJapanese::replacement() const {
return formatDate("japanese", -1, icu::DateFormat::EStyle::kMedium);
}

std::string InputMacroDateTomorrowShort::replacement() const {
return formatDate("", 1, icu::DateFormat::EStyle::kShort);
}
Expand All @@ -256,6 +286,10 @@ std::string InputMacroDateTomorrowMediumChinese::replacement() const {
return formatDate("chinese", 1, icu::DateFormat::EStyle::kMedium);
}

std::string InputMacroDateTomorrowMediumJapanese::replacement() const {
return formatDate("japanese", 1, icu::DateFormat::EStyle::kMedium);
}

std::string formatTime(icu::DateFormat::EStyle timeStyle) {
UErrorCode status = U_ZERO_ERROR;
icu::TimeZone* timezone = icu::TimeZone::createDefault();
Expand Down Expand Up @@ -312,6 +346,7 @@ int currentYear() {
return year;
}

// NOLINTBEGIN(readability-magic-numbers)
int getYearBase(int year) {
if (year < 4) {
year = year * -1;
Expand Down Expand Up @@ -341,6 +376,7 @@ std::string chineseZodiac(int year) {
int zhiIndex = base % 12;
return gan[ganIndex] + zhi[zhiIndex] + "";
}
// NOLINTEND(readability-magic-numbers)

std::string InputMacroThisYearGanZhi::replacement() const {
int year = currentYear();
Expand Down

0 comments on commit aae09c4

Please sign in to comment.