Localization: Use basic ListFormat if not supported (#4710)

This commit is contained in:
zubiden 2024-07-15 15:50:44 +02:00 committed by Alexander Zinchuk
parent bbb5ef0a86
commit eb6694d061
5 changed files with 22 additions and 10 deletions

View File

@ -8,11 +8,10 @@ function compatTest() {
var hasIntl = typeof window.Intl !== 'undefined'; var hasIntl = typeof window.Intl !== 'undefined';
var hasDisplayNames = hasIntl && typeof Intl.DisplayNames !== 'undefined'; var hasDisplayNames = hasIntl && typeof Intl.DisplayNames !== 'undefined';
var hasPluralRules = hasIntl && typeof Intl.PluralRules !== 'undefined'; var hasPluralRules = hasIntl && typeof Intl.PluralRules !== 'undefined';
var hasListFormat = hasIntl && typeof Intl.ListFormat !== 'undefined';
var hasNumberFormat = hasIntl && typeof Intl.NumberFormat !== 'undefined'; var hasNumberFormat = hasIntl && typeof Intl.NumberFormat !== 'undefined';
var isCompatible = hasPromise && hasWebSockets && hasWebCrypto && hasObjectFromEntries && hasResizeObserver var isCompatible = hasPromise && hasWebSockets && hasWebCrypto && hasObjectFromEntries && hasResizeObserver
&& hasCssSupports && hasDisplayNames && hasPluralRules && hasListFormat && hasNumberFormat; && hasCssSupports && hasDisplayNames && hasPluralRules && hasNumberFormat;
if (isCompatible || (window.localStorage && window.localStorage.getItem('tt-ignore-compat'))) { if (isCompatible || (window.localStorage && window.localStorage.getItem('tt-ignore-compat'))) {
window.isCompatTestPassed = true; window.isCompatTestPassed = true;
@ -29,7 +28,6 @@ function compatTest() {
console.warn('CSS.supports', hasCssSupports); console.warn('CSS.supports', hasCssSupports);
console.warn('Intl.DisplayNames', hasDisplayNames); console.warn('Intl.DisplayNames', hasDisplayNames);
console.warn('Intl.PluralRules', hasPluralRules); console.warn('Intl.PluralRules', hasPluralRules);
console.warn('Intl.ListFormat', hasListFormat);
console.warn('Intl.NumberFormat', hasNumberFormat); console.warn('Intl.NumberFormat', hasNumberFormat);
} }

View File

@ -0,0 +1,5 @@
export function getBasicListFormat() {
return {
format: (items: string[]) => items.join(', '),
};
}

View File

@ -22,6 +22,7 @@ import { DEBUG } from '../../config';
import { callApi } from '../../api/gramjs'; import { callApi } from '../../api/gramjs';
import renderText, { type TextFilter } from '../../components/common/helpers/renderText'; import renderText, { type TextFilter } from '../../components/common/helpers/renderText';
import { MAIN_IDB_STORE } from '../browser/idb'; import { MAIN_IDB_STORE } from '../browser/idb';
import { getBasicListFormat } from '../browser/intlListFormat';
import { createCallbackManager } from '../callbacks'; import { createCallbackManager } from '../callbacks';
import readFallbackStrings from '../data/readFallbackStrings'; import readFallbackStrings from '../data/readFallbackStrings';
import { initialEstablishmentPromise, isCurrentTabMaster } from '../establishMultitabRole'; import { initialEstablishmentPromise, isCurrentTabMaster } from '../establishMultitabRole';
@ -29,7 +30,7 @@ import { omit } from '../iteratees';
import { notifyLangpackUpdate } from '../multitab'; import { notifyLangpackUpdate } from '../multitab';
import { replaceInStringsWithTeact } from '../replaceWithTeact'; import { replaceInStringsWithTeact } from '../replaceWithTeact';
import { fastRaf } from '../schedulers'; import { fastRaf } from '../schedulers';
import { IS_MULTITAB_SUPPORTED } from '../windowEnvironment'; import { IS_INTL_LIST_FORMAT_SUPPORTED, IS_MULTITAB_SUPPORTED } from '../windowEnvironment';
import Deferred from '../Deferred'; import Deferred from '../Deferred';
import LimitedMap from '../primitives/LimitedMap'; import LimitedMap from '../primitives/LimitedMap';
@ -145,13 +146,18 @@ function updateLanguage(newLang: ApiLanguage) {
function createFormatters() { function createFormatters() {
if (!language) return; if (!language) return;
const langCode = language.pluralCode; const langCode = language.pluralCode;
const listFormatFallback = getBasicListFormat();
function createListFormat(lang: string, type: 'conjunction' | 'disjunction') {
return IS_INTL_LIST_FORMAT_SUPPORTED ? new Intl.ListFormat(lang, { type }) : listFormatFallback;
}
try { try {
formatters = { formatters = {
pluralRules: new Intl.PluralRules(langCode), pluralRules: new Intl.PluralRules(langCode),
region: new Intl.DisplayNames(langCode, { type: 'region' }), region: new Intl.DisplayNames(langCode, { type: 'region' }),
conjunction: new Intl.ListFormat(langCode, { type: 'conjunction' }), conjunction: createListFormat(langCode, 'conjunction'),
disjunction: new Intl.ListFormat(langCode, { type: 'disjunction' }), disjunction: createListFormat(langCode, 'disjunction'),
number: new Intl.NumberFormat(langCode), number: new Intl.NumberFormat(langCode),
}; };
} catch (e) { } catch (e) {
@ -160,8 +166,8 @@ function createFormatters() {
formatters = { formatters = {
pluralRules: new Intl.PluralRules(FORMATTERS_FALLBACK_LANG), pluralRules: new Intl.PluralRules(FORMATTERS_FALLBACK_LANG),
region: new Intl.DisplayNames(FORMATTERS_FALLBACK_LANG, { type: 'region' }), region: new Intl.DisplayNames(FORMATTERS_FALLBACK_LANG, { type: 'region' }),
conjunction: new Intl.ListFormat(FORMATTERS_FALLBACK_LANG, { type: 'conjunction' }), conjunction: createListFormat(FORMATTERS_FALLBACK_LANG, 'conjunction'),
disjunction: new Intl.ListFormat(FORMATTERS_FALLBACK_LANG, { type: 'disjunction' }), disjunction: createListFormat(FORMATTERS_FALLBACK_LANG, 'disjunction'),
number: new Intl.NumberFormat(FORMATTERS_FALLBACK_LANG), number: new Intl.NumberFormat(FORMATTERS_FALLBACK_LANG),
}; };
} }

View File

@ -70,11 +70,13 @@ export type LangFn = {
pluralCode: string; pluralCode: string;
}; };
type ListFormat = Pick<Intl.ListFormat, 'format'>;
export type LangFormatters = { export type LangFormatters = {
pluralRules: Intl.PluralRules; pluralRules: Intl.PluralRules;
region: Intl.DisplayNames; region: Intl.DisplayNames;
conjunction: Intl.ListFormat; conjunction: ListFormat;
disjunction: Intl.ListFormat; disjunction: ListFormat;
number: Intl.NumberFormat; number: Intl.NumberFormat;
}; };

View File

@ -99,6 +99,7 @@ export const IS_INSTALL_PROMPT_SUPPORTED = 'onbeforeinstallprompt' in window;
export const IS_MULTITAB_SUPPORTED = 'BroadcastChannel' in window; export const IS_MULTITAB_SUPPORTED = 'BroadcastChannel' in window;
export const IS_OPEN_IN_NEW_TAB_SUPPORTED = IS_MULTITAB_SUPPORTED && !(IS_PWA && IS_MOBILE); export const IS_OPEN_IN_NEW_TAB_SUPPORTED = IS_MULTITAB_SUPPORTED && !(IS_PWA && IS_MOBILE);
export const IS_TRANSLATION_SUPPORTED = !IS_TEST; export const IS_TRANSLATION_SUPPORTED = !IS_TEST;
export const IS_INTL_LIST_FORMAT_SUPPORTED = 'ListFormat' in Intl;
export const MESSAGE_LIST_SENSITIVE_AREA = 750; export const MESSAGE_LIST_SENSITIVE_AREA = 750;