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

getMeridiemText() from moment utils now returns translated text #478

Merged
merged 5 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions __tests__/localization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ describe("Moment -- localization", () => {
expect(momentUtils.getCurrentLocaleCode()).toBe("ru");
});

it("getMeridiemText: returns translated AM/PM format on available locales.", () => {
expect(momentUtils.getMeridiemText("am")).toBe("AM");
expect(momentUtils.getMeridiemText("pm")).toBe("PM");
});

it("parse: should parse localized dates", () => {
const format = "ddd-MMMM-DD-YYYY";
const dateString = "вт-октябрь-30-2018";
Expand All @@ -129,6 +134,19 @@ describe("Moment -- localization", () => {
});
});

describe("Korean", () => {
const momentUtils = new MomentUtils({ locale: "ko" });

beforeEach(() => {
moment.locale("ko");
});

it("getMeridiemText: returns translated AM/PM format on available locales.", () => {
expect(momentUtils.getMeridiemText("am")).toBe("오전");
expect(momentUtils.getMeridiemText("pm")).toBe("오후");
});
});

describe("English", () => {
const momentUtils = new MomentUtils({ locale: "en" });
const date = momentUtils.date(TEST_TIMESTAMP);
Expand Down
7 changes: 6 additions & 1 deletion packages/moment/src/moment-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,12 @@ export default class MomentUtils implements IUtils<defaultMoment.Moment> {
}

public getMeridiemText(ampm: "am" | "pm") {
return ampm === "am" ? "AM" : "PM";
if (this.is12HourCycleInCurrentLocale()) {
// AM/PM translation only possible in those who have 12 hour cycle in locale.
return this.moment.localeData().meridiem(ampm === "am" ? 0 : 13, 0, false);
}

return ampm === "am" ? "AM" : "PM"; // fallback for de, ru, ...etc
}

public startOfMonth(date: Moment) {
Expand Down