feat(ui): support locale change option from UI (#9553)

This commit is contained in:
Chirag Madlani 2023-01-03 14:27:21 +05:30 committed by GitHub
parent 9c7e8ba424
commit 831edcf002
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 3 deletions

View File

@ -11,7 +11,9 @@
* limitations under the License. * limitations under the License.
*/ */
import { Badge, Dropdown, Image, Input, Menu, Space } from 'antd'; import { Badge, Dropdown, Image, Input, Menu, Select, Space } from 'antd';
import { CookieStorage } from 'cookie-storage';
import i18next from 'i18next';
import { debounce, toString } from 'lodash'; import { debounce, toString } from 'lodash';
import React, { useCallback, useEffect, useMemo, useState } from 'react'; import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
@ -32,6 +34,10 @@ import {
getEntityType, getEntityType,
prepareFeedLink, prepareFeedLink,
} from '../../utils/FeedUtils'; } from '../../utils/FeedUtils';
import {
languageSelectOptions,
SupportedLocales,
} from '../../utils/i18next/i18nextUtil';
import { import {
inPageSearchOptions, inPageSearchOptions,
isInPageSearchAllowed, isInPageSearchAllowed,
@ -50,6 +56,9 @@ import { WhatsNewModal } from '../Modals/WhatsNewModal';
import NotificationBox from '../NotificationBox/NotificationBox.component'; import NotificationBox from '../NotificationBox/NotificationBox.component';
import { useWebSocketConnector } from '../web-scoket/web-scoket.provider'; import { useWebSocketConnector } from '../web-scoket/web-scoket.provider';
import { NavBarProps } from './NavBar.interface'; import { NavBarProps } from './NavBar.interface';
const cookieStorage = new CookieStorage();
const NavBar = ({ const NavBar = ({
supportDropdown, supportDropdown,
profileDropdown, profileDropdown,
@ -86,6 +95,10 @@ const NavBar = ({
[currentUser] [currentUser]
); );
const [language, setLanguage] = useState(
cookieStorage.getItem('i18next') || SupportedLocales.English
);
const { socket } = useWebSocketConnector(); const { socket } = useWebSocketConnector();
const navStyle = (value: boolean) => { const navStyle = (value: boolean) => {
@ -262,6 +275,11 @@ const NavBar = ({
} }
}, [profilePicture]); }, [profilePicture]);
const handleLanguageChange = (langCode: string) => {
setLanguage(langCode);
i18next.changeLanguage(langCode);
};
return ( return (
<> <>
<div className="tw-h-16 tw-py-3 tw-border-b-2 tw-border-separator tw-bg-white"> <div className="tw-h-16 tw-py-3 tw-border-b-2 tw-border-separator tw-bg-white">
@ -374,7 +392,13 @@ const NavBar = ({
))} ))}
</div> </div>
<Space className="tw-ml-auto"> <Space className="tw-ml-auto">
<Space size={24}> <Space size={16}>
<Select
bordered={false}
options={languageSelectOptions}
value={language}
onChange={handleLanguageChange}
/>
<NavLink <NavLink
className="focus:tw-no-underline" className="focus:tw-no-underline"
data-testid="appbar-item-settings" data-testid="appbar-item-settings"

View File

@ -12,18 +12,33 @@
*/ */
import { InitOptions } from 'i18next'; import { InitOptions } from 'i18next';
import { map } from 'lodash';
import enUS from '../../locale/languages/en-us.json'; import enUS from '../../locale/languages/en-us.json';
import frFR from '../../locale/languages/fr-fr.json'; import frFR from '../../locale/languages/fr-fr.json';
export enum SupportedLocales {
English = 'en-US',
Français = 'fr-FR',
}
export const languageSelectOptions = map(SupportedLocales, (value, key) => ({
label: key,
value,
}));
// Returns i18next options // Returns i18next options
export const getInitOptions = (): InitOptions => { export const getInitOptions = (): InitOptions => {
return { return {
supportedLngs: ['en-US', 'fr-FR'], supportedLngs: Object.values(SupportedLocales),
resources: { resources: {
'en-US': { translation: enUS }, 'en-US': { translation: enUS },
'fr-FR': { translation: frFR }, 'fr-FR': { translation: frFR },
}, },
fallbackLng: ['en-US'], fallbackLng: ['en-US'],
detection: {
order: ['cookie'],
caches: ['cookie'], // cache user language on
},
interpolation: { interpolation: {
escapeValue: false, // XSS safety provided by React escapeValue: false, // XSS safety provided by React
}, },