Skip to content

Add localization for remaining core functions and add tests#1487

Open
dpvc wants to merge 1 commit into
feature/locale-startupfrom
feature/locale-core
Open

Add localization for remaining core functions and add tests#1487
dpvc wants to merge 1 commit into
feature/locale-startupfrom
feature/locale-core

Conversation

@dpvc
Copy link
Copy Markdown
Member

@dpvc dpvc commented May 31, 2026

This PR localizes the files that are part of the core component, which includes the ts/util files and the files in ts/core. It also include the messages from ts/output/common even though those aren't in the core component, but since there are only a few of them, and the common code is in more than one output component, they are put in core to avoid duplication in output/chtml and output/svg.

Details

A new testsuite/src/locale.ts is added that sets up the locales. Because the setupTex is included in the index.js file in #helpers, and it loads node-main, which sets up mathjax.asyncLoad to be a synchronous require-based call, that altered some of the Locale test behavior. This file can be used in non-tex tests (like the util tests) to initialize the locale without altering asyncLoad.

In setupTex.ts, we pre-load AsyncLoad and inform Locale that it is synchronous so that Locale.setLocale()will be synchronous (more about this later). That way, we don't have to make the setupTex() and related functions be async, which would require changing a bunch of tests that use it. (So we no longer return promises from those.) We move the Locale.setLocale() to before the creation of the MathDocument in order to avoid the message about Locale not being initialized.

The util tests are modified to load the new src/locale.js file. Tests are added to Locale.test.ts to test the synchronous loading and the new setup code (described when discussing Locale changes below). Because the core component is now being registered whenever a core module is loaded, Locale.setLocale() will try to load a core/__locales__/*.json file, so some tests that use undefined locales end up throwing that error for that. So we trap those errors when we don't care about them, and remove the MathJax(component) prefix from some tests, since the actual component that throws the error may be different (e.g., MathJax(core)).

A few other tests are changed to accommodate changes in the error strings (like " to ', so make the json files easier to deal with).

Most of the rest of the changes are the localizations of the messages in various files.

For the ts/core/MmlNode files, the mError() function is modified to take a message ID and an array of substitutions as its arguments rather than the message itself. Technically, this is a breaking change, but unless someone has created their own internal MathML node, it should not affect anyone.

Finally, the changes to ts/util/Locale.ts are for two things: to allow for synchronous loading of locale files, when possible, and to avoid a cyclical dependency. For the latter, now that ts/util/AsyncLoad.ts has been localized by importing the core/__locales__/Component.ts file (which calls Locale.registerLocaleFiles()), importing AsynchLoad into Locale.ts would cause Locale.registerLocalFiles() to be called before Locale is set up, causing an error.

To avoid this, Locale now puts off loading AsynchLoad until the Locale.setLocale() call, and uses import() to do so asynchronously. It checks whether mathjax.asyncIsSynchronous is true, and save that as Locale.syncLoad if it is. The getLocaleData() method is modified to use this.syncLoad() when it is available in order to do the file loading synchronously, so that Locale.setLocale() is synchronous rather than asynchronous (though it still returns a promise).

Note, however, that the first call to Locale.setLocale() would still be asynchronous in order to wait for mathjax.ts and AsyncLoad.ts to load. To really make Locale.setLocale() completely synchronous, one needs to set Locale.syncLoad by hand before Locale.setLocale() is called. That is why setupTex.ts does that for the testsuite, thus allowing setupTex() and related functions to be synchronous, avoiding the need for await and async functions in the tests that call setupTex(), as described above.

@dpvc dpvc requested a review from zorkow May 31, 2026 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant