Expose i18n globals in SignalContext across all windows

This commit is contained in:
Jamie Kyle 2023-08-07 13:28:09 -07:00 committed by GitHub
parent 55394e17c0
commit 4455328312
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 38 additions and 27 deletions

View file

@ -29,14 +29,16 @@ global.window = {
warn: (...args) => console.warn(...args),
error: (...args) => console.error(...args),
},
getResolvedMessagesLocale: () => 'en',
getResolvedMessagesLocaleDirection: () => 'ltr',
getHourCyclePreference: () => HourCyclePreference.UnknownPreference,
getPreferredSystemLocales: () => ['en'],
},
i18n: key => `i18n(${key})`,
storage: {
get: key => storageMap.get(key),
put: async (key, value) => storageMap.set(key, value),
},
getPreferredSystemLocales: () => ['en'],
getHourCyclePreference: () => HourCyclePreference.UnknownPreference,
};
// For ducks/network.getEmptyState()

View file

@ -573,12 +573,12 @@ export async function startApp(): Promise<void> {
document.documentElement.setAttribute(
'lang',
window.getResolvedMessagesLocale().split(/[-_]/)[0]
window.SignalContext.getResolvedMessagesLocale().split(/[-_]/)[0]
);
document.documentElement.setAttribute(
'dir',
window.getResolvedMessagesLocaleDirection()
window.SignalContext.getResolvedMessagesLocaleDirection()
);
KeyChangeListener.init(window.textsecure.storage.protocol);

View file

@ -36,7 +36,7 @@ export function ChatSessionRefreshedNotification(
const baseUrl =
'https://support.signal.org/hc/LOCALE/requests/new?desktop&chat_refreshed';
const locale = window.getResolvedMessagesLocale();
const locale = window.SignalContext.getResolvedMessagesLocale();
const supportLocale = mapToSupportLocale(locale);
const url = baseUrl.replace('LOCALE', supportLocale);

View file

@ -2806,8 +2806,8 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
'getProfile: expected updatesUrl to be a defined string'
);
const userLanguages = getUserLanguages(
window.getPreferredSystemLocales(),
window.getResolvedMessagesLocale()
window.SignalContext.getPreferredSystemLocales(),
window.SignalContext.getResolvedMessagesLocale()
);
const { messaging } = window.textsecure;
if (!messaging) {

View file

@ -224,8 +224,8 @@ async function doGetProfile(c: ConversationModel): Promise<void> {
);
const userLanguages = getUserLanguages(
window.getPreferredSystemLocales(),
window.getResolvedMessagesLocale()
window.SignalContext.getPreferredSystemLocales(),
window.SignalContext.getResolvedMessagesLocale()
);
let profile;

View file

@ -16,8 +16,14 @@ describe('formatTimestamp', () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
localesStub = sandbox.stub(window, 'getPreferredSystemLocales');
hourCycleStub = sandbox.stub(window, 'getHourCyclePreference');
localesStub = sandbox.stub(
window.SignalContext,
'getPreferredSystemLocales'
);
hourCycleStub = sandbox.stub(
window.SignalContext,
'getHourCyclePreference'
);
});
afterEach(() => {

View file

@ -7,7 +7,7 @@ import { assertDev } from './assert';
function getOptionsWithPreferences(
options: Intl.DateTimeFormatOptions
): Intl.DateTimeFormatOptions {
const hourCyclePreference = window.getHourCyclePreference();
const hourCyclePreference = window.SignalContext.getHourCyclePreference();
if (options.hour12 != null) {
return options;
}
@ -67,7 +67,7 @@ const formatterCache = new Map<string, Intl.DateTimeFormat>();
export function getDateTimeFormatter(
options: Intl.DateTimeFormatOptions
): Intl.DateTimeFormat {
const locales = window.getPreferredSystemLocales();
const locales = window.SignalContext.getPreferredSystemLocales();
const optionsWithPreferences = getOptionsWithPreferences(options);
const cacheKey = getCacheKey(locales, optionsWithPreferences);
const cachedFormatter = formatterCache.get(cacheKey);

View file

@ -15,6 +15,7 @@ const logicalArrows: Record<
};
export function arrow(logicalDirection: LogicalDirection): HorizontalArrowKey {
const localeDirection = window.getResolvedMessagesLocaleDirection();
const localeDirection =
window.SignalContext.getResolvedMessagesLocaleDirection();
return logicalArrows[logicalDirection][localeDirection];
}

View file

@ -121,10 +121,10 @@ export function setupI18n(
localizer.getLocale = () => locale;
localizer.getLocaleMessages = () => messages;
localizer.getLocaleDirection = () => {
return window.getResolvedMessagesLocaleDirection();
return window.SignalContext.getResolvedMessagesLocaleDirection();
};
localizer.getHourCyclePreference = () => {
return window.getHourCyclePreference();
return window.SignalContext.getHourCyclePreference();
};
return localizer;

6
ts/window.d.ts vendored
View file

@ -56,9 +56,7 @@ import type * as Message2 from './types/Message2';
import type { initializeMigrations } from './signal';
import type { RetryPlaceholders } from './util/retryPlaceholders';
import type { PropsPreloadType as PreferencesPropsType } from './components/Preferences';
import type { LocaleDirection } from '../app/locale';
import type { WindowsNotificationData } from './services/notifications';
import type { HourCyclePreference } from './types/I18N';
export { Long } from 'long';
@ -199,10 +197,6 @@ declare global {
getEnvironment: typeof getEnvironment;
getHostName: () => string;
getInteractionMode: () => 'mouse' | 'keyboard';
getResolvedMessagesLocaleDirection: () => LocaleDirection;
getHourCyclePreference: () => HourCyclePreference;
getResolvedMessagesLocale: () => string;
getPreferredSystemLocales: () => Array<string>;
getServerPublicParams: () => string;
getSfuUrl: () => string;
getSocketStatus: () => SocketStatus;

View file

@ -22,6 +22,8 @@ import { i18n } from '../context/i18n';
import { strictAssert } from '../util/assert';
import { initialize as initializeLogging } from '../logging/set_up_renderer_logging';
import { MinimalSignalContext } from './minimalContext';
import type { LocaleDirection } from '../../app/locale';
import type { HourCyclePreference } from '../types/I18N';
strictAssert(Boolean(window.SignalContext), 'context must be defined');
@ -41,6 +43,10 @@ export type MinimalSignalContextType = {
getEnvironment: () => string;
getI18nLocale: LocalizerType['getLocale'];
getI18nLocaleMessages: LocalizerType['getLocaleMessages'];
getResolvedMessagesLocaleDirection: () => LocaleDirection;
getHourCyclePreference: () => HourCyclePreference;
getResolvedMessagesLocale: () => string;
getPreferredSystemLocales: () => Array<string>;
getMainWindowStats: () => Promise<MainWindowStatsType>;
getMenuOptions: () => Promise<MenuOptionsType>;
getNodeVersion: () => string;

View file

@ -45,11 +45,6 @@ window.RETRY_DELAY = false;
window.platform = process.platform;
window.getTitle = () => title;
window.getResolvedMessagesLocale = () => config.resolvedTranslationsLocale;
window.getResolvedMessagesLocaleDirection = () =>
config.resolvedTranslationsLocaleDirection;
window.getHourCyclePreference = () => config.hourCyclePreference;
window.getPreferredSystemLocales = () => config.preferredSystemLocales;
window.getEnvironment = getEnvironment;
window.getAppInstance = () => config.appInstance;
window.getVersion = () => config.version;

View file

@ -42,6 +42,13 @@ export const MinimalSignalContext: MinimalSignalContextType = {
},
getI18nLocale: () => config.resolvedTranslationsLocale,
getI18nLocaleMessages: () => localeMessages,
getResolvedMessagesLocale: () => config.resolvedTranslationsLocale,
getResolvedMessagesLocaleDirection: () =>
config.resolvedTranslationsLocaleDirection,
getHourCyclePreference: () => config.hourCyclePreference,
getPreferredSystemLocales: () => config.preferredSystemLocales,
nativeThemeListener: createNativeThemeListener(ipcRenderer, window),
OS: {
getClassName: () => ipcRenderer.sendSync('OS.getClassName'),