2022-09-29 00:48:27 +08:00
|
|
|
import { createIntl } from 'react-intl';
|
|
|
|
|
2024-03-07 01:28:18 +08:00
|
|
|
const FALLBACK_ON_EMPTY_STRING = window.meetingClientSettings.public.app.fallbackOnEmptyLocaleString;
|
2022-09-29 01:24:27 +08:00
|
|
|
|
2022-09-29 00:48:27 +08:00
|
|
|
/**
|
2024-03-18 21:58:53 +08:00
|
|
|
* Use this if you need any translation outside of React lifecycle.
|
2022-09-29 00:48:27 +08:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-29 00:48:27 +08:00
|
|
|
setLocale(locale, messages) {
|
|
|
|
this.intl = createIntl({
|
|
|
|
locale,
|
|
|
|
messages,
|
2022-09-29 01:24:27 +08:00
|
|
|
fallbackOnEmptyString: this._fallback,
|
2022-09-29 00:48:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
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 });
|