Skip to content

A local-first personal finance app

Add language setting (#4112)

Julian Dominguez-Schatz99724f611c1f
diff --git a/packages/desktop-client/e2e/settings.mobile.test.js-snapshots/Mobile-Settings-checks-that-settings-page-can-be-opened-4-chromium-linux.png b/packages/desktop-client/e2e/settings.mobile.test.js-snapshots/Mobile-Settings-checks-that-settings-page-can-be-opened-4-chromium-linux.png
index cd517191cf..88f6cacf70 100644
Binary files a/packages/desktop-client/e2e/settings.mobile.test.js-snapshots/Mobile-Settings-checks-that-settings-page-can-be-opened-4-chromium-linux.png and b/packages/desktop-client/e2e/settings.mobile.test.js-snapshots/Mobile-Settings-checks-that-settings-page-can-be-opened-4-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/settings.test.js-snapshots/Settings-checks-the-page-visuals-1-chromium-linux.png b/packages/desktop-client/e2e/settings.test.js-snapshots/Settings-checks-the-page-visuals-1-chromium-linux.png
index 5d39366571..b436bd7bf8 100644
Binary files a/packages/desktop-client/e2e/settings.test.js-snapshots/Settings-checks-the-page-visuals-1-chromium-linux.png and b/packages/desktop-client/e2e/settings.test.js-snapshots/Settings-checks-the-page-visuals-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/settings.test.js-snapshots/Settings-checks-the-page-visuals-2-chromium-linux.png b/packages/desktop-client/e2e/settings.test.js-snapshots/Settings-checks-the-page-visuals-2-chromium-linux.png
index f65196d93c..80d474eaa6 100644
Binary files a/packages/desktop-client/e2e/settings.test.js-snapshots/Settings-checks-the-page-visuals-2-chromium-linux.png and b/packages/desktop-client/e2e/settings.test.js-snapshots/Settings-checks-the-page-visuals-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/settings.test.js-snapshots/Settings-checks-the-page-visuals-3-chromium-linux.png b/packages/desktop-client/e2e/settings.test.js-snapshots/Settings-checks-the-page-visuals-3-chromium-linux.png
index 56cce417d6..0c688f88a5 100644
Binary files a/packages/desktop-client/e2e/settings.test.js-snapshots/Settings-checks-the-page-visuals-3-chromium-linux.png and b/packages/desktop-client/e2e/settings.test.js-snapshots/Settings-checks-the-page-visuals-3-chromium-linux.png differ
diff --git a/packages/desktop-client/src/components/settings/LanguageSettings.tsx b/packages/desktop-client/src/components/settings/LanguageSettings.tsx
new file mode 100644
index 0000000000..28aec19f04
--- /dev/null
+++ b/packages/desktop-client/src/components/settings/LanguageSettings.tsx
@@ -0,0 +1,86 @@
+import React from 'react';
+import { Trans, useTranslation } from 'react-i18next';
+
+import { type TFunction } from 'i18next';
+
+import { useGlobalPref } from '../../hooks/useGlobalPref';
+import { availableLanguages, setI18NextLanguage } from '../../i18n';
+import { Link } from '../common/Link';
+import { Menu } from '../common/Menu';
+import { Select, type SelectOption } from '../common/Select';
+import { Text } from '../common/Text';
+
+import { Setting } from './UI';
+
+const languageOptions = (t: TFunction): SelectOption[] =>
+ [
+ ['', t('System default')] as [string, string],
+ Menu.line as typeof Menu.line,
+ ].concat(
+ availableLanguages.map(lang => [
+ lang,
+ new Intl.DisplayNames([lang], {
+ type: 'language',
+ }).of(lang) || lang,
+ ]),
+ );
+
+export function LanguageSettings() {
+ const { t } = useTranslation();
+ const [language, setLanguage] = useGlobalPref('language');
+ const isEnabled = !!availableLanguages.length;
+
+ return (
+ <Setting
+ primaryAction={
+ <Select
+ aria-label={t('Select language')}
+ options={languageOptions(t)}
+ value={isEnabled ? (language ?? '') : 'not-available'}
+ defaultLabel={
+ isEnabled ? t('Select language') : t('No languages available')
+ }
+ onChange={value => {
+ setLanguage(value);
+ setI18NextLanguage(value);
+ }}
+ disabled={!isEnabled}
+ />
+ }
+ >
+ <Text>
+ {isEnabled ? (
+ <Trans>
+ <strong>Language</strong> is the display language of all text.
+ Please note that no warranty is provided for the accuracy or
+ completeness of non-English translations. If you encounter a
+ translation error, feel free to make a suggestion on{' '}
+ <Link
+ variant="external"
+ to={
+ 'https://hosted.weblate.org/projects/actualbudget/actual/' +
+ language
+ }
+ linkColor="purple"
+ >
+ Weblate
+ </Link>
+ .
+ </Trans>
+ ) : (
+ <Trans>
+ <strong>Language</strong> support is not available. Please follow
+ the instructions{' '}
+ <Link
+ variant="external"
+ to="https://actualbudget.org/docs/translations"
+ >
+ here
+ </Link>{' '}
+ to add missing translation files.
+ </Trans>
+ )}
+ </Text>
+ </Setting>
+ );
+}
diff --git a/packages/desktop-client/src/components/settings/index.tsx b/packages/desktop-client/src/components/settings/index.tsx
index 491ed850e6..18252c0970 100644
--- a/packages/desktop-client/src/components/settings/index.tsx
+++ b/packages/desktop-client/src/components/settings/index.tsx
@@ -32,6 +32,7 @@ import { ExperimentalFeatures } from './Experimental';
import { ExportBudget } from './Export';
import { FixSplits } from './FixSplits';
import { FormatSettings } from './Format';
+import { LanguageSettings } from './LanguageSettings';
import { ResetCache, ResetSync } from './Reset';
import { ThemeSettings } from './Themes';
import { AdvancedToggle, Setting } from './UI';
@@ -195,6 +196,7 @@ export function Settings() {
<About />
<ThemeSettings />
<FormatSettings />
+ <LanguageSettings />
<AuthSettings />
<EncryptionSettings />
<BudgetTypeSettings />
diff --git a/packages/desktop-client/src/i18n.ts b/packages/desktop-client/src/i18n.ts
index 27af3ecac8..aedb2194f9 100644
--- a/packages/desktop-client/src/i18n.ts
+++ b/packages/desktop-client/src/i18n.ts
@@ -3,25 +3,29 @@ import { initReactI18next } from 'react-i18next';
import i18n from 'i18next';
import resourcesToBackend from 'i18next-resources-to-backend';
-const languages = import.meta.glob('/locale/*.json');
+const languages = import.meta.glob(['/locale/*.json', '!/locale/*_old.json']);
+
+export const availableLanguages = Object.keys(languages).map(
+ path => path.split('/')[2].split('.')[0],
+);
+
+const isLanguageAvailable = (language: string) =>
+ Object.hasOwn(languages, `/locale/${language}.json`);
const loadLanguage = (language: string) => {
- const path = `/locale/${language}.json`;
- if (!Object.hasOwn(languages, path)) {
+ if (!isLanguageAvailable(language)) {
console.error(`Unknown locale ${language}`);
throw new Error(`Unknown locale ${language}`);
}
- return languages[path]();
+ return languages[`/locale/${language}.json`]();
};
i18n
.use(initReactI18next)
.use(resourcesToBackend(loadLanguage))
.init({
- // While we mark all strings for translations, one can test
- // it by setting the language in localStorage to their choice.
// Set this to 'cimode' to see the exact keys without interpolation.
- lng: localStorage.getItem('language') || 'en',
+ lng: 'en',
// allow keys to be phrases having `:`, `.`
nsSeparator: false,
@@ -35,3 +39,28 @@ i18n
transSupportBasicHtmlNodes: false,
},
});
+
+export const setI18NextLanguage = (language: string) => {
+ if (language === 'en' && !isLanguageAvailable(language)) {
+ // English is always available since we use natural-language keys.
+ return;
+ }
+
+ if (!language) {
+ // System default
+ setI18NextLanguage(navigator.language || 'en');
+ return;
+ }
+
+ language = language.toLowerCase();
+ if (!availableLanguages.includes(language)) {
+ if (language.includes('-')) {
+ setI18NextLanguage(language.split('-')[0]);
+ return;
+ }
+
+ console.error(`Unknown locale ${language}`);
+ throw new Error(`Unknown locale ${language}`);
+ }
+ i18n.changeLanguage(language || 'en');
+};
diff --git a/packages/loot-core/src/client/actions/prefs.ts b/packages/loot-core/src/client/actions/prefs.ts
index a777f39046..137010a18f 100644
--- a/packages/loot-core/src/client/actions/prefs.ts
+++ b/packages/loot-core/src/client/actions/prefs.ts
@@ -1,3 +1,5 @@
+import { setI18NextLanguage } from '@actual-app/web/src/i18n';
+
import { send } from '../../platform/client/fetch';
import { parseNumberFormat, setNumberFormat } from '../../shared/util';
import {
@@ -40,6 +42,9 @@ export function loadPrefs() {
}),
);
+ // We need to load translations before the app renders
+ setI18NextLanguage(globalPrefs.language);
+
return prefs;
};
}
diff --git a/packages/loot-core/src/server/main.ts b/packages/loot-core/src/server/main.ts
index c4ac7e7ef0..e5e96bac5d 100644
--- a/packages/loot-core/src/server/main.ts
+++ b/packages/loot-core/src/server/main.ts
@@ -1348,6 +1348,9 @@ handlers['save-global-prefs'] = async function (prefs) {
if ('floatingSidebar' in prefs) {
await asyncStorage.setItem('floating-sidebar', '' + prefs.floatingSidebar);
}
+ if ('language' in prefs) {
+ await asyncStorage.setItem('language', prefs.language);
+ }
if ('theme' in prefs) {
await asyncStorage.setItem('theme', prefs.theme);
}
@@ -1372,6 +1375,7 @@ handlers['load-global-prefs'] = async function () {
[, maxMonths],
[, documentDir],
[, encryptKey],
+ [, language],
[, theme],
[, preferredDarkTheme],
[, serverSelfSignedCert],
@@ -1380,6 +1384,7 @@ handlers['load-global-prefs'] = async function () {
'max-months',
'document-dir',
'encrypt-key',
+ 'language',
'theme',
'preferred-dark-theme',
'server-self-signed-cert',
@@ -1389,6 +1394,7 @@ handlers['load-global-prefs'] = async function () {
maxMonths: stringToInteger(maxMonths || ''),
documentDir: documentDir || getDefaultDocumentDir(),
keyId: encryptKey && JSON.parse(encryptKey).id,
+ language,
theme:
theme === 'light' ||
theme === 'dark' ||
diff --git a/packages/loot-core/src/types/prefs.d.ts b/packages/loot-core/src/types/prefs.d.ts
index 97610ce05d..ea397a6764 100644
--- a/packages/loot-core/src/types/prefs.d.ts
+++ b/packages/loot-core/src/types/prefs.d.ts
@@ -77,6 +77,7 @@ export type GlobalPrefs = Partial<{
floatingSidebar: boolean;
maxMonths: number;
keyId?: string;
+ language: string;
theme: Theme;
preferredDarkTheme: DarkTheme;
documentDir: string; // Electron only
diff --git a/upcoming-release-notes/4112.md b/upcoming-release-notes/4112.md
new file mode 100644
index 0000000000..ccd2b0eaff
--- /dev/null
+++ b/upcoming-release-notes/4112.md
@@ -0,0 +1,6 @@
+---
+category: Features
+authors: [jfdoming]
+---
+
+Add language setting