Skip to content

Commit 3625616

Browse files
committed
fix: cache first locale and refresh when system locale changes
1 parent 3d100f4 commit 3625616

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

electron/src/locale/index.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,23 @@ export const SUPPORTED_LANGUAGES = {
146146
let current: SupportedI18nLanguage | undefined;
147147

148148
export const getCurrent = (): SupportedI18nLanguage => {
149-
const savedLocale = settings.restore<SupportedI18nLanguage | undefined>(SettingsType.LOCALE);
150-
const savedOverride = settings.restore<boolean | undefined>(SettingsType.LOCALE_OVERRIDE);
151-
152-
// If the override flag is missing, assume the user wanted an override only when the saved value differs
153-
// from the current system locale. This keeps existing manual choices intact while allowing auto language
154-
// to track OS changes.
155149
const systemLocale = getSystemLocale();
156-
const hasUserOverride =
157-
typeof savedOverride === 'boolean' ? savedOverride : Boolean(savedLocale && savedLocale !== systemLocale);
158150

159-
current = savedLocale && hasUserOverride ? parseLocale(savedLocale) : systemLocale;
151+
if (!current) {
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;
159+
}
160+
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+
}
160166
return current;
161167
};
162168

@@ -188,7 +194,7 @@ export const getText = (
188194

189195
export const setLocale = (locale: string): void => {
190196
current = parseLocale(locale);
191-
settings.save(SettingsType.LOCALE, current);
197+
192198
const systemLocale = getSystemLocale();
193199
const isOverride = current !== systemLocale;
194200

0 commit comments

Comments
 (0)