mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-11-22 09:15:33 +03:00
Try/catch match because it throws when there's invalid language code
This commit is contained in:
parent
04b3fd9545
commit
753789c0c7
4 changed files with 19 additions and 8 deletions
|
@ -1,6 +1,5 @@
|
||||||
import './compose.css';
|
import './compose.css';
|
||||||
|
|
||||||
import { match } from '@formatjs/intl-localematcher';
|
|
||||||
import '@github/text-expander-element';
|
import '@github/text-expander-element';
|
||||||
import equal from 'fast-deep-equal';
|
import equal from 'fast-deep-equal';
|
||||||
import { forwardRef } from 'preact/compat';
|
import { forwardRef } from 'preact/compat';
|
||||||
|
@ -16,6 +15,7 @@ import urlRegex from '../data/url-regex';
|
||||||
import { api } from '../utils/api';
|
import { api } from '../utils/api';
|
||||||
import db from '../utils/db';
|
import db from '../utils/db';
|
||||||
import emojifyText from '../utils/emojify-text';
|
import emojifyText from '../utils/emojify-text';
|
||||||
|
import localeMatch from '../utils/locale-match';
|
||||||
import openCompose from '../utils/open-compose';
|
import openCompose from '../utils/open-compose';
|
||||||
import states, { saveStatus } from '../utils/states';
|
import states, { saveStatus } from '../utils/states';
|
||||||
import store from '../utils/store';
|
import store from '../utils/store';
|
||||||
|
@ -85,7 +85,7 @@ const observer = new IntersectionObserver((entries) => {
|
||||||
});
|
});
|
||||||
observer.observe(menu);
|
observer.observe(menu);
|
||||||
|
|
||||||
const DEFAULT_LANG = match(
|
const DEFAULT_LANG = localeMatch(
|
||||||
[new Intl.DateTimeFormat().resolvedOptions().locale, ...navigator.languages],
|
[new Intl.DateTimeFormat().resolvedOptions().locale, ...navigator.languages],
|
||||||
supportedLanguages.map((l) => l[0]),
|
supportedLanguages.map((l) => l[0]),
|
||||||
'en',
|
'en',
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import './status.css';
|
import './status.css';
|
||||||
|
|
||||||
import { match } from '@formatjs/intl-localematcher';
|
|
||||||
import '@justinribeiro/lite-youtube';
|
import '@justinribeiro/lite-youtube';
|
||||||
import {
|
import {
|
||||||
ControlledMenu,
|
ControlledMenu,
|
||||||
|
@ -33,6 +32,7 @@ import getHTMLText from '../utils/getHTMLText';
|
||||||
import handleContentLinks from '../utils/handle-content-links';
|
import handleContentLinks from '../utils/handle-content-links';
|
||||||
import htmlContentLength from '../utils/html-content-length';
|
import htmlContentLength from '../utils/html-content-length';
|
||||||
import isMastodonLinkMaybe from '../utils/isMastodonLinkMaybe';
|
import isMastodonLinkMaybe from '../utils/isMastodonLinkMaybe';
|
||||||
|
import localeMatch from '../utils/locale-match';
|
||||||
import niceDateTime from '../utils/nice-date-time';
|
import niceDateTime from '../utils/nice-date-time';
|
||||||
import shortenNumber from '../utils/shorten-number';
|
import shortenNumber from '../utils/shorten-number';
|
||||||
import showToast from '../utils/show-toast';
|
import showToast from '../utils/show-toast';
|
||||||
|
@ -409,9 +409,9 @@ function Status({
|
||||||
const differentLanguage =
|
const differentLanguage =
|
||||||
language &&
|
language &&
|
||||||
language !== targetLanguage &&
|
language !== targetLanguage &&
|
||||||
!match([language], [targetLanguage]) &&
|
!localeMatch([language], [targetLanguage]) &&
|
||||||
!contentTranslationHideLanguages.find(
|
!contentTranslationHideLanguages.find(
|
||||||
(l) => language === l || match([language], [l]),
|
(l) => language === l || localeMatch([language], [l]),
|
||||||
);
|
);
|
||||||
|
|
||||||
const menuInstanceRef = useRef();
|
const menuInstanceRef = useRef();
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { match } from '@formatjs/intl-localematcher';
|
|
||||||
|
|
||||||
import translationTargetLanguages from '../data/lingva-target-languages';
|
import translationTargetLanguages from '../data/lingva-target-languages';
|
||||||
|
|
||||||
|
import localeMatch from './locale-match';
|
||||||
import states from './states';
|
import states from './states';
|
||||||
|
|
||||||
function getTranslateTargetLanguage(fromSettings = false) {
|
function getTranslateTargetLanguage(fromSettings = false) {
|
||||||
|
@ -11,7 +10,7 @@ function getTranslateTargetLanguage(fromSettings = false) {
|
||||||
return contentTranslationTargetLanguage;
|
return contentTranslationTargetLanguage;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return match(
|
return localeMatch(
|
||||||
[
|
[
|
||||||
new Intl.DateTimeFormat().resolvedOptions().locale,
|
new Intl.DateTimeFormat().resolvedOptions().locale,
|
||||||
...navigator.languages,
|
...navigator.languages,
|
||||||
|
|
12
src/utils/locale-match.jsx
Normal file
12
src/utils/locale-match.jsx
Normal 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;
|
Loading…
Reference in a new issue