Preload en locale in __locales__/Component.ts files, and allow building MathJax with any locale as default.#1527
Open
dpvc wants to merge 4 commits into
Open
Preload en locale in __locales__/Component.ts files, and allow building MathJax with any locale as default.#1527dpvc wants to merge 4 commits into
__locales__/Component.ts files, and allow building MathJax with any locale as default.#1527dpvc wants to merge 4 commits into
Conversation
…scripts to specify the default locale
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note: while there are a lot of files touched by this PR, most come in batches that all work the same (e.g., removal of imports of
en.jsfrom the component definitions, and deletion ofen.jsitself, or addition ofpreload.ctsfiles to the__locales__directories, plus modifications toComponent.tsfiles to importpreload.cjsand changes to componentconfig.jsonfiles to exclude copyingpreload.ctsto the bundle directories. There are 30__locales__directories, so this changes are repeated 30 times each, leading to 150 file changes just for these changes alone, the vast majority of the files changed here.)This PR updates how the English locale is included into MathJax by default. In an earlier PR, this was done through the component files, so worked for in-browser use, and for node applications that used components, but not for those that used direct importing of the MathJax modules.
This PR moves the loading of English localizations into the
__locales__/Component.tsfiles themselves, so that even direct loading of the modules will have the English messages available by default. Applications can still useLocale.setLocale()to change the locale and load the appropriate message files.Because
import ... with {type: 'json'};reports an error for our CJS compilation configuration, I needed to work out an arrangement that works for both MJS and CJS files. To do that, I addedpreload.ctsfiles in the__locales__directories, which userequire()to load theen.jsonfile and export that data back to theComponent.tsfiles. They also export heenstring, andComponents.tsuses that to set the initial messages. This makes it possible to have webpack replace theen.jsonandenstring by any of the other locales so that you can build a vision of MathJax that preloads a different locale (without having the English). The build scripts are modified to allow the locale to be specified, so it is easy to build MathJax with a different default locale.That means we could potentially make packages like
mathjax-de,mathjax-fr, etc., that have the components built with each of the available languages. Then a web page could loadhttps://cdn.jsdelivr.net/npm/mathjax-de@4/tex-chtml.jsto get a version with German localization that does not include the English messages. Because the messages are webpacked into the components, this would decrease the number of files needed for a German page, as otherwise, all the German localization JSON files would need to be loaded individually for each component. So that might be a useful thing to provide (though then a release would need to build a dozen separate packages).Details
The
components/bin/makeAllandcomponents/bin/packfiles are modified to handle passing the locale.The various component
config.jsonfiles are modified to exclude thepreload.ctsfile from being copied to the bundle__locales__directories.The original
en.jsfiles from the components have been removed, and the definition files modified to remove the importing ofen.js, since that is now done in the ts modules directly. That accounts for a large number of the changes in this PR.The messages from
ts/util/Locale.tsare moved to a newts/util/Locale/__locales__directory, and a newlocalecomponent is created just to do the copying of those files into thebundledirectory.The
components/wepack.common.cjsfile is modified to handle changing the default locale. It does this by performing a string substitution on thepreload.cjsfiles to convertento whatever locale is requested. This changes theenstring that is passed back toComponent.ts(so that the proper locale is initialized) as well as theeninen.jsonso that the proper messages are loaded. TheComponents.tsfile doesn't really know which locale is being loaded, so whichever one it ends up being is OK with it.The other
webpack.config.*files are modified to pass the local on to the main webpack common file.Because the
preload.ctsfiles userequire(), which is not allowed in the lab, a newlab/preload.jsfile that returns an empty locale and empty message data object is added so that the lab can redirect the__locales__/preload.cjsfiles tolab/preload.js, this avoiding the errors that would occur from trying to load the actualpreload.cjsfiles. Because the lab includes aLocale.setLocale()call, it will end up loading the needed localizations itself, so there is no problem with undefined messages in the lab. The only complication is that you do need to redirect everypreload.cjsfile tolab/preload.jsin the lab'simportmapscript. So you need to addto the import map, and if any new components get locales, those would have to be added as well. There doesn't seem to be any way to use wildcards in the import map, so you need an individual line for each preload file. Sigh.
The
package.jsonfile is modified to pass the locale information on as needed, and a few additional-soptions are added to prevent output from some commands when using current versions ofpnpmwhich seem to have changed how output is produced from nested scripts. it also loads thestring-replace-loaderdependency that is needed for the web packing of alternate locales.You can now do
to build all of MathJax with a given locale, or
to just webpack everything with the given local, or
to build a specific component with the given locale. We don't usually build the cjs bundle, but you can do that with
to pack with a specific locale.
Some tests were modified to accommodate the changes or add coverage.
The various
__locales__/Component.tsfiles were modified to load the locale and message data from__locales__/preload.cjs, and the preload files were added. This is another big chunk of this PR.The default
localeints/a11y/semantic-enrich.tsis initialized by theLocale.defaultrather than an explicit'en', so that if MathJax is webpacked with another default locale, that will be used here instead. Similarly in thets/a11y/speech/SpeechUtil.tsandts/ui/menu/Menu.tsfiles.The
ts/utile/Locale.tsfile is modified to move its messages into separate locale files ints/util/Locale/__locales__, so that it operates the same as other components, without needing to have its messages for all the locales be hard-coded into it, and so that MathJax can be built with other default locales using the same mechanism as with other components. We don't use aComponent.tsfile here, however, because that file callsLocale.registerLocaleFiles()andLocale.registerMessages(), and these can't be called before theLocaleclass is defined, so we could not importComponent.tsinLocale.ts. The actions that are usually performed inComponent.tsare done inLocale.tsinstead.In addition, there are some small changes made to not overwrite the component data if a component is registered twice (as the
a11y/ariacomponent may be, if thea11y/explorerandui/menucomponents are loaded separately rather than as part of a combined component), and to produce an error message if messages are being registered to a component that hasn't been defined.