Fix timestamps/spellcheck to use locale override

This commit is contained in:
Jamie Kyle 2023-11-06 17:02:13 -08:00 committed by GitHub
parent 59fa75c309
commit fd8691b871
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 45 additions and 11 deletions

View file

@ -110,6 +110,7 @@ window.SignalContext = {
getHourCyclePreference: () => HourCyclePreference.UnknownPreference,
getPreferredSystemLocales: () => ['en'],
getResolvedMessagesLocaleDirection: () => 'ltr',
getLocaleOverride: () => null,
};
window.i18n = i18n;

View file

@ -143,6 +143,7 @@ const consoleLogger = createBufferedConsoleLogger();
// These will be set after app fires the 'ready' event
let logger: LoggerType | undefined;
let preferredSystemLocales: Array<string> | undefined;
let localeOverride: string | null | undefined;
let resolvedTranslationsLocale: LocaleType | undefined;
let settingsChannel: SettingsChannel | undefined;
@ -431,6 +432,13 @@ function getPreferredSystemLocales(): Array<string> {
return preferredSystemLocales;
}
function getLocaleOverride(): string | null {
if (typeof localeOverride === 'undefined') {
throw new Error('getLocaleOverride: Locale not yet initialized!');
}
return localeOverride;
}
function getResolvedMessagesLocale(): LocaleType {
if (!resolvedTranslationsLocale) {
throw new Error('getResolvedMessagesLocale: Locale not yet initialized!');
@ -824,6 +832,7 @@ async function createWindow() {
setupSpellChecker(
mainWindow,
getPreferredSystemLocales(),
getLocaleOverride(),
getResolvedMessagesLocale().i18n,
getLogger()
);
@ -1809,7 +1818,7 @@ app.on('ready', async () => {
loadPreferredSystemLocales()
);
const localeOverride = await getLocaleOverrideSetting();
localeOverride = await getLocaleOverrideSetting();
const hourCyclePreference = getHourCyclePreference();
logger.info(`app.ready: hour cycle preference: ${hourCyclePreference}`);
@ -2400,6 +2409,7 @@ ipc.on('get-config', async event => {
resolvedTranslationsLocaleDirection: getResolvedMessagesLocale().direction,
hourCyclePreference: getResolvedMessagesLocale().hourCyclePreference,
preferredSystemLocales: getPreferredSystemLocales(),
localeOverride: getLocaleOverride(),
version: app.getVersion(),
buildCreation: config.get<number>('buildCreation'),
buildExpiration: config.get<number>('buildExpiration'),

View file

@ -56,6 +56,7 @@ export function getLanguages(
export const setup = (
browserWindow: BrowserWindow,
preferredSystemLocales: ReadonlyArray<string>,
localeOverride: string | null,
i18n: LocalizerType,
logger: LoggerType
): void => {
@ -74,13 +75,16 @@ export const setup = (
logger.info('spellcheck: dictionary initialized:', lang);
});
// Locale override should be combined with other preferences rather than
// replace them entirely.
const combinedLocales =
localeOverride != null
? [localeOverride, ...preferredSystemLocales]
: preferredSystemLocales;
const availableLocales = session.availableSpellCheckerLanguages;
const languages = getLanguages(
preferredSystemLocales,
availableLocales,
'en'
);
console.log('spellcheck: user locales:', preferredSystemLocales);
const languages = getLanguages(combinedLocales, availableLocales, 'en');
console.log('spellcheck: user locales:', combinedLocales);
console.log(
'spellcheck: available spellchecker languages:',
availableLocales

View file

@ -33,6 +33,7 @@ global.window = {
getResolvedMessagesLocaleDirection: () => 'ltr',
getHourCyclePreference: () => HourCyclePreference.UnknownPreference,
getPreferredSystemLocales: () => ['en'],
getLocaleOverride: () => null,
},
i18n: key => `i18n(${key})`,
storage: {

View file

@ -60,3 +60,4 @@ window.textsecure.storage.protocol = window.getSignalProtocolStore();
})();
window.getPreferredSystemLocales = () => ['en'];
window.getLocaleOverride = () => null;

View file

@ -12,6 +12,7 @@ const max = new Date(2023, 0, 1, 23).getTime();
describe('formatTimestamp', () => {
let sandbox: sinon.SinonSandbox;
let localesStub: sinon.SinonStub;
let localeOverrideStub: sinon.SinonStub;
let hourCycleStub: sinon.SinonStub;
beforeEach(() => {
@ -20,6 +21,10 @@ describe('formatTimestamp', () => {
window.SignalContext,
'getPreferredSystemLocales'
);
localeOverrideStub = sandbox.stub(
window.SignalContext,
'getLocaleOverride'
);
hourCycleStub = sandbox.stub(
window.SignalContext,
'getHourCyclePreference'
@ -41,6 +46,7 @@ describe('formatTimestamp', () => {
}).format(time);
it(`should format with locale: ${locale} (${HourCyclePreference[preference]}) @ ${timeFmt})`, () => {
localesStub.returns([locale]);
localeOverrideStub.returns(null);
hourCycleStub.returns(preference);
assert.equal(formatTimestamp(time, { timeStyle: 'medium' }), expected);
});

View file

@ -51,6 +51,7 @@ export const rendererConfigSchema = z.object({
resolvedTranslationsLocaleDirection: z.enum(['ltr', 'rtl']),
hourCyclePreference: HourCyclePreferenceSchema,
preferredSystemLocales: z.array(configRequiredStringSchema),
localeOverride: z.string().nullable(),
name: configRequiredStringSchema,
nodeVersion: configRequiredStringSchema,
proxyUrl: configOptionalStringSchema,

View file

@ -67,7 +67,11 @@ const formatterCache = new Map<string, Intl.DateTimeFormat>();
export function getDateTimeFormatter(
options: Intl.DateTimeFormatOptions
): Intl.DateTimeFormat {
const locales = window.SignalContext.getPreferredSystemLocales();
const preferredSystemLocales =
window.SignalContext.getPreferredSystemLocales();
const localeOverride = window.SignalContext.getLocaleOverride();
const locales =
localeOverride != null ? [localeOverride] : preferredSystemLocales;
const optionsWithPreferences = getOptionsWithPreferences(options);
const cacheKey = getCacheKey(locales, optionsWithPreferences);
const cachedFormatter = formatterCache.get(cacheKey);

View file

@ -48,6 +48,7 @@ export type MinimalSignalContextType = {
getHourCyclePreference: () => HourCyclePreference;
getResolvedMessagesLocale: () => string;
getPreferredSystemLocales: () => Array<string>;
getLocaleOverride: () => string | null;
getMainWindowStats: () => Promise<MainWindowStatsType>;
getMenuOptions: () => Promise<MenuOptionsType>;
getNodeVersion: () => string;

View file

@ -45,15 +45,19 @@ window.libphonenumberFormat = PhoneNumberFormat;
window.React = React;
window.ReactDOM = ReactDOM;
const { resolvedTranslationsLocale, preferredSystemLocales } = config;
moment.updateLocale(resolvedTranslationsLocale, {
const { resolvedTranslationsLocale, preferredSystemLocales, localeOverride } =
config;
moment.updateLocale(localeOverride ?? resolvedTranslationsLocale, {
relativeTime: {
s: window.i18n('icu:timestamp_s'),
m: window.i18n('icu:timestamp_m'),
h: window.i18n('icu:timestamp_h'),
},
});
moment.locale(preferredSystemLocales);
moment.locale(
localeOverride != null ? [localeOverride] : preferredSystemLocales
);
const userDataPath = SignalContext.getPath('userData');
window.BasePaths = {

View file

@ -49,6 +49,7 @@ export const MinimalSignalContext: MinimalSignalContextType = {
config.resolvedTranslationsLocaleDirection,
getHourCyclePreference: () => config.hourCyclePreference,
getPreferredSystemLocales: () => config.preferredSystemLocales,
getLocaleOverride: () => config.localeOverride,
nativeThemeListener: createNativeThemeListener(ipcRenderer, window),
restartApp: () => ipcRenderer.send('restart'),
OS: {