Skip to content

Commit b441549

Browse files
authored
Merge pull request #9366 from wireapp/feat/default-system-language-WPB-21019
feat: Desktop app respect operating system's language setting by default(WPB-21019)
2 parents 35bc5b5 + 3625616 commit b441549

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

electron/src/locale/index.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ export type SupportedI18nLanguageObject = Record<SupportedI18nLanguage, i18nStri
5555

5656
const app = Electron.app || require('@electron/remote').app;
5757

58+
const parseLocale = (locale: string): SupportedI18nLanguage => {
59+
const languageKeys = Object.keys(SUPPORTED_LANGUAGES) as SupportedI18nLanguage[];
60+
return languageKeys.find(languageKey => languageKey === locale) || languageKeys[0];
61+
};
62+
63+
const getSystemLocale = (): SupportedI18nLanguage => parseLocale(app.getLocale().substring(0, 2));
64+
5865
export const LANGUAGES: SupportedI18nLanguageObject = {
5966
cs,
6067
da,
@@ -139,17 +146,24 @@ export const SUPPORTED_LANGUAGES = {
139146
let current: SupportedI18nLanguage | undefined;
140147

141148
export const getCurrent = (): SupportedI18nLanguage => {
149+
const systemLocale = getSystemLocale();
150+
142151
if (!current) {
143-
// We care only about the language part and not the country (en_US, de_DE)
144-
const defaultLocale = parseLocale(app.getLocale().substring(0, 2));
145-
current = settings.restore(SettingsType.LOCALE, defaultLocale);
152+
const savedLocale = settings.restore<SupportedI18nLanguage | undefined>(SettingsType.LOCALE);
153+
const savedOverride = settings.restore<boolean | undefined>(SettingsType.LOCALE_OVERRIDE);
154+
const hasUserOverride =
155+
typeof savedOverride === 'boolean' ? savedOverride : Boolean(savedLocale && savedLocale !== systemLocale);
156+
157+
current = savedLocale && hasUserOverride ? parseLocale(savedLocale) : systemLocale;
158+
return current;
146159
}
147-
return current;
148-
};
149160

150-
const parseLocale = (locale: string): SupportedI18nLanguage => {
151-
const languageKeys = Object.keys(SUPPORTED_LANGUAGES) as SupportedI18nLanguage[];
152-
return languageKeys.find(languageKey => languageKey === locale) || languageKeys[0];
161+
// If there’s no override and the system locale changed, update the cache
162+
const hasOverride = settings.restore<boolean | undefined>(SettingsType.LOCALE_OVERRIDE) === true;
163+
if (!hasOverride && current !== systemLocale) {
164+
current = systemLocale;
165+
}
166+
return current;
153167
};
154168

155169
const customReplacements: Record<string, string> = {
@@ -180,5 +194,15 @@ export const getText = (
180194

181195
export const setLocale = (locale: string): void => {
182196
current = parseLocale(locale);
183-
settings.save(SettingsType.LOCALE, current);
197+
198+
const systemLocale = getSystemLocale();
199+
const isOverride = current !== systemLocale;
200+
201+
if (isOverride) {
202+
settings.save(SettingsType.LOCALE_OVERRIDE, true);
203+
settings.save(SettingsType.LOCALE, current);
204+
} else {
205+
settings.delete(SettingsType.LOCALE_OVERRIDE);
206+
settings.delete(SettingsType.LOCALE);
207+
}
184208
};

electron/src/settings/SettingsType.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export enum SettingsType {
4646
FULL_SCREEN = 'fullscreen',
4747
/** Which language (ISO 639-1) should be used to load our web app (de, en, fr, etc.)? */
4848
LOCALE = 'locale',
49+
/** Whether the locale has been explicitly set by the user instead of using the system default. */
50+
LOCALE_OVERRIDE = 'localeOverride',
4951
/** Defines the proxy server url (e.g. http://127.0.0.1:3128)
5052
*
5153
* https://github.com/wireapp/wire-desktop/wiki/Start-Parameters#use-authenticated-proxy-server

0 commit comments

Comments
 (0)