Try/catch match because it throws when there's invalid language code

This commit is contained in:
Lim Chee Aun 2023-05-20 14:14:35 +08:00
parent 04b3fd9545
commit 753789c0c7
4 changed files with 19 additions and 8 deletions

View file

@ -1,6 +1,5 @@
import './compose.css';
import { match } from '@formatjs/intl-localematcher';
import '@github/text-expander-element';
import equal from 'fast-deep-equal';
import { forwardRef } from 'preact/compat';
@ -16,6 +15,7 @@ import urlRegex from '../data/url-regex';
import { api } from '../utils/api';
import db from '../utils/db';
import emojifyText from '../utils/emojify-text';
import localeMatch from '../utils/locale-match';
import openCompose from '../utils/open-compose';
import states, { saveStatus } from '../utils/states';
import store from '../utils/store';
@ -85,7 +85,7 @@ const observer = new IntersectionObserver((entries) => {
});
observer.observe(menu);
const DEFAULT_LANG = match(
const DEFAULT_LANG = localeMatch(
[new Intl.DateTimeFormat().resolvedOptions().locale, ...navigator.languages],
supportedLanguages.map((l) => l[0]),
'en',

View file

@ -1,6 +1,5 @@
import './status.css';
import { match } from '@formatjs/intl-localematcher';
import '@justinribeiro/lite-youtube';
import {
ControlledMenu,
@ -33,6 +32,7 @@ import getHTMLText from '../utils/getHTMLText';
import handleContentLinks from '../utils/handle-content-links';
import htmlContentLength from '../utils/html-content-length';
import isMastodonLinkMaybe from '../utils/isMastodonLinkMaybe';
import localeMatch from '../utils/locale-match';
import niceDateTime from '../utils/nice-date-time';
import shortenNumber from '../utils/shorten-number';
import showToast from '../utils/show-toast';
@ -409,9 +409,9 @@ function Status({
const differentLanguage =
language &&
language !== targetLanguage &&
!match([language], [targetLanguage]) &&
!localeMatch([language], [targetLanguage]) &&
!contentTranslationHideLanguages.find(
(l) => language === l || match([language], [l]),
(l) => language === l || localeMatch([language], [l]),
);
const menuInstanceRef = useRef();

View file

@ -1,7 +1,6 @@
import { match } from '@formatjs/intl-localematcher';
import translationTargetLanguages from '../data/lingva-target-languages';
import localeMatch from './locale-match';
import states from './states';
function getTranslateTargetLanguage(fromSettings = false) {
@ -11,7 +10,7 @@ function getTranslateTargetLanguage(fromSettings = false) {
return contentTranslationTargetLanguage;
}
}
return match(
return localeMatch(
[
new Intl.DateTimeFormat().resolvedOptions().locale,
...navigator.languages,

View file

@ -0,0 +1,12 @@
import { match } from '@formatjs/intl-localematcher';
function localeMatch(...args) {
// Wrap in try/catch because localeMatcher throws on invalid locales
try {
return match(...args);
} catch (e) {
return false;
}
}
export default localeMatch;