Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.
TypeScript debiandeep-videojitsijitsi-meetscalable-video-conferencessfuvideovideo-communicationvideo-conferencingwebrtc
fix(i18n): keep config.defaultLanguage as the last fallback on web (#18146)
Дамян Минков6745c6a3c517
diff --git a/react/features/base/i18n/functions.native.tsx b/react/features/base/i18n/functions.native.tsxindex ca876131c7..8faaa123d1 100644--- a/react/features/base/i18n/functions.native.tsx+++ b/react/features/base/i18n/functions.native.tsx@@ -17,3 +17,14 @@ export function translateToHTML(t: Function, key: string, options: Record<stringreturn <Text>{ text }</Text>;}++/**+ * Resolves the language to use once config.js is known. The system locale is the only detected language on native,+ * so config.defaultLanguage wins over it.+ *+ * @param {string} defaultLanguage - The config.defaultLanguage value, if any.+ * @returns {string | undefined}+ */+export function resolveConfigLanguage(defaultLanguage?: string) {+ return defaultLanguage;+}diff --git a/react/features/base/i18n/functions.web.tsx b/react/features/base/i18n/functions.web.tsxindex 6c1bc80191..9a33607920 100644--- a/react/features/base/i18n/functions.web.tsx+++ b/react/features/base/i18n/functions.web.tsx@@ -1,7 +1,10 @@import DOMPurify from 'dompurify';import React from 'react';+import languageDetector, { migrateLegacyLanguage } from './languageDetector';+export { changeLanguageBundle, translate } from './functions.any';+export { storeUserSelectedLanguage } from './languageDetector';const SANITIZE_CONFIG = {ALLOWED_TAGS: [ 'a', 'b', 'br', 'span' ],@@ -65,3 +68,18 @@ export function translateToHTML(t: Function, key: string, options: Record<string// eslint-disable-next-line react/no-dangerreturn <span dangerouslySetInnerHTML = {{ __html: html }} />;}++/**+ * Resolves the language to use once config.js is known. The detection chain is run again, so a language from the+ * URL, the one the user picked or the browser one still wins over config.defaultLanguage.+ *+ * @param {string} _defaultLanguage - Not used on web: configLanguageDetector reads it from window.config, which is+ * the object SET_CONFIG carries.+ * @returns {string}+ */+export function resolveConfigLanguage(_defaultLanguage?: string): string {+ migrateLegacyLanguage();++ // Falls back to DEFAULT_LANGUAGE when nothing matched.+ return languageDetector.detect() as string;+}diff --git a/react/features/base/i18n/languageDetector.web.ts b/react/features/base/i18n/languageDetector.web.tsindex 695fa27d6f..4b5b576914 100644--- a/react/features/base/i18n/languageDetector.web.ts+++ b/react/features/base/i18n/languageDetector.web.ts@@ -1,3 +1,4 @@+import { jitsiLocalStorage } from '@jitsi/js-utils/jitsi-local-storage';import BrowserLanguageDetector from 'i18next-browser-languagedetector';import { noop } from 'lodash-es';@@ -33,6 +34,27 @@ const safeLangQueryDetector = {}};+/**+ * The key under which the language the user picked is stored.+ */+const USER_SELECTED_LANGUAGE_KEY = 'userSelectedLanguage';++/**+ * Language detector for the language the user picked in the settings. It goes through jitsiLocalStorage, so the pick+ * also survives in embeds that keep the local storage on the host page.+ */+const userSelectedLanguageDetector = {+ name: 'userSelectedLanguage',+ cacheUserLanguage(lng: string) {+ jitsiLocalStorage.setItem(USER_SELECTED_LANGUAGE_KEY, lng);+ },+ lookup() {+ const value = jitsiLocalStorage.getItem(USER_SELECTED_LANGUAGE_KEY);++ return (value && SUPPORTED_LANGUAGES.get(value.toLowerCase())) || undefined;+ }+};+/*** The ordered list (by name) of language detectors to be utilized as backends* by the singleton language detector for Web.@@ -41,7 +63,7 @@ const safeLangQueryDetector = {*/const order = [safeLangQueryDetector.name,- 'localStorage'+ userSelectedLanguageDetector.name];// Allow i18next to detect the system language reported by the Web browser@@ -58,13 +80,57 @@ const languageDetector= new BrowserLanguageDetector(/* services */ null,/* options */ {- caches: [ 'localStorage' ],- lookupLocalStorage: 'language',+ // Do not cache detected languages, or the config one would win over a later config change.+ caches: [],lookupQuerystring: 'lang',order});languageDetector.addDetector(safeLangQueryDetector);+languageDetector.addDetector(userSelectedLanguageDetector);languageDetector.addDetector(configLanguageDetector);+/**+ * Stores the language the user picked, so it is restored on the next load.+ *+ * @param {string} language - The language the user picked.+ * @returns {void}+ */+export function storeUserSelectedLanguage(language: string) {+ languageDetector.cacheUserLanguage(language, [ userSelectedLanguageDetector.name ]);+}++/**+ * Moves the language stored under the old 'language' key, where i18next used to cache every detected language, to+ * the key that holds the language the user picked. It cannot tell a pick from a cached one, so it is only kept when+ * it differs from what the browser and config give. Needs i18next to be initialized and config.js to be loaded.+ *+ * @returns {void}+ */+export function migrateLegacyLanguage() {+ try {+ const legacyLanguage = window.localStorage.getItem('language');++ if (legacyLanguage === null) {+ return;+ }++ const lang = SUPPORTED_LANGUAGES.get(legacyLanguage.toLowerCase());+ const detectionOrder = order.filter(+ name => name !== safeLangQueryDetector.name && name !== userSelectedLanguageDetector.name);++ if (lang+ && !userSelectedLanguageDetector.lookup()++ // @ts-expect-error The 3.0.1 types take DetectorOptions, the code takes the detection order.+ && lang !== languageDetector.detect(detectionOrder)) {+ storeUserSelectedLanguage(lang);+ }++ window.localStorage.removeItem('language');+ } catch {+ // localStorage may not be available.+ }+}+export default languageDetector;diff --git a/react/features/base/i18n/middleware.ts b/react/features/base/i18n/middleware.tsindex 24b2cdb147..7e37a48f5f 100644--- a/react/features/base/i18n/middleware.ts+++ b/react/features/base/i18n/middleware.ts@@ -5,7 +5,7 @@ import { SET_CONFIG } from '../config/actionTypes';import MiddlewareRegistry from '../redux/MiddlewareRegistry';import { I18NEXT_INITIALIZED, LANGUAGE_CHANGED } from './actionTypes';-import { changeLanguageBundle } from './functions';+import { changeLanguageBundle, resolveConfigLanguage } from './functions';import i18next, { SUPPORTED_NS } from './i18next';import logger from './logger';@@ -31,13 +31,13 @@ MiddlewareRegistry.register(store => next => action => {case SET_CONFIG: {// When config.js is fetched asynchronously (no window.config at boot),// configLanguageDetector returns undefined and i18next falls back. Once- // the store has the config, apply its defaultLanguage if it differs- // from what i18next picked. A language the user picked wins over it.- const defaultLanguage = action.config?.defaultLanguage;+ // the store has the config, apply its defaultLanguage unless a language+ // with a higher priority was detected. A language the user picked wins over it.const { userSelectedLanguage } = store.getState()['features/base/settings'];+ const language = resolveConfigLanguage(action.config?.defaultLanguage);- if (defaultLanguage && !userSelectedLanguage && defaultLanguage !== i18next.language) {- i18next.changeLanguage(defaultLanguage).catch(err => {+ if (language && !userSelectedLanguage && language !== i18next.language) {+ i18next.changeLanguage(language).catch(err => {logger.log('Error applying defaultLanguage from config', err);});}diff --git a/react/features/settings/actions.web.ts b/react/features/settings/actions.web.tsindex 3b46cae884..500a6579af 100644--- a/react/features/settings/actions.web.ts+++ b/react/features/settings/actions.web.ts@@ -10,6 +10,7 @@ import {import { getConferenceState } from '../base/conference/functions';import { hangup } from '../base/connection/actions.web';import { openDialog } from '../base/dialog/actions';+import { storeUserSelectedLanguage } from '../base/i18n/functions';import i18next from '../base/i18n/i18next';import { setJWT } from '../base/jwt/actions';import { browser } from '../base/lib-jitsi-meet';@@ -161,6 +162,7 @@ export function submitMoreTab(newState: any) {if (newState.currentLanguage !== currentState.currentLanguage) {i18next.changeLanguage(newState.currentLanguage);+ storeUserSelectedLanguage(newState.currentLanguage);const { conference } = getConferenceState(state);