From f150ec15bc17a643673e6f1b98bcb44668bf0803 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 4 Mar 2025 22:45:38 +0200 Subject: [PATCH] refactor(client): move finding by locale in service --- src/public/app/services/i18n.ts | 10 ++++++++++ src/public/app/widgets/note_language.ts | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/public/app/services/i18n.ts b/src/public/app/services/i18n.ts index ec1e55acf..939fea000 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 f88bb450f..7ffd8e250 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();