macOS: Add support for 12/24-hour time display preferences

This commit is contained in:
Jamie Kyle 2023-07-31 09:23:19 -07:00 committed by GitHub
parent 88858af144
commit 1143c0e9ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 208 additions and 14 deletions

View file

@ -6,6 +6,7 @@ import { stub } from 'sinon';
import * as LocaleMatcher from '@formatjs/intl-localematcher';
import { load, _getAvailableLocales } from '../../../app/locale';
import { FAKE_DEFAULT_LOCALE } from '../../../app/spell_check';
import { HourCyclePreference } from '../../types/I18N';
describe('locale', async () => {
describe('load', () => {
@ -23,7 +24,11 @@ describe('locale', async () => {
preferredSystemLocales: Array<string>,
expectedLocale: string
) {
const actualLocale = await load({ preferredSystemLocales, logger });
const actualLocale = await load({
preferredSystemLocales,
hourCyclePreference: HourCyclePreference.UnknownPreference,
logger,
});
assert.strictEqual(actualLocale.name, expectedLocale);
}

View file

@ -9,6 +9,7 @@ import type { CreateTemplateOptionsType } from '../../../app/menu';
import { createTemplate } from '../../../app/menu';
import { load as loadLocale } from '../../../app/locale';
import type { MenuListType } from '../../types/menu';
import { HourCyclePreference } from '../../types/I18N';
const forceUpdate = stub();
const openArtCreator = stub();
@ -198,6 +199,7 @@ const PLATFORMS = [
describe('createTemplate', () => {
const { i18n } = loadLocale({
preferredSystemLocales: ['en'],
hourCyclePreference: HourCyclePreference.UnknownPreference,
logger: {
fatal: stub().throwsArg(0),
error: stub().throwsArg(0),

View file

@ -0,0 +1,63 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import * as sinon from 'sinon';
import { formatTimestamp } from '../../util/formatTimestamp';
import { HourCyclePreference } from '../../types/I18N';
const min = new Date(2023, 0, 1, 0).getTime();
const max = new Date(2023, 0, 1, 23).getTime();
describe('formatTimestamp', () => {
let sandbox: sinon.SinonSandbox;
let localesStub: sinon.SinonStub;
let hourCycleStub: sinon.SinonStub;
beforeEach(() => {
sandbox = sinon.createSandbox();
localesStub = sandbox.stub(window, 'getPreferredSystemLocales');
hourCycleStub = sandbox.stub(window, 'getHourCyclePreference');
});
afterEach(() => {
sandbox.restore();
});
function testCase(
locale: string,
preference: HourCyclePreference,
time: number,
expected: string
) {
const timeFmt = new Intl.DateTimeFormat('en', {
timeStyle: 'medium',
}).format(time);
it(`should format with locale: ${locale} (${HourCyclePreference[preference]}) @ ${timeFmt})`, () => {
localesStub.returns([locale]);
hourCycleStub.returns(preference);
assert.equal(formatTimestamp(time, { timeStyle: 'medium' }), expected);
});
}
testCase('en', HourCyclePreference.UnknownPreference, min, '12:00:00 AM');
testCase('en', HourCyclePreference.UnknownPreference, max, '11:00:00 PM');
testCase('en', HourCyclePreference.Prefer12, min, '12:00:00 AM');
testCase('en', HourCyclePreference.Prefer12, max, '11:00:00 PM');
testCase('en', HourCyclePreference.Prefer24, min, '00:00:00');
testCase('en', HourCyclePreference.Prefer24, max, '23:00:00');
testCase('nb', HourCyclePreference.UnknownPreference, min, '00:00:00');
testCase('nb', HourCyclePreference.UnknownPreference, max, '23:00:00');
testCase('nb', HourCyclePreference.Prefer12, min, '12:00:00 a.m.');
testCase('nb', HourCyclePreference.Prefer12, max, '11:00:00 p.m.');
testCase('nb', HourCyclePreference.Prefer24, min, '00:00:00');
testCase('nb', HourCyclePreference.Prefer24, max, '23:00:00');
testCase('ja', HourCyclePreference.UnknownPreference, min, '0:00:00');
testCase('ja', HourCyclePreference.UnknownPreference, max, '23:00:00');
testCase('ja', HourCyclePreference.Prefer12, min, '午前0:00:00');
testCase('ja', HourCyclePreference.Prefer12, max, '午後11:00:00');
testCase('ja', HourCyclePreference.Prefer24, min, '0:00:00');
testCase('ja', HourCyclePreference.Prefer24, max, '23:00:00');
});