Skip to content

Build your personal knowledge base with Trilium Notes

fix(note_language): crash if user entered invalid locale

Elian Doranb81fd699819a
diff --git a/src/public/app/widgets/note_language.ts b/src/public/app/widgets/note_language.ts
index e811211196..a1ef83bd01 100644
--- a/src/public/app/widgets/note_language.ts
+++ b/src/public/app/widgets/note_language.ts
@@ -16,6 +16,11 @@ const TPL = `\
</div>
`;
+const DEFAULT_LOCALE: Locale = {
+ id: "",
+ name: t("note_language.not_set")
+};
+
export default class NoteLanguageWidget extends NoteContextAwareWidget {
private dropdown!: Dropdown;
@@ -27,10 +32,7 @@ export default class NoteLanguageWidget extends NoteContextAwareWidget {
constructor() {
super();
this.locales = [
- {
- id: "",
- name: t("note_language.not_set")
- },
+ DEFAULT_LOCALE,
"---",
...getAvailableLocales()
];
@@ -81,7 +83,7 @@ export default class NoteLanguageWidget extends NoteContextAwareWidget {
async refreshWithNote(note: FNote) {
const languageId = note.getLabelValue("language") ?? "";
- const language = this.locales.find((l) => (typeof l === "object" && l.id === languageId)) as Locale;
+ const language = (this.locales.find((l) => (typeof l === "object" && l.id === languageId)) as Locale | null) ?? DEFAULT_LOCALE;
this.$noteLanguageDesc.text(language.name);
}