Skip to content

Build your personal knowledge base with Trilium Notes

fix(math): localize MathLive in the application language

Elian Doran2935cf5fe485
diff --git a/packages/ckeditor5/src/plugins/math/math_input_view.spec.ts b/packages/ckeditor5/src/plugins/math/math_input_view.spec.ts
index 8019b17269..e08032b4f8 100644
--- a/packages/ckeditor5/src/plugins/math/math_input_view.spec.ts
+++ b/packages/ckeditor5/src/plugins/math/math_input_view.spec.ts
@@ -1,9 +1,11 @@
// Side-effect import: declares the `math` key on EditorConfig used by the editor configs below.
import './math.js';
+import { type DISPLAYABLE_LOCALE_IDS, LOCALES } from '@triliumnext/commons';
import { Locale } from 'ckeditor5';
-import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import getCkLocale from '../../i18n.js';
import MathInputView, { LatexTextAreaView, MathFieldFocusableView } from './math_input_view.js';
interface MathField extends HTMLElement {
@@ -17,8 +19,8 @@ describe( 'MathInputView', () => {
let view: MathInputView;
/** Render the view into the document and wait for MathLive to mount its <math-field>. */
- async function renderAndWait(): Promise<MathField> {
- view = new MathInputView( new Locale() );
+ async function renderAndWait( locale = new Locale() ): Promise<MathField> {
+ view = new MathInputView( locale );
view.render();
document.body.appendChild( view.element as HTMLElement );
@@ -52,6 +54,71 @@ describe( 'MathInputView', () => {
expect( mathfield.getAttribute( 'tabindex' ) ).toBe( '0' );
} );
+ it( 'localizes MathLive in the editor UI language rather than the browser one', async () => {
+ const mathFieldClass = () => customElements.get( 'math-field' ) as unknown as { locale: string };
+
+ await renderAndWait( new Locale( { uiLanguage: 'de' } ) );
+ expect( mathFieldClass().locale ).toBe( 'de' );
+ view.element?.remove();
+ view.destroy();
+
+ await renderAndWait( new Locale( { uiLanguage: 'zh' } ) );
+ expect( mathFieldClass().locale ).toBe( 'zh-cn' );
+ view.element?.remove();
+ view.destroy();
+
+ await renderAndWait();
+ expect( mathFieldClass().locale ).toBe( 'en' );
+ } );
+
+ describe( 'in every UI language', () => {
+ const localeIds = LOCALES
+ .filter( locale => !locale.contentOnly )
+ .map( locale => locale.id as DISPLAYABLE_LOCALE_IDS );
+ const errors: unknown[] = [];
+ const onError = ( event: ErrorEvent ) => errors.push( event.error ?? event.message );
+ const onRejection = ( event: PromiseRejectionEvent ) => errors.push( event.reason );
+
+ // Vitest runs the spec in an iframe, where `window.mathVirtualKeyboard` is a proxy that shows
+ // the keyboard in the top window. Mounting it here makes it render in this document.
+ beforeAll( async () => {
+ const { initVirtualKeyboardInCurrentBrowsingContext } = await import( 'mathlive' );
+ initVirtualKeyboardInCurrentBrowsingContext();
+ } );
+
+ beforeEach( () => {
+ errors.length = 0;
+ window.addEventListener( 'error', onError );
+ window.addEventListener( 'unhandledrejection', onRejection );
+ } );
+
+ afterEach( () => {
+ window.mathVirtualKeyboard?.hide();
+ window.removeEventListener( 'error', onError );
+ window.removeEventListener( 'unhandledrejection', onRejection );
+ } );
+
+ it.each( localeIds )( 'mounts the math field and shows the keyboard in %s', async localeId => {
+ // The same language code the text editor is configured with for this locale.
+ const { language } = await getCkLocale( localeId );
+ const uiLanguage = typeof language === 'string' ? language : 'en';
+ const mathfield = await renderAndWait( new Locale( { uiLanguage } ) );
+ mathfield.focus();
+ window.mathVirtualKeyboard?.show();
+
+ // MathLive applies a locale change to the DOM in a `setTimeout`.
+ await new Promise( resolve => setTimeout( resolve, 0 ) );
+ await new Promise( resolve => requestAnimationFrame( resolve ) );
+
+ const menuToggle = mathfield.shadowRoot?.querySelector( '[data-l10n-tooltip="tooltip.menu"]' );
+ expect( menuToggle?.getAttribute( 'data-tooltip' ) ).toBeTruthy();
+ const keyboard = document.querySelector( 'body > .ML__keyboard' );
+ expect( keyboard ).not.toBeNull();
+ expect( keyboard?.querySelector( '[data-tooltip]' ) ).not.toBeNull();
+ expect( errors ).toEqual( [] );
+ } );
+ } );
+
it( 'renders only the LaTeX textarea when MathLive is disabled', () => {
view = new MathInputView( new Locale(), false );
view.render();
diff --git a/packages/ckeditor5/src/plugins/math/math_input_view.ts b/packages/ckeditor5/src/plugins/math/math_input_view.ts
index 7c8518faff..4863d4b794 100644
--- a/packages/ckeditor5/src/plugins/math/math_input_view.ts
+++ b/packages/ckeditor5/src/plugins/math/math_input_view.ts
@@ -62,10 +62,14 @@ export default class MathInputView extends View {
/** Replaces the editor when MathLive fails to load; resolved up front, while the locale is at hand. */
private readonly _unavailableLabel: string;
+ /** The editor UI language, which MathLive otherwise reads from `navigator.language`. */
+ private readonly _mathLiveLocale: string;
+
constructor( locale: Locale, enableMathField = true ) {
super( locale );
this._enableMathField = enableMathField;
this._unavailableLabel = locale.t( 'Math editor unavailable' );
+ this._mathLiveLocale = toMathLiveLocale( locale.uiLanguage );
this.latexTextAreaView = new LatexTextAreaView( locale );
this.mathFieldFocusableView = new MathFieldFocusableView( locale, this );
this.set( 'value', null );
@@ -169,14 +173,18 @@ export default class MathInputView extends View {
if ( this._destroyed ) {
return;
}
- if ( !MathInputView._configured ) {
- const MathfieldClass = customElements.get( 'math-field' ) as any;
- /* v8 ignore next -- defensive: whenDefined() resolved, so the element is registered */
- if ( MathfieldClass ) {
+ const MathfieldClass = customElements.get( 'math-field' ) as any;
+ /* v8 ignore next -- defensive: whenDefined() resolved, so the element is registered */
+ if ( MathfieldClass ) {
+ if ( !MathInputView._configured ) {
MathfieldClass.soundsDirectory = null;
MathfieldClass.plonkSound = null;
MathInputView._configured = true;
}
+ // Setting the locale re-renders every math field and keyboard, so it is set only on change.
+ if ( MathfieldClass.locale !== this._mathLiveLocale ) {
+ MathfieldClass.locale = this._mathLiveLocale;
+ }
}
if ( this.element && !this._destroyed ) {
this._initMathField( true );
@@ -288,3 +296,9 @@ export default class MathInputView extends View {
super.destroy();
}
}
+
+// MathLive looks a locale up as given, then by its first two letters, and files Simplified Chinese
+// under `zh-cn`, which CKEditor calls `zh`.
+function toMathLiveLocale( uiLanguage: string ): string {
+ return uiLanguage === 'zh' ? 'zh-cn' : uiLanguage;
+}