feat(i18n): restore hidden subtree names on language change

This commit is contained in:
Elian Doran 2024-11-30 10:13:39 +02:00
parent 6a9865eb4f
commit 690506ea26
No known key found for this signature in database
2 changed files with 17 additions and 6 deletions

View File

@ -272,17 +272,21 @@ const HIDDEN_SUBTREE_DEFINITION: Item = {
]
};
function checkHiddenSubtree(force = false) {
interface CheckHiddenExtraOpts {
restoreNames?: boolean;
}
function checkHiddenSubtree(force = false, extraOpts: CheckHiddenExtraOpts = {}) {
if (!force && !migrationService.isDbUpToDate()) {
// on-delete hook might get triggered during some future migration and cause havoc
log.info("Will not check hidden subtree until migration is finished.");
return;
}
checkHiddenSubtreeRecursively('root', HIDDEN_SUBTREE_DEFINITION);
checkHiddenSubtreeRecursively('root', HIDDEN_SUBTREE_DEFINITION, extraOpts);
}
function checkHiddenSubtreeRecursively(parentNoteId: string, item: Item) {
function checkHiddenSubtreeRecursively(parentNoteId: string, item: Item, extraOpts: CheckHiddenExtraOpts = {}) {
if (!item.id || !item.type || !item.title) {
throw new Error(`Item does not contain mandatory properties: ${JSON.stringify(item)}`);
}
@ -335,6 +339,11 @@ function checkHiddenSubtreeRecursively(parentNoteId: string, item: Item) {
}
}
if (extraOpts.restoreNames && note.title !== item.title) {
note.title = item.title;
note.save();
}
if (note.type !== item.type) {
// enforce a correct note type
note.type = item.type;
@ -371,7 +380,7 @@ function checkHiddenSubtreeRecursively(parentNoteId: string, item: Item) {
}
for (const child of item.children || []) {
checkHiddenSubtreeRecursively(item.id, child);
checkHiddenSubtreeRecursively(item.id, child, extraOpts);
}
}

View File

@ -4,6 +4,7 @@ import options from "./options.js";
import sql_init from "./sql_init.js";
import { join } from "path";
import { getResourceDir } from "./utils.js";
import hidden_subtree from "./hidden_subtree.js";
export async function initializeTranslations() {
const resourceDir = getResourceDir();
@ -33,6 +34,7 @@ function getCurrentLanguage() {
return language;
}
export function changeLanguage(locale: string) {
return i18next.changeLanguage(locale);
export async function changeLanguage(locale: string) {
await i18next.changeLanguage(locale);
hidden_subtree.checkHiddenSubtree(true, { restoreNames: true });
}