bigbluebutton-Github/bigbluebutton-html5/imports/ui/services/locale/index.js

76 lines
1.6 KiB
JavaScript
Raw Normal View History

import { createIntl } from 'react-intl';
const FALLBACK_ON_EMPTY_STRING = window.meetingClientSettings.public.app.fallbackOnEmptyLocaleString;
2022-09-29 01:24:27 +08:00
/**
* Use this if you need any translation outside of React lifecycle.
*/
class BBBIntl {
_intl = {
tracker: new Tracker.Dependency(),
value: undefined,
};
_fetching = {
tracker: new Tracker.Dependency(),
value: true,
};
2022-09-29 01:24:27 +08:00
constructor({ fallback }) {
this._fallback = fallback;
}
setLocale(locale, messages) {
this.intl = createIntl({
locale,
messages,
2022-09-29 01:24:27 +08:00
fallbackOnEmptyString: this._fallback,
});
this.fetching = false;
}
set fetching(value) {
this._fetching.value = value;
this._fetching.tracker.changed();
}
get fetching() {
this._fetching.tracker.depend();
return this._fetching.value;
}
set intl(value) {
this._intl.value = value;
this._intl.tracker.changed();
}
get intl() {
this._intl.tracker.depend();
return this._intl.value;
}
formatMessage(descriptor, values, options) {
return new Promise((resolve, reject) => {
try {
if (!this.fetching && this.intl) {
resolve(this.intl.formatMessage(descriptor, values, options));
} else {
Tracker.autorun((c) => {
const { fetching, intl } = this;
if (fetching || !intl) return;
resolve(this.intl.formatMessage(descriptor, values, options));
c.stop();
});
}
} catch (e) {
reject(e);
}
});
}
}
2022-09-29 01:24:27 +08:00
export default new BBBIntl({ fallback: FALLBACK_ON_EMPTY_STRING });