Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions testsuite/tests/util/Locale.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, test, expect } from '@jest/globals';
import { mathjax } from '#js/mathjax.js';
import { Locale } from '#js/util/Locale.js';
import '#js/util/asyncLoad/esm.js';

Expand Down Expand Up @@ -120,6 +121,46 @@ describe('Locale', () => {
});

/********************************************************************************/

test('Locale sync', async () => {
const locale = Locale as any;
Locale.registerLocaleFiles('sync', '../testsuite/lib/component');

//
// Save environment
//
const [load, sync] = [mathjax.asyncLoad, mathjax.asyncIsSynchronous];
const error = console.error;

//
// Test synchronous loading failure
//
mathjax.asyncLoad = () => {throw Error('failed!')};
mathjax.asyncIsSynchronous = true;

const log: string[] = [];
console.error = (msg) => log.push(msg);

locale.getLocaleData('sync', 'en', 'en.json').catch(() => {});
expect(log).toEqual(["MathJax(sync): Can't load 'en.json': failed!"]);

//
// Test synchronous loading success
//
mathjax.asyncLoad = () => {return {'test': 'A test'}};

locale.getLocaleData('sync', 'en', 'en.json');
expect(Locale.message('sync', 'test')).toBe('A test');

//
// Restore environment
//
mathjax.asyncLoad = load;
mathjax.asyncIsSynchronous = sync;
console.error = error;
});

/********************************************************************************/
});

/**********************************************************************************/
Expand Down
20 changes: 14 additions & 6 deletions ts/util/Locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @author dpvc@mathjax.org (Davide Cervone)
*/

import { mathjax } from '../mathjax.js';
import { asyncLoad } from './AsyncLoad.js';

/**
Expand Down Expand Up @@ -123,7 +124,7 @@ export class Locale {
*
* @param {string} component The component whose message is requested
* @param {string} id The id of the message
* @param {string|namedData} data The first argument or the object of names arguments
* @param {string|namedData} data The first argument or the object of named arguments
* @param {string[]} args Any additional string arguments (if data is a string)
* @returns {string} The localized message with arguments substituted in
*/
Expand All @@ -147,11 +148,10 @@ export class Locale {
* Process a message string by substituting the given arguments. The arguments
* can be positional, or a data mapping of names to values.
*
* @param {string} message The message string to process.
* @param {string| namedData} data The first argument or the object of
* names arguments
* @param {string[]} args Additional arguments (if data is a string)
* @returns {string} The processed message string with arguments substituted
* @param {string} message The message string to process.
* @param {string|namedData} data The first argument or the object of named arguments
* @param {string[]} args Additional arguments (if data is a string)
* @returns {string} The processed message string with arguments substituted
*/
public static processMessage(
message: string,
Expand Down Expand Up @@ -262,6 +262,14 @@ export class Locale {
locale: string,
file: string
): Promise<void> {
if (mathjax.asyncIsSynchronous) {
try {
this.registerMessages(component, locale, mathjax.asyncLoad(file));
} catch (error) {
await this.localeError(component, locale, error);
}
return;
}
return asyncLoad(file)
.then((data: messageData) =>
this.registerMessages(component, locale, data)
Expand Down