Try/catch all intl stuff

This commit is contained in:
Lim Chee Aun 2024-08-20 19:58:50 +08:00
parent 5bf81b2269
commit 2db909d7af
4 changed files with 31 additions and 14 deletions

View file

@ -10,12 +10,16 @@ import states from '../utils/states';
import Avatar from './avatar';
import EmojiText from './emoji-text';
const nameCollator = mem(
(locale) =>
new Intl.Collator(locale || undefined, {
sensitivity: 'base',
}),
);
const nameCollator = mem((locale) => {
const options = {
sensitivity: 'base',
};
try {
return new Intl.Collator(locale || undefined, options);
} catch (e) {
return new Intl.Collator(undefined, options);
}
});
function NameText({
account,

View file

@ -20,8 +20,12 @@ i18n.on('change', () => {
// lang
document.documentElement.lang = lang;
// LTR or RTL
const { direction } = new Locale(lang).textInfo;
document.documentElement.dir = direction;
try {
const { direction } = new Locale(lang).textInfo;
document.documentElement.dir = direction;
} catch (e) {
console.error(e);
}
}
});

View file

@ -8,7 +8,7 @@ const _DateTimeFormat = (opts) => {
const { locale, dateYear, hideTime, formatOpts } = opts || {};
const loc = locale && !/pseudo/i.test(locale) ? locale : defaultLocale;
const currentYear = new Date().getFullYear();
return Intl.DateTimeFormat(loc, {
const options = {
// Show year if not current year
year: dateYear === currentYear ? undefined : 'numeric',
month: 'short',
@ -17,7 +17,12 @@ const _DateTimeFormat = (opts) => {
hour: hideTime ? undefined : 'numeric',
minute: hideTime ? undefined : 'numeric',
...formatOpts,
});
};
try {
return Intl.DateTimeFormat(loc, opts);
} catch (e) {
return Intl.DateTimeFormat(undefined, opts);
}
};
const DateTimeFormat = mem(_DateTimeFormat);

View file

@ -1,8 +1,12 @@
import { i18n } from '@lingui/core';
export default function shortenNumber(num) {
return i18n.number(num, {
notation: 'compact',
roundingMode: 'floor',
});
try {
return i18n.number(num, {
notation: 'compact',
roundingMode: 'floor',
});
} catch (e) {
return num;
}
}