Skip to content

Build your personal knowledge base with Trilium Notes

refactor(client): move finding by locale in service

Elian Doranf150ec15bc17
diff --git a/src/public/app/services/i18n.ts b/src/public/app/services/i18n.ts
index ec1e55acf5..939fea000a 100644
--- a/src/public/app/services/i18n.ts
+++ b/src/public/app/services/i18n.ts
@@ -29,5 +29,15 @@ export function getAvailableLocales() {
return locales;
}
+/**
+ * Finds the given locale by ID.
+ *
+ * @param localeId the locale ID to search for.
+ * @returns the corresponding {@link Locale} or `null` if it was not found.
+ */
+export function getLocaleById(localeId: string ) {
+ return locales?.find((l) => l.id === localeId);
+}
+
export const t = i18next.t;
export const getCurrentLanguage = () => i18next.language;
diff --git a/src/public/app/widgets/note_language.ts b/src/public/app/widgets/note_language.ts
index f88bb450f0..7ffd8e2503 100644
--- a/src/public/app/widgets/note_language.ts
+++ b/src/public/app/widgets/note_language.ts
@@ -1,6 +1,6 @@
import { Dropdown } from "bootstrap";
import NoteContextAwareWidget from "./note_context_aware_widget.js";
-import { getAvailableLocales } from "../services/i18n.js";
+import { getAvailableLocales, getLocaleById } from "../services/i18n.js";
import { t } from "i18next";
import type { EventData } from "../components/app_context.js";
import type FNote from "../entities/fnote.js";
@@ -118,7 +118,7 @@ export default class NoteLanguageWidget extends NoteContextAwareWidget {
async refreshWithNote(note: FNote) {
const currentLanguageId = note.getLabelValue("language") ?? "";
- const language = (this.locales.find((l) => (typeof l === "object" && l.id === currentLanguageId)) as Locale | null) ?? DEFAULT_LOCALE;
+ const language = getLocaleById(currentLanguageId) ?? DEFAULT_LOCALE;
this.currentLanguageId = currentLanguageId;
this.$noteLanguageDesc.text(language.name);
this.dropdown.hide();