mirror of
https://github.com/umami-software/umami.git
synced 2026-02-09 15:17:23 +01:00
Support i18n.
This commit is contained in:
parent
f0ac9b6522
commit
e8538f6e23
14 changed files with 372 additions and 36 deletions
42
components/common/LanguageButton.js
Normal file
42
components/common/LanguageButton.js
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import React, { useState, useRef } from 'react';
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import Globe from 'assets/globe.svg';
|
||||
import useDocumentClick from 'hooks/useDocumentClick';
|
||||
import { updateApp } from 'redux/actions/app';
|
||||
import Menu from './Menu';
|
||||
import styles from './LanguageButton.module.css';
|
||||
import Button from './Button';
|
||||
|
||||
const menuOptions = [
|
||||
{ label: 'English', value: 'en' },
|
||||
{ label: '中文 (Chinese)', value: 'zh-CN' },
|
||||
];
|
||||
|
||||
export default function LanguageButton() {
|
||||
const dispatch = useDispatch();
|
||||
const [showMenu, setShowMenu] = useState(false);
|
||||
const locale = useSelector(state => state.app.locale);
|
||||
const ref = useRef();
|
||||
const selectedLocale = menuOptions.find(e => e.value === locale)?.label.split(' ')[0];
|
||||
|
||||
function handleSelect(value) {
|
||||
dispatch(updateApp({ locale: value }));
|
||||
window.localStorage.setItem('locale', value);
|
||||
setShowMenu(false);
|
||||
}
|
||||
|
||||
useDocumentClick(e => {
|
||||
if (!ref.current.contains(e.target)) {
|
||||
setShowMenu(false);
|
||||
}
|
||||
});
|
||||
|
||||
return (
|
||||
<div ref={ref} className={styles.container}>
|
||||
<Button icon={<Globe />} onClick={() => setShowMenu(true)} size="small">
|
||||
<div>{selectedLocale}</div>
|
||||
</Button>
|
||||
{showMenu && <Menu options={menuOptions} onSelect={handleSelect} float="top" />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
5
components/common/LanguageButton.module.css
Normal file
5
components/common/LanguageButton.module.css
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
.container {
|
||||
display: flex;
|
||||
position: relative;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import React, { useState, useRef } from 'react';
|
||||
import { FormattedMessage } from 'react-intl';
|
||||
import { useSelector } from 'react-redux';
|
||||
import { useRouter } from 'next/router';
|
||||
import Menu from './Menu';
|
||||
|
|
@ -17,14 +18,16 @@ export default function UserButton() {
|
|||
const menuOptions = [
|
||||
{
|
||||
label: (
|
||||
<>
|
||||
Logged in as <b>{user.username}</b>
|
||||
</>
|
||||
<FormattedMessage
|
||||
id="label.logged-in-as"
|
||||
defaultMessage="Logged in as {username}"
|
||||
values={{ username: <b>{user.username}</b> }}
|
||||
/>
|
||||
),
|
||||
value: 'username',
|
||||
className: styles.username,
|
||||
},
|
||||
{ label: 'Logout', value: 'logout' },
|
||||
{ label: <FormattedMessage id="label.logout" defaultMessage="Logout" />, value: 'logout' },
|
||||
];
|
||||
|
||||
function handleSelect(value) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue