Add first lazily loaded i18n strings

This commit is contained in:
Henrik Giesel 2021-03-31 22:05:24 +02:00
parent 9b68a5c2c3
commit 3ddbc1e6c3
4 changed files with 56 additions and 17 deletions

View File

@ -7,6 +7,7 @@
export let props: Record<string, string> = {};
export let label: string;
export let title: string;
export let onClick: (event: ClickEvent) => void;
export let disables = true;
@ -58,6 +59,7 @@
class={extendClassName(className)}
{...props}
on:click={onClick}
on:mousedown|preventDefault>
on:mousedown|preventDefault
{title}>
{label}
</button>

View File

@ -1,7 +1,8 @@
import type { SvelteComponent } from "svelte";
import { checkNightMode } from "anki/nightmode";
import { setupI18n, ModuleName } from "anki/i18n";
import { setupI18n, ModuleName, i18n } from "anki/i18n";
import * as tr from "anki/i18n";
import EditorToolbarSvelte from "./EditorToolbar.svelte";
@ -70,19 +71,19 @@ class EditorToolbar extends HTMLElement {
connectedCallback(): void {
this.disabled = writable(false);
setupI18n({ modules: [ModuleName.STATISTICS, ModuleName.SCHEDULING] }).then(
() => {
this.component = new EditorToolbarSvelte({
target: this,
props: {
menus: defaultMenus,
buttons: defaultButtons,
nightMode: checkNightMode(),
disabled: this.disabled,
},
});
}
);
setupI18n({ modules: [ModuleName.EDITING] }).then(() => {
console.log(i18n, tr);
this.component = new EditorToolbarSvelte({
target: this,
props: {
menus: defaultMenus,
buttons: defaultButtons,
nightMode: checkNightMode(),
disabled: this.disabled,
},
});
});
}
enableButtons(): void {

View File

@ -1,15 +1,26 @@
import { bridgeCommand } from "anki/bridgecommand";
import { lazyLoaded } from "anki/lazy";
import * as tr from "anki/i18n";
import LabelButton from "./LabelButton.svelte";
export const fieldsButton = {
component: LabelButton,
label: "Fields...",
onClick: () => bridgeCommand("fields"),
disables: false,
};
lazyLoaded(fieldsButton, {
label: () => `${tr.editingFields()}...`,
title: tr.editingCustomizeFields,
});
export const cardsButton = {
component: LabelButton,
label: "Cards...",
onClick: () => bridgeCommand("cards"),
disables: false,
};
lazyLoaded(cardsButton, {
label: () => `${tr.editingCards()}...`,
title: tr.editingCustomizeCardTemplatesCtrlandl,
});

25
ts/lib/lazy.ts Normal file
View File

@ -0,0 +1,25 @@
export function lazyLoaded(
object: Record<string, unknown>,
properties: Record<string, () => unknown>
): void {
const propertyDescriptorMap = Object.entries(properties)
.map(([name, getter]: [string, () => unknown]): [
string,
PropertyDescriptor
] => [
name,
{
get: getter,
enumerable: true,
},
])
.reduce(
(
accumulator: PropertyDescriptorMap,
[name, property]
): PropertyDescriptorMap => ((accumulator[name] = property), accumulator),
{}
);
Object.defineProperties(object, propertyDescriptorMap);
}