From 753789c0c7d13979154ba5df877eaa90ed8f85cc Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Sat, 20 May 2023 14:14:35 +0800 Subject: [PATCH] Try/catch match because it throws when there's invalid language code --- src/components/compose.jsx | 4 ++-- src/components/status.jsx | 6 +++--- src/utils/get-translate-target-language.jsx | 5 ++--- src/utils/locale-match.jsx | 12 ++++++++++++ 4 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 src/utils/locale-match.jsx diff --git a/src/components/compose.jsx b/src/components/compose.jsx index 5d224337..abe4621b 100644 --- a/src/components/compose.jsx +++ b/src/components/compose.jsx @@ -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', diff --git a/src/components/status.jsx b/src/components/status.jsx index 30cba751..65391e5b 100644 --- a/src/components/status.jsx +++ b/src/components/status.jsx @@ -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(); diff --git a/src/utils/get-translate-target-language.jsx b/src/utils/get-translate-target-language.jsx index ce837079..7a598643 100644 --- a/src/utils/get-translate-target-language.jsx +++ b/src/utils/get-translate-target-language.jsx @@ -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, diff --git a/src/utils/locale-match.jsx b/src/utils/locale-match.jsx new file mode 100644 index 00000000..f67d1fad --- /dev/null +++ b/src/utils/locale-match.jsx @@ -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;