refactor(client): move finding by locale in service

This commit is contained in:
Elian Doran 2025-03-04 22:45:38 +02:00
parent d582fdea02
commit f150ec15bc
No known key found for this signature in database
2 changed files with 12 additions and 2 deletions

View File

@ -29,5 +29,15 @@ export function getAvailableLocales() {
return locales; 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 t = i18next.t;
export const getCurrentLanguage = () => i18next.language; export const getCurrentLanguage = () => i18next.language;

View File

@ -1,6 +1,6 @@
import { Dropdown } from "bootstrap"; import { Dropdown } from "bootstrap";
import NoteContextAwareWidget from "./note_context_aware_widget.js"; 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 { t } from "i18next";
import type { EventData } from "../components/app_context.js"; import type { EventData } from "../components/app_context.js";
import type FNote from "../entities/fnote.js"; import type FNote from "../entities/fnote.js";
@ -118,7 +118,7 @@ export default class NoteLanguageWidget extends NoteContextAwareWidget {
async refreshWithNote(note: FNote) { async refreshWithNote(note: FNote) {
const currentLanguageId = note.getLabelValue("language") ?? ""; 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.currentLanguageId = currentLanguageId;
this.$noteLanguageDesc.text(language.name); this.$noteLanguageDesc.text(language.name);
this.dropdown.hide(); this.dropdown.hide();