A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
intl: add auto locale option, dynamic system locale detection and tests
kashaf-ansari-devf427a7a25016
diff --git a/apps/desktop/src/api/os-integration.ts b/apps/desktop/src/api/os-integration.tsindex 1b98d54c0..516a17c28 100644--- a/apps/desktop/src/api/os-integration.ts+++ b/apps/desktop/src/api/os-integration.ts@@ -139,9 +139,9 @@ export const osIntegrationRouter = t.router({config.backupDirectory = result.filePaths[0];}),setAppLanguage: t.procedure- .input(z.string())+ .input(z.string().optional()).mutation(async ({ input: language }) => {- config.appLanguage = language;+ config.appLanguage = language || "";await initLocale();setupDesktopIntegration(config.desktopSettings);setupJumplist();diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.tsindex 26573e838..a93934229 100644--- a/apps/desktop/src/main.ts+++ b/apps/desktop/src/main.ts@@ -18,7 +18,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.*/import "./overrides";-import { app, BrowserWindow, nativeTheme, shell, dialog } from "electron";+import {+ app,+ BrowserWindow,+ nativeTheme,+ shell,+ dialog,+ ipcMain+} from "electron";import { isDevelopment } from "./utils";import { registerProtocol, PROTOCOL_URL } from "./utils/protocol";import { configureAutoUpdater } from "./utils/autoupdater";@@ -74,7 +81,9 @@ process.on("unhandledRejection", (reason) => {console.error("unhandledRejection:", reason);});-app.commandLine.appendSwitch("lang", "en-US");+ipcMain.on("get-app-locale", (event) => {+ event.returnValue = app.getLocale();+});async function createWindow() {const cliOptions = await parseArguments(process.argv);diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.tsindex dcf2a1ca7..e796564b9 100644--- a/apps/desktop/src/preload.ts+++ b/apps/desktop/src/preload.ts@@ -24,6 +24,7 @@ import { ipcRenderer, contextBridge } from "electron";declare global {var os: () => "mas" | typeof process.platform;var electronTRPC: any;+ var appLocale: () => string;}const electronTRPC = {@@ -34,6 +35,8 @@ const electronTRPC = {};const os = () => (MAC_APP_STORE ? "mas" : process.platform);+const appLocale = () => ipcRenderer.sendSync("get-app-locale");contextBridge.exposeInMainWorld("electronTRPC", electronTRPC);contextBridge.exposeInMainWorld("os", os);+contextBridge.exposeInMainWorld("appLocale", appLocale);diff --git a/apps/desktop/src/utils/locale.ts b/apps/desktop/src/utils/locale.tsindex 32009ef16..23c5892eb 100644--- a/apps/desktop/src/utils/locale.ts+++ b/apps/desktop/src/utils/locale.ts@@ -24,9 +24,6 @@ import { initLocale as initIntlLocale } from "@notesnook/intl";export async function initLocale() {return initIntlLocale({getSavedLocale: () => config.appLanguage,- onSaveLocale: (locale) => {- config.appLanguage = locale;- },systemLocale: app.getLocale() || "en"});}diff --git a/apps/mobile/app/common/locale/index.js b/apps/mobile/app/common/locale/index.jsindex 373c0cdb0..0b6c8e8f3 100644--- a/apps/mobile/app/common/locale/index.js+++ b/apps/mobile/app/common/locale/index.js@@ -24,9 +24,6 @@ import * as RNLocalize from "react-native-localize";export function initLocale() {return initLocaleSync({getSavedLocale: () => SettingsService.getProperty("appLanguage"),- onSaveLocale: (locale) => {- SettingsService.setProperty("appLanguage", locale);- },systemLocale: RNLocalize.getLocales()[0]?.languageTag ?? "en"});}diff --git a/apps/mobile/app/screens/settings/picker/pickers.tsx b/apps/mobile/app/screens/settings/picker/pickers.tsxindex 0c80e51d9..c4ca13ba8 100644--- a/apps/mobile/app/screens/settings/picker/pickers.tsx+++ b/apps/mobile/app/screens/settings/picker/pickers.tsx@@ -38,7 +38,6 @@ import { verifyUserWithApplock } from "../functions";import PaywallSheet from "../../../components/sheets/paywall";import {AVAILABLE_LANGUAGES,- resolveTargetLocale,strings} from "@notesnook/intl";import { initLocale } from "../../../common/locale";@@ -69,18 +68,13 @@ export const LanguagePicker = createSettingsPicker<>({getValue: () => {const saved = useSettingStore.getState().settings.appLanguage;- let systemLocale = "en";- try {- systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;- } catch (e) {- // ignore- }- return resolveTargetLocale(saved, systemLocale);+ return saved || "auto";},updateValue: async (item) => {const lang = typeof item === "object" ? item.code : item;- const currentLang = useSettingStore.getState().settings.appLanguage;- if (lang === currentLang) return;+ const currentLang = useSettingStore.getState().settings.appLanguage || "";+ const newLang = lang === "auto" ? "" : lang;+ if (newLang === currentLang) return;setTimeout(() => {presentDialog({@@ -90,7 +84,7 @@ export const LanguagePicker = createSettingsPicker<negativeText: strings.cancel(),positivePress: async () => {eSendEvent(eCloseSimpleDialog);- SettingsService.setProperty("appLanguage", lang);+ SettingsService.setProperty("appLanguage", newLang);initLocale();// restarting early causes appLanguage value to not get saved.setTimeout(() => RNRestart.restart(), 100);@@ -101,11 +95,17 @@ export const LanguagePicker = createSettingsPicker<},formatValue: (item) => {const code = typeof item === "object" ? item.code : item;+ if (code === "auto") {+ return strings.auto();+ }const found = AVAILABLE_LANGUAGES.find((l) => l.code === code);return found ? `${found.label} (${found.nativeLabel})` : code;},getItemKey: (item) => (typeof item === "object" ? item.code : item),- options: AVAILABLE_LANGUAGES,+ options: [+ { code: "auto", label: "Auto", nativeLabel: "Auto" },+ ...AVAILABLE_LANGUAGES+ ],compareValue: (current, item) =>current === (typeof item === "object" ? item.code : item),isFeatureAvailable: async () => true,diff --git a/apps/web/src/common/locale.ts b/apps/web/src/common/locale.tsindex 5c6fa122d..a5ae05a8a 100644--- a/apps/web/src/common/locale.ts+++ b/apps/web/src/common/locale.ts@@ -21,11 +21,14 @@ import Config from "../utils/config";import { initLocale as initIntlLocale } from "@notesnook/intl";export async function initLocale() {+ const desktopLocale =+ typeof window.appLocale === "function" ? window.appLocale() : undefined;+ const navLang =+ desktopLocale ||+ (typeof navigator !== "undefined" ? navigator.language : undefined);+return initIntlLocale({getSavedLocale: () => Config.get<string>("appLanguage", ""),- onSaveLocale: (locale) => {- Config.set("appLanguage", locale);- },- systemLocale: navigator.language ?? "en"+ systemLocale: navLang ?? "en"});}diff --git a/apps/web/src/dialogs/settings/behaviour-settings.ts b/apps/web/src/dialogs/settings/behaviour-settings.tsindex f7e2aa108..a2c6908c9 100644--- a/apps/web/src/dialogs/settings/behaviour-settings.ts+++ b/apps/web/src/dialogs/settings/behaviour-settings.ts@@ -28,8 +28,7 @@ import { TimeFormat, DayFormat } from "@notesnook/core";import { TrashCleanupInterval } from "@notesnook/core";import {strings,- AVAILABLE_LANGUAGES,- getSupportedLocale+ AVAILABLE_LANGUAGES} from "@notesnook/intl";import { desktop } from "../../common/desktop-bridge";import { ConfirmDialog } from "../confirm";@@ -52,16 +51,9 @@ export const BehaviourSettings: SettingsGroup[] = [{type: "dropdown",onSelectionChanged: async (value) => {- const currentLang = useSettingStore.getState().appLanguage;- let systemLocale = "en";- try {- systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;- } catch (e) {- // ignore- }- const effectiveCurrent =- currentLang || getSupportedLocale(systemLocale);- if (value === effectiveCurrent) return;+ const currentLang = useSettingStore.getState().appLanguage || "";+ const newLang = value === "auto" ? "" : value;+ if (newLang === currentLang) return;const ok = await ConfirmDialog.show({title: strings.changeLanguage(),@@ -71,27 +63,29 @@ export const BehaviourSettings: SettingsGroup[] = [});if (ok) {- useSettingStore.getState().setAppLanguage(value);+ useSettingStore.getState().setAppLanguage(newLang);if (IS_DESKTOP_APP && desktop) {- await desktop.integration.setAppLanguage.mutate(value);+ await desktop.integration.setAppLanguage.mutate(newLang);}window.location.reload();}},selectedOption: () => {const saved = useSettingStore.getState().appLanguage;- let systemLocale = "en";- try {- systemLocale = Intl.DateTimeFormat().resolvedOptions().locale;- } catch (e) {- // ignore- }- return saved || getSupportedLocale(systemLocale);+ return saved || "auto";},- options: AVAILABLE_LANGUAGES.map((l) => ({- value: l.code,- title: `${l.label} (${l.nativeLabel})`- }))+ options: [+ {+ value: "auto",+ get title() {+ return strings.auto();+ }+ },+ ...AVAILABLE_LANGUAGES.map((l) => ({+ value: l.code,+ title: `${l.label} (${l.nativeLabel})`+ }))+ ]}]},diff --git a/apps/web/src/stores/setting-store.ts b/apps/web/src/stores/setting-store.tsindex efd37c0d0..1ceb59417 100644--- a/apps/web/src/stores/setting-store.ts+++ b/apps/web/src/stores/setting-store.ts@@ -93,7 +93,7 @@ class SettingStore extends BaseStore<SettingStore> {isPortable = false;proxyRules?: string;isInboxEnabled = false;- appLanguage = Config.get<string>("appLanguage", "en");+ appLanguage = Config.get<string>("appLanguage", "");init = () => {db.eventManager.subscribe(EVENTS.userFetched, async () => {@@ -112,7 +112,7 @@ class SettingStore extends BaseStore<SettingStore> {titleFormat: db.settings.getTitleFormat(),trashCleanupInterval: db.settings.getTrashCleanupInterval(),profile: db.settings.getProfile(),- appLanguage: Config.get<string>("appLanguage", "en"),+ appLanguage: Config.get<string>("appLanguage", ""),isFlatpak: await desktop?.integration.isFlatpak.query(),isSnap: await desktop?.integration.isSnap.query(),isPortable: await desktop?.integration.isPortable.query(),diff --git a/packages/core/__tests__/locale-resolution.test.ts b/packages/core/__tests__/locale-resolution.test.tsnew file mode 100644index 000000000..1cc31594e--- /dev/null+++ b/packages/core/__tests__/locale-resolution.test.ts@@ -0,0 +1,136 @@+/*+This file is part of the Notesnook project (https://notesnook.com/)++Copyright (C) 2023 Streetwriters (Private) Limited++This program is free software: you can redistribute it and/or modify+it under the terms of the GNU General Public License as published by+the Free Software Foundation, either version 3 of the License, or+(at your option) any later version.++This program is distributed in the hope that it will be useful,+but WITHOUT ANY WARRANTY; without even the implied warranty of+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+GNU General Public License for more details.++You should have received a copy of the GNU General Public License+along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++import { describe, it, expect } from "vitest";+import {+ getSupportedLocale,+ resolveTargetLocale,+ AVAILABLE_LANGUAGES+} from "@notesnook/intl";++describe("Locale Detection & Resolution", () => {+ describe("Exact matches for supported languages", () => {+ AVAILABLE_LANGUAGES.forEach(({ code }) => {+ it(`should correctly resolve exact match for "${code}"`, () => {+ expect(getSupportedLocale(code)).toBe(code);+ });+ });+ });++ describe("German regional variants resolve to 'de'", () => {+ const germanVariants = [+ "de-DE",+ "de-AT",+ "de-CH",+ "de-LU",+ "de-LI",+ "de_DE",+ "de_AT",+ "de_CH",+ "DE-DE",+ "de-1901"+ ];++ germanVariants.forEach((variant) => {+ it(`should map "${variant}" to "de"`, () => {+ expect(getSupportedLocale(variant)).toBe("de");+ });+ });+ });++ describe("Other regional variants resolve to base language", () => {+ const regionalCases: [string, string][] = [+ ["es-ES", "es"],+ ["es-MX", "es"],+ ["es-AR", "es"],+ ["es_CO", "es"],+ ["fr-FR", "fr"],+ ["fr-CA", "fr"],+ ["fr-BE", "fr"],+ ["fr_CH", "fr"],+ ["it-IT", "it"],+ ["it-CH", "it"],+ ["nl-NL", "nl"],+ ["nl-BE", "nl"],+ ["pl-PL", "pl"],+ ["ru-RU", "ru"],+ ["ru-BY", "ru"],+ ["tr-TR", "tr"],+ ["tr_CY", "tr"],+ ["uk-UA", "uk"],+ ["en-US", "en"],+ ["en-GB", "en"],+ ["en-AU", "en"],+ ["en-CA", "en"],+ ["pt-BR", "pt-BR"],+ ["pt-PT", "pt-BR"],+ ["pt", "pt-BR"]+ ];++ regionalCases.forEach(([input, expected]) => {+ it(`should map "${input}" to "${expected}"`, () => {+ expect(getSupportedLocale(input)).toBe(expected);+ });+ });+ });++ describe("Unsupported and invalid locales safely fall back to 'en'", () => {+ const unsupportedCases = [+ "ja-JP",+ "zh-CN",+ "zh-TW",+ "ar-SA",+ "ur-PK",+ "hi-IN",+ "ko-KR",+ "sv-SE",+ "da-DK",+ "fi-FI",+ "unknown",+ "invalid-locale-string",+ "",+ undefined+ ];++ unsupportedCases.forEach((input) => {+ it(`should fall back "${input}" to "en"`, () => {+ expect(getSupportedLocale(input)).toBe("en");+ });+ });+ });++ describe("resolveTargetLocale precedence", () => {+ it("should prioritize a valid saved language over systemLocale", () => {+ expect(resolveTargetLocale("fr", "de-DE")).toBe("fr");+ expect(resolveTargetLocale("tr", "en-US")).toBe("tr");+ });++ it("should use systemLocale when saved language is empty, auto, or system", () => {+ expect(resolveTargetLocale("", "de-AT")).toBe("de");+ expect(resolveTargetLocale("auto", "fr-FR")).toBe("fr");+ expect(resolveTargetLocale("system", "es-ES")).toBe("es");+ expect(resolveTargetLocale(undefined, "tr-TR")).toBe("tr");+ expect(resolveTargetLocale(null, "es-MX")).toBe("es");+ });++ it("should fall back to systemLocale if saved language is invalid/unsupported", () => {+ expect(resolveTargetLocale("invalid-lang", "de-CH")).toBe("de");+ });+ });+});diff --git a/packages/intl/src/locale.ts b/packages/intl/src/locale.tsindex efdfcc12f..0d5d4d837 100644--- a/packages/intl/src/locale.ts+++ b/packages/intl/src/locale.ts@@ -50,18 +50,17 @@ async function getLocaleMessages(lang: string): Promise<Messages> {export type InitLocaleOptions = {getSavedLocale?: () => string | null | undefined;- onSaveLocale?: (locale: string) => void;systemLocale: string;};export type InitLocaleSyncOptions = InitLocaleOptions;-function resolveAndSaveLocale(options: InitLocaleOptions): string {+function resolveLocale(options: InitLocaleOptions): string {const saved = options.getSavedLocale?.();const targetLang = resolveTargetLocale(saved, options.systemLocale);- if (!saved && options.onSaveLocale) {- options.onSaveLocale(targetLang);- }+ console.info(+ `[locale] initialized target="${targetLang}" (saved="${saved}", system="${options.systemLocale}")`+ );return targetLang;}@@ -71,7 +70,7 @@ function activateLocale(targetLang: string) {}export function initLocaleSync(options: InitLocaleOptions): string {- const targetLang = resolveAndSaveLocale(options);+ const targetLang = resolveLocale(options);const messages = getLocaleMessagesSync(targetLang);defaultI18n.load({ [targetLang]: messages });activateLocale(targetLang);@@ -79,7 +78,7 @@ export function initLocaleSync(options: InitLocaleOptions): string {}export async function initLocale(options: InitLocaleOptions): Promise<string> {- const targetLang = resolveAndSaveLocale(options);+ const targetLang = resolveLocale(options);const messages = await getLocaleMessages(targetLang);defaultI18n.load({ [targetLang]: messages });activateLocale(targetLang);